Format: application/octet-stream
⬇ Download File (23.6 KB)
   1  {
   2   "cells": [
   3    {
   4     "cell_type": "markdown",
   5     "metadata": {},
   6     "source": [
   7      "---\n",
   8      "\n",
   9      "_You are currently looking at **version 1.0** of this notebook. To download notebooks and datafiles, as well as get help on Jupyter notebooks in the Coursera platform, visit the [Jupyter Notebook FAQ](https://www.coursera.org/learn/python-machine-learning/resources/bANLa) course resource._\n",
  10      "\n",
  11      "---"
  12     ]
  13    },
  14    {
  15     "cell_type": "markdown",
  16     "metadata": {
  17      "collapsed": true
  18     },
  19     "source": [
  20      "# Applied Machine Learning: Module 4 (Supervised Learning, Part II)"
  21     ]
  22    },
  23    {
  24     "cell_type": "markdown",
  25     "metadata": {},
  26     "source": [
  27      "## Preamble and Datasets"
  28     ]
  29    },
  30    {
  31     "cell_type": "code",
  32     "execution_count": null,
  33     "metadata": {
  34      "collapsed": false,
  35      "scrolled": false
  36     },
  37     "outputs": [],
  38     "source": [
  39      "%matplotlib notebook\n",
  40      "import numpy as np\n",
  41      "import pandas as pd\n",
  42      "import seaborn as sn\n",
  43      "import matplotlib.pyplot as plt\n",
  44      "\n",
  45      "from sklearn.model_selection import train_test_split\n",
  46      "from sklearn.datasets import make_classification, make_blobs\n",
  47      "from matplotlib.colors import ListedColormap\n",
  48      "from sklearn.datasets import load_breast_cancer\n",
  49      "from adspy_shared_utilities import load_crime_dataset\n",
  50      "\n",
  51      "\n",
  52      "cmap_bold = ListedColormap(['#FFFF00', '#00FF00', '#0000FF','#000000'])\n",
  53      "\n",
  54      "# fruits dataset\n",
  55      "fruits = pd.read_table('fruit_data_with_colors.txt')\n",
  56      "\n",
  57      "feature_names_fruits = ['height', 'width', 'mass', 'color_score']\n",
  58      "X_fruits = fruits[feature_names_fruits]\n",
  59      "y_fruits = fruits['fruit_label']\n",
  60      "target_names_fruits = ['apple', 'mandarin', 'orange', 'lemon']\n",
  61      "\n",
  62      "X_fruits_2d = fruits[['height', 'width']]\n",
  63      "y_fruits_2d = fruits['fruit_label']\n",
  64      "\n",
  65      "# synthetic dataset for simple regression\n",
  66      "from sklearn.datasets import make_regression\n",
  67      "plt.figure()\n",
  68      "plt.title('Sample regression problem with one input variable')\n",
  69      "X_R1, y_R1 = make_regression(n_samples = 100, n_features=1,\n",
  70      "                            n_informative=1, bias = 150.0,\n",
  71      "                            noise = 30, random_state=0)\n",
  72      "plt.scatter(X_R1, y_R1, marker= 'o', s=50)\n",
  73      "plt.show()\n",
  74      "\n",
  75      "# synthetic dataset for more complex regression\n",
  76      "from sklearn.datasets import make_friedman1\n",
  77      "plt.figure()\n",
  78      "plt.title('Complex regression problem with one input variable')\n",
  79      "X_F1, y_F1 = make_friedman1(n_samples = 100, n_features = 7,\n",
  80      "                           random_state=0)\n",
  81      "\n",
  82      "plt.scatter(X_F1[:, 2], y_F1, marker= 'o', s=50)\n",
  83      "plt.show()\n",
  84      "\n",
  85      "# synthetic dataset for classification (binary)\n",
  86      "plt.figure()\n",
  87      "plt.title('Sample binary classification problem with two informative features')\n",
  88      "X_C2, y_C2 = make_classification(n_samples = 100, n_features=2,\n",
  89      "                                n_redundant=0, n_informative=2,\n",
  90      "                                n_clusters_per_class=1, flip_y = 0.1,\n",
  91      "                                class_sep = 0.5, random_state=0)\n",
  92      "plt.scatter(X_C2[:, 0], X_C2[:, 1], marker= 'o',\n",
  93      "           c=y_C2, s=50, cmap=cmap_bold)\n",
  94      "plt.show()\n",
  95      "\n",
  96      "# more difficult synthetic dataset for classification (binary)\n",
  97      "# with classes that are not linearly separable\n",
  98      "X_D2, y_D2 = make_blobs(n_samples = 100, n_features = 2,\n",
  99      "                       centers = 8, cluster_std = 1.3,\n",
 100      "                       random_state = 4)\n",
 101      "y_D2 = y_D2 % 2\n",
 102      "plt.figure()\n",
 103      "plt.title('Sample binary classification problem with non-linearly separable classes')\n",
 104      "plt.scatter(X_D2[:,0], X_D2[:,1], c=y_D2,\n",
 105      "           marker= 'o', s=50, cmap=cmap_bold)\n",
 106      "plt.show()\n",
 107      "\n",
 108      "# Breast cancer dataset for classification\n",
 109      "cancer = load_breast_cancer()\n",
 110      "(X_cancer, y_cancer) = load_breast_cancer(return_X_y = True)\n",
 111      "\n",
 112      "# Communities and Crime dataset\n",
 113      "(X_crime, y_crime) = load_crime_dataset()"
 114     ]
 115    },
 116    {
 117     "cell_type": "markdown",
 118     "metadata": {},
 119     "source": [
 120      "## Naive Bayes classifiers"
 121     ]
 122    },
 123    {
 124     "cell_type": "code",
 125     "execution_count": null,
 126     "metadata": {
 127      "collapsed": false
 128     },
 129     "outputs": [],
 130     "source": [
 131      "from sklearn.naive_bayes import GaussianNB\n",
 132      "from adspy_shared_utilities import plot_class_regions_for_classifier\n",
 133      "\n",
 134      "X_train, X_test, y_train, y_test = train_test_split(X_C2, y_C2, random_state=0)\n",
 135      "\n",
 136      "nbclf = GaussianNB().fit(X_train, y_train)\n",
 137      "plot_class_regions_for_classifier(nbclf, X_train, y_train, X_test, y_test,\n",
 138      "                                 'Gaussian Naive Bayes classifier: Dataset 1')"
 139     ]
 140    },
 141    {
 142     "cell_type": "code",
 143     "execution_count": null,
 144     "metadata": {
 145      "collapsed": false
 146     },
 147     "outputs": [],
 148     "source": [
 149      "X_train, X_test, y_train, y_test = train_test_split(X_D2, y_D2,\n",
 150      "                                                   random_state=0)\n",
 151      "\n",
 152      "nbclf = GaussianNB().fit(X_train, y_train)\n",
 153      "plot_class_regions_for_classifier(nbclf, X_train, y_train, X_test, y_test,\n",
 154      "                                 'Gaussian Naive Bayes classifier: Dataset 2')"
 155     ]
 156    },
 157    {
 158     "cell_type": "markdown",
 159     "metadata": {},
 160     "source": [
 161      "### Application to a real-world dataset"
 162     ]
 163    },
 164    {
 165     "cell_type": "code",
 166     "execution_count": null,
 167     "metadata": {
 168      "collapsed": false
 169     },
 170     "outputs": [],
 171     "source": [
 172      "X_train, X_test, y_train, y_test = train_test_split(X_cancer, y_cancer, random_state = 0)\n",
 173      "\n",
 174      "nbclf = GaussianNB().fit(X_train, y_train)\n",
 175      "print('Breast cancer dataset')\n",
 176      "print('Accuracy of GaussianNB classifier on training set: {:.2f}'\n",
 177      "     .format(nbclf.score(X_train, y_train)))\n",
 178      "print('Accuracy of GaussianNB classifier on test set: {:.2f}'\n",
 179      "     .format(nbclf.score(X_test, y_test)))"
 180     ]
 181    },
 182    {
 183     "cell_type": "markdown",
 184     "metadata": {},
 185     "source": [
 186      "## Ensembles of Decision Trees"
 187     ]
 188    },
 189    {
 190     "cell_type": "markdown",
 191     "metadata": {},
 192     "source": [
 193      "### Random forests"
 194     ]
 195    },
 196    {
 197     "cell_type": "code",
 198     "execution_count": null,
 199     "metadata": {
 200      "collapsed": false,
 201      "scrolled": false
 202     },
 203     "outputs": [],
 204     "source": [
 205      "from sklearn.ensemble import RandomForestClassifier\n",
 206      "from sklearn.model_selection import train_test_split\n",
 207      "from adspy_shared_utilities import plot_class_regions_for_classifier_subplot\n",
 208      "\n",
 209      "X_train, X_test, y_train, y_test = train_test_split(X_D2, y_D2,\n",
 210      "                                                   random_state = 0)\n",
 211      "fig, subaxes = plt.subplots(1, 1, figsize=(6, 6))\n",
 212      "\n",
 213      "clf = RandomForestClassifier().fit(X_train, y_train)\n",
 214      "title = 'Random Forest Classifier, complex binary dataset, default settings'\n",
 215      "plot_class_regions_for_classifier_subplot(clf, X_train, y_train, X_test,\n",
 216      "                                         y_test, title, subaxes)\n",
 217      "\n",
 218      "plt.show()"
 219     ]
 220    },
 221    {
 222     "cell_type": "markdown",
 223     "metadata": {},
 224     "source": [
 225      "### Random forest: Fruit dataset"
 226     ]
 227    },
 228    {
 229     "cell_type": "code",
 230     "execution_count": null,
 231     "metadata": {
 232      "collapsed": false,
 233      "scrolled": false
 234     },
 235     "outputs": [],
 236     "source": [
 237      "from sklearn.ensemble import RandomForestClassifier\n",
 238      "from sklearn.model_selection import train_test_split\n",
 239      "from adspy_shared_utilities import plot_class_regions_for_classifier_subplot\n",
 240      "\n",
 241      "X_train, X_test, y_train, y_test = train_test_split(X_fruits.as_matrix(),\n",
 242      "                                                   y_fruits.as_matrix(),\n",
 243      "                                                   random_state = 0)\n",
 244      "fig, subaxes = plt.subplots(6, 1, figsize=(6, 32))\n",
 245      "\n",
 246      "title = 'Random Forest, fruits dataset, default settings'\n",
 247      "pair_list = [[0,1], [0,2], [0,3], [1,2], [1,3], [2,3]]\n",
 248      "\n",
 249      "for pair, axis in zip(pair_list, subaxes):\n",
 250      "    X = X_train[:, pair]\n",
 251      "    y = y_train\n",
 252      "    \n",
 253      "    clf = RandomForestClassifier().fit(X, y)\n",
 254      "    plot_class_regions_for_classifier_subplot(clf, X, y, None,\n",
 255      "                                             None, title, axis,\n",
 256      "                                             target_names_fruits)\n",
 257      "    \n",
 258      "    axis.set_xlabel(feature_names_fruits[pair[0]])\n",
 259      "    axis.set_ylabel(feature_names_fruits[pair[1]])\n",
 260      "    \n",
 261      "plt.tight_layout()\n",
 262      "plt.show()\n",
 263      "\n",
 264      "clf = RandomForestClassifier(n_estimators = 10,\n",
 265      "                            random_state=0).fit(X_train, y_train)\n",
 266      "\n",
 267      "print('Random Forest, Fruit dataset, default settings')\n",
 268      "print('Accuracy of RF classifier on training set: {:.2f}'\n",
 269      "     .format(clf.score(X_train, y_train)))\n",
 270      "print('Accuracy of RF classifier on test set: {:.2f}'\n",
 271      "     .format(clf.score(X_test, y_test)))"
 272     ]
 273    },
 274    {
 275     "cell_type": "markdown",
 276     "metadata": {},
 277     "source": [
 278      "#### Random Forests on a real-world dataset"
 279     ]
 280    },
 281    {
 282     "cell_type": "code",
 283     "execution_count": null,
 284     "metadata": {
 285      "collapsed": false
 286     },
 287     "outputs": [],
 288     "source": [
 289      "from sklearn.ensemble import RandomForestClassifier\n",
 290      "\n",
 291      "X_train, X_test, y_train, y_test = train_test_split(X_cancer, y_cancer, random_state = 0)\n",
 292      "\n",
 293      "clf = RandomForestClassifier(max_features = 8, random_state = 0)\n",
 294      "clf.fit(X_train, y_train)\n",
 295      "\n",
 296      "print('Breast cancer dataset')\n",
 297      "print('Accuracy of RF classifier on training set: {:.2f}'\n",
 298      "     .format(clf.score(X_train, y_train)))\n",
 299      "print('Accuracy of RF classifier on test set: {:.2f}'\n",
 300      "     .format(clf.score(X_test, y_test)))"
 301     ]
 302    },
 303    {
 304     "cell_type": "markdown",
 305     "metadata": {},
 306     "source": [
 307      "### Gradient-boosted decision trees"
 308     ]
 309    },
 310    {
 311     "cell_type": "code",
 312     "execution_count": null,
 313     "metadata": {
 314      "collapsed": false
 315     },
 316     "outputs": [],
 317     "source": [
 318      "from sklearn.ensemble import GradientBoostingClassifier\n",
 319      "from sklearn.model_selection import train_test_split\n",
 320      "from adspy_shared_utilities import plot_class_regions_for_classifier_subplot\n",
 321      "\n",
 322      "X_train, X_test, y_train, y_test = train_test_split(X_D2, y_D2, random_state = 0)\n",
 323      "fig, subaxes = plt.subplots(1, 1, figsize=(6, 6))\n",
 324      "\n",
 325      "clf = GradientBoostingClassifier().fit(X_train, y_train)\n",
 326      "title = 'GBDT, complex binary dataset, default settings'\n",
 327      "plot_class_regions_for_classifier_subplot(clf, X_train, y_train, X_test,\n",
 328      "                                         y_test, title, subaxes)\n",
 329      "\n",
 330      "plt.show()"
 331     ]
 332    },
 333    {
 334     "cell_type": "markdown",
 335     "metadata": {},
 336     "source": [
 337      "#### Gradient boosted decision trees on the fruit dataset"
 338     ]
 339    },
 340    {
 341     "cell_type": "code",
 342     "execution_count": null,
 343     "metadata": {
 344      "collapsed": false,
 345      "scrolled": false
 346     },
 347     "outputs": [],
 348     "source": [
 349      "X_train, X_test, y_train, y_test = train_test_split(X_fruits.as_matrix(),\n",
 350      "                                                   y_fruits.as_matrix(),\n",
 351      "                                                   random_state = 0)\n",
 352      "fig, subaxes = plt.subplots(6, 1, figsize=(6, 32))\n",
 353      "\n",
 354      "pair_list = [[0,1], [0,2], [0,3], [1,2], [1,3], [2,3]]\n",
 355      "\n",
 356      "for pair, axis in zip(pair_list, subaxes):\n",
 357      "    X = X_train[:, pair]\n",
 358      "    y = y_train\n",
 359      "    \n",
 360      "    clf = GradientBoostingClassifier().fit(X, y)\n",
 361      "    plot_class_regions_for_classifier_subplot(clf, X, y, None,\n",
 362      "                                             None, title, axis,\n",
 363      "                                             target_names_fruits)\n",
 364      "    \n",
 365      "    axis.set_xlabel(feature_names_fruits[pair[0]])\n",
 366      "    axis.set_ylabel(feature_names_fruits[pair[1]])\n",
 367      "    \n",
 368      "plt.tight_layout()\n",
 369      "plt.show()\n",
 370      "clf = GradientBoostingClassifier().fit(X_train, y_train)\n",
 371      "\n",
 372      "print('GBDT, Fruit dataset, default settings')\n",
 373      "print('Accuracy of GBDT classifier on training set: {:.2f}'\n",
 374      "     .format(clf.score(X_train, y_train)))\n",
 375      "print('Accuracy of GBDT classifier on test set: {:.2f}'\n",
 376      "     .format(clf.score(X_test, y_test)))"
 377     ]
 378    },
 379    {
 380     "cell_type": "markdown",
 381     "metadata": {},
 382     "source": [
 383      "#### Gradient-boosted decision trees on a real-world dataset"
 384     ]
 385    },
 386    {
 387     "cell_type": "code",
 388     "execution_count": null,
 389     "metadata": {
 390      "collapsed": false
 391     },
 392     "outputs": [],
 393     "source": [
 394      "from sklearn.ensemble import GradientBoostingClassifier\n",
 395      "\n",
 396      "X_train, X_test, y_train, y_test = train_test_split(X_cancer, y_cancer, random_state = 0)\n",
 397      "\n",
 398      "clf = GradientBoostingClassifier(random_state = 0)\n",
 399      "clf.fit(X_train, y_train)\n",
 400      "\n",
 401      "print('Breast cancer dataset (learning_rate=0.1, max_depth=3)')\n",
 402      "print('Accuracy of GBDT classifier on training set: {:.2f}'\n",
 403      "     .format(clf.score(X_train, y_train)))\n",
 404      "print('Accuracy of GBDT classifier on test set: {:.2f}\\n'\n",
 405      "     .format(clf.score(X_test, y_test)))\n",
 406      "\n",
 407      "clf = GradientBoostingClassifier(learning_rate = 0.01, max_depth = 2, random_state = 0)\n",
 408      "clf.fit(X_train, y_train)\n",
 409      "\n",
 410      "print('Breast cancer dataset (learning_rate=0.01, max_depth=2)')\n",
 411      "print('Accuracy of GBDT classifier on training set: {:.2f}'\n",
 412      "     .format(clf.score(X_train, y_train)))\n",
 413      "print('Accuracy of GBDT classifier on test set: {:.2f}'\n",
 414      "     .format(clf.score(X_test, y_test)))"
 415     ]
 416    },
 417    {
 418     "cell_type": "markdown",
 419     "metadata": {},
 420     "source": [
 421      "## Neural networks"
 422     ]
 423    },
 424    {
 425     "cell_type": "markdown",
 426     "metadata": {},
 427     "source": [
 428      "#### Activation functions"
 429     ]
 430    },
 431    {
 432     "cell_type": "code",
 433     "execution_count": null,
 434     "metadata": {
 435      "collapsed": false
 436     },
 437     "outputs": [],
 438     "source": [
 439      "xrange = np.linspace(-2, 2, 200)\n",
 440      "\n",
 441      "plt.figure(figsize=(7,6))\n",
 442      "\n",
 443      "plt.plot(xrange, np.maximum(xrange, 0), label = 'relu')\n",
 444      "plt.plot(xrange, np.tanh(xrange), label = 'tanh')\n",
 445      "plt.plot(xrange, 1 / (1 + np.exp(-xrange)), label = 'logistic')\n",
 446      "plt.legend()\n",
 447      "plt.title('Neural network activation functions')\n",
 448      "plt.xlabel('Input value (x)')\n",
 449      "plt.ylabel('Activation function output')\n",
 450      "\n",
 451      "plt.show()"
 452     ]
 453    },
 454    {
 455     "cell_type": "markdown",
 456     "metadata": {},
 457     "source": [
 458      "### Neural networks: Classification"
 459     ]
 460    },
 461    {
 462     "cell_type": "markdown",
 463     "metadata": {},
 464     "source": [
 465      "#### Synthetic dataset 1: single hidden layer"
 466     ]
 467    },
 468    {
 469     "cell_type": "code",
 470     "execution_count": null,
 471     "metadata": {
 472      "collapsed": false,
 473      "scrolled": false
 474     },
 475     "outputs": [],
 476     "source": [
 477      "from sklearn.neural_network import MLPClassifier\n",
 478      "from adspy_shared_utilities import plot_class_regions_for_classifier_subplot\n",
 479      "\n",
 480      "X_train, X_test, y_train, y_test = train_test_split(X_D2, y_D2, random_state=0)\n",
 481      "\n",
 482      "fig, subaxes = plt.subplots(3, 1, figsize=(6,18))\n",
 483      "\n",
 484      "for units, axis in zip([1, 10, 100], subaxes):\n",
 485      "    nnclf = MLPClassifier(hidden_layer_sizes = [units], solver='lbfgs',\n",
 486      "                         random_state = 0).fit(X_train, y_train)\n",
 487      "    \n",
 488      "    title = 'Dataset 1: Neural net classifier, 1 layer, {} units'.format(units)\n",
 489      "    \n",
 490      "    plot_class_regions_for_classifier_subplot(nnclf, X_train, y_train,\n",
 491      "                                             X_test, y_test, title, axis)\n",
 492      "    plt.tight_layout()"
 493     ]
 494    },
 495    {
 496     "cell_type": "markdown",
 497     "metadata": {},
 498     "source": [
 499      "#### Synthetic dataset 1: two hidden layers"
 500     ]
 501    },
 502    {
 503     "cell_type": "code",
 504     "execution_count": null,
 505     "metadata": {
 506      "collapsed": false
 507     },
 508     "outputs": [],
 509     "source": [
 510      "from adspy_shared_utilities import plot_class_regions_for_classifier\n",
 511      "\n",
 512      "X_train, X_test, y_train, y_test = train_test_split(X_D2, y_D2, random_state=0)\n",
 513      "\n",
 514      "nnclf = MLPClassifier(hidden_layer_sizes = [10, 10], solver='lbfgs',\n",
 515      "                     random_state = 0).fit(X_train, y_train)\n",
 516      "\n",
 517      "plot_class_regions_for_classifier(nnclf, X_train, y_train, X_test, y_test,\n",
 518      "                                 'Dataset 1: Neural net classifier, 2 layers, 10/10 units')"
 519     ]
 520    },
 521    {
 522     "cell_type": "markdown",
 523     "metadata": {},
 524     "source": [
 525      "#### Regularization parameter: alpha"
 526     ]
 527    },
 528    {
 529     "cell_type": "code",
 530     "execution_count": null,
 531     "metadata": {
 532      "collapsed": false,
 533      "scrolled": false
 534     },
 535     "outputs": [],
 536     "source": [
 537      "X_train, X_test, y_train, y_test = train_test_split(X_D2, y_D2, random_state=0)\n",
 538      "\n",
 539      "fig, subaxes = plt.subplots(4, 1, figsize=(6, 23))\n",
 540      "\n",
 541      "for this_alpha, axis in zip([0.01, 0.1, 1.0, 5.0], subaxes):\n",
 542      "    nnclf = MLPClassifier(solver='lbfgs', activation = 'tanh',\n",
 543      "                         alpha = this_alpha,\n",
 544      "                         hidden_layer_sizes = [100, 100],\n",
 545      "                         random_state = 0).fit(X_train, y_train)\n",
 546      "    \n",
 547      "    title = 'Dataset 2: NN classifier, alpha = {:.3f} '.format(this_alpha)\n",
 548      "    \n",
 549      "    plot_class_regions_for_classifier_subplot(nnclf, X_train, y_train,\n",
 550      "                                             X_test, y_test, title, axis)\n",
 551      "    plt.tight_layout()\n",
 552      "    "
 553     ]
 554    },
 555    {
 556     "cell_type": "markdown",
 557     "metadata": {},
 558     "source": [
 559      "#### The effect of different choices of activation function"
 560     ]
 561    },
 562    {
 563     "cell_type": "code",
 564     "execution_count": null,
 565     "metadata": {
 566      "collapsed": false,
 567      "scrolled": false
 568     },
 569     "outputs": [],
 570     "source": [
 571      "X_train, X_test, y_train, y_test = train_test_split(X_D2, y_D2, random_state=0)\n",
 572      "\n",
 573      "fig, subaxes = plt.subplots(3, 1, figsize=(6,18))\n",
 574      "\n",
 575      "for this_activation, axis in zip(['logistic', 'tanh', 'relu'], subaxes):\n",
 576      "    nnclf = MLPClassifier(solver='lbfgs', activation = this_activation,\n",
 577      "                         alpha = 0.1, hidden_layer_sizes = [10, 10],\n",
 578      "                         random_state = 0).fit(X_train, y_train)\n",
 579      "    \n",
 580      "    title = 'Dataset 2: NN classifier, 2 layers 10/10, {} \\\n",
 581      "activation function'.format(this_activation)\n",
 582      "    \n",
 583      "    plot_class_regions_for_classifier_subplot(nnclf, X_train, y_train,\n",
 584      "                                             X_test, y_test, title, axis)\n",
 585      "    plt.tight_layout()"
 586     ]
 587    },
 588    {
 589     "cell_type": "markdown",
 590     "metadata": {},
 591     "source": [
 592      "### Neural networks: Regression"
 593     ]
 594    },
 595    {
 596     "cell_type": "code",
 597     "execution_count": null,
 598     "metadata": {
 599      "collapsed": false
 600     },
 601     "outputs": [],
 602     "source": [
 603      "from sklearn.neural_network import MLPRegressor\n",
 604      "\n",
 605      "fig, subaxes = plt.subplots(2, 3, figsize=(11,8), dpi=70)\n",
 606      "\n",
 607      "X_predict_input = np.linspace(-3, 3, 50).reshape(-1,1)\n",
 608      "\n",
 609      "X_train, X_test, y_train, y_test = train_test_split(X_R1[0::5], y_R1[0::5], random_state = 0)\n",
 610      "\n",
 611      "for thisaxisrow, thisactivation in zip(subaxes, ['tanh', 'relu']):\n",
 612      "    for thisalpha, thisaxis in zip([0.0001, 1.0, 100], thisaxisrow):\n",
 613      "        mlpreg = MLPRegressor(hidden_layer_sizes = [100,100],\n",
 614      "                             activation = thisactivation,\n",
 615      "                             alpha = thisalpha,\n",
 616      "                             solver = 'lbfgs').fit(X_train, y_train)\n",
 617      "        y_predict_output = mlpreg.predict(X_predict_input)\n",
 618      "        thisaxis.set_xlim([-2.5, 0.75])\n",
 619      "        thisaxis.plot(X_predict_input, y_predict_output,\n",
 620      "                     '^', markersize = 10)\n",
 621      "        thisaxis.plot(X_train, y_train, 'o')\n",
 622      "        thisaxis.set_xlabel('Input feature')\n",
 623      "        thisaxis.set_ylabel('Target value')\n",
 624      "        thisaxis.set_title('MLP regression\\nalpha={}, activation={})'\n",
 625      "                          .format(thisalpha, thisactivation))\n",
 626      "        plt.tight_layout()"
 627     ]
 628    },
 629    {
 630     "cell_type": "markdown",
 631     "metadata": {},
 632     "source": [
 633      "#### Application to real-world dataset for classification"
 634     ]
 635    },
 636    {
 637     "cell_type": "code",
 638     "execution_count": null,
 639     "metadata": {
 640      "collapsed": false
 641     },
 642     "outputs": [],
 643     "source": [
 644      "from sklearn.neural_network import MLPClassifier\n",
 645      "from sklearn.preprocessing import MinMaxScaler\n",
 646      "\n",
 647      "\n",
 648      "scaler = MinMaxScaler()\n",
 649      "\n",
 650      "X_train, X_test, y_train, y_test = train_test_split(X_cancer, y_cancer, random_state = 0)\n",
 651      "X_train_scaled = scaler.fit_transform(X_train)\n",
 652      "X_test_scaled = scaler.transform(X_test)\n",
 653      "\n",
 654      "clf = MLPClassifier(hidden_layer_sizes = [100, 100], alpha = 5.0,\n",
 655      "                   random_state = 0, solver='lbfgs').fit(X_train_scaled, y_train)\n",
 656      "\n",
 657      "print('Breast cancer dataset')\n",
 658      "print('Accuracy of NN classifier on training set: {:.2f}'\n",
 659      "     .format(clf.score(X_train_scaled, y_train)))\n",
 660      "print('Accuracy of NN classifier on test set: {:.2f}'\n",
 661      "     .format(clf.score(X_test_scaled, y_test)))"
 662     ]
 663    },
 664    {
 665     "cell_type": "code",
 666     "execution_count": 1,
 667     "metadata": {
 668      "collapsed": false
 669     },
 670     "outputs": [
 671      {
 672       "name": "stdout",
 673       "output_type": "stream",
 674       "text": [
 675        "./addresses.csv\r\n",
 676        "./train.csv\r\n",
 677        "./Module 2.ipynb\r\n",
 678        "./Assignment 3.ipynb\r\n",
 679        "./Module 4.ipynb\r\n",
 680        "./Assignment 1.ipynb\r\n",
 681        "./test.csv\r\n",
 682        "./CommViolPredUnnormalizedData.txt\r\n",
 683        "./adspy_shared_utilities.py\r\n",
 684        "./Module 3.ipynb\r\n",
 685        "./fraud_data.csv\r\n",
 686        "./fruit_data_with_colors.txt\r\n",
 687        "./Assignment 4.ipynb\r\n",
 688        "./Assignment 2.ipynb\r\n",
 689        "./mushrooms.csv\r\n",
 690        "./Classifier Visualization.ipynb\r\n",
 691        "./latlons.csv\r\n",
 692        "./Module 1.ipynb\r\n"
 693       ]
 694      }
 695     ],
 696     "source": [
 697      "!find . -maxdepth 1 -not -type d"
 698     ]
 699    },
 700    {
 701     "cell_type": "code",
 702     "execution_count": 7,
 703     "metadata": {
 704      "collapsed": false
 705     },
 706     "outputs": [
 707      {
 708       "name": "stdout",
 709       "output_type": "stream",
 710       "text": [
 711        "addresses.csv  adspy_temp.dot  polynomialreg1.png  train.csv\r\n"
 712       ]
 713      }
 714     ],
 715     "source": [
 716      "!ls readonly"
 717     ]
 718    },
 719    {
 720     "cell_type": "code",
 721     "execution_count": 8,
 722     "metadata": {
 723      "collapsed": false
 724     },
 725     "outputs": [
 726      {
 727       "name": "stdout",
 728       "output_type": "stream",
 729       "text": [
 730        "cp: target ‘2.ipynb’ is not a directory\r\n"
 731       ]
 732      }
 733     ],
 734     "source": [
 735      "!cp ./Module 2.ipynb readonly/Module 2.ipynb"
 736     ]
 737    },
 738    {
 739     "cell_type": "code",
 740     "execution_count": null,
 741     "metadata": {
 742      "collapsed": true
 743     },
 744     "outputs": [],
 745     "source": []
 746    }
 747   ],
 748   "metadata": {
 749    "anaconda-cloud": {},
 750    "kernelspec": {
 751     "display_name": "Python 3",
 752     "language": "python",
 753     "name": "python3"
 754    },
 755    "language_info": {
 756     "codemirror_mode": {
 757      "name": "ipython",
 758      "version": 3
 759     },
 760     "file_extension": ".py",
 761     "mimetype": "text/x-python",
 762     "name": "python",
 763     "nbconvert_exporter": "python",
 764     "pygments_lexer": "ipython3",
 765     "version": "3.5.2"
 766    }
 767   },
 768   "nbformat": 4,
 769   "nbformat_minor": 2
 770  }
 771