Format: application/octet-stream
⬇ Download File (8.1 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     "source": [
  18      "## Applied Machine Learning, Module 1:  A simple classification task"
  19     ]
  20    },
  21    {
  22     "cell_type": "markdown",
  23     "metadata": {},
  24     "source": [
  25      "### Import required modules and load data file"
  26     ]
  27    },
  28    {
  29     "cell_type": "code",
  30     "execution_count": null,
  31     "metadata": {
  32      "collapsed": true
  33     },
  34     "outputs": [],
  35     "source": [
  36      "%matplotlib notebook\n",
  37      "import numpy as np\n",
  38      "import matplotlib.pyplot as plt\n",
  39      "import pandas as pd\n",
  40      "from sklearn.model_selection import train_test_split\n",
  41      "\n",
  42      "fruits = pd.read_table('fruit_data_with_colors.txt')"
  43     ]
  44    },
  45    {
  46     "cell_type": "code",
  47     "execution_count": null,
  48     "metadata": {
  49      "collapsed": false
  50     },
  51     "outputs": [],
  52     "source": [
  53      "fruits.head()"
  54     ]
  55    },
  56    {
  57     "cell_type": "code",
  58     "execution_count": null,
  59     "metadata": {
  60      "collapsed": false
  61     },
  62     "outputs": [],
  63     "source": [
  64      "# create a mapping from fruit label value to fruit name to make results easier to interpret\n",
  65      "lookup_fruit_name = dict(zip(fruits.fruit_label.unique(), fruits.fruit_name.unique()))   \n",
  66      "lookup_fruit_name"
  67     ]
  68    },
  69    {
  70     "cell_type": "markdown",
  71     "metadata": {},
  72     "source": [
  73      "The file contains the mass, height, and width of a selection of oranges, lemons and apples. The heights were measured along the core of the fruit. The widths were the widest width perpendicular to the height."
  74     ]
  75    },
  76    {
  77     "cell_type": "markdown",
  78     "metadata": {},
  79     "source": [
  80      "### Examining the data"
  81     ]
  82    },
  83    {
  84     "cell_type": "code",
  85     "execution_count": null,
  86     "metadata": {
  87      "collapsed": false
  88     },
  89     "outputs": [],
  90     "source": [
  91      "# plotting a scatter matrix\n",
  92      "from matplotlib import cm\n",
  93      "\n",
  94      "X = fruits[['height', 'width', 'mass', 'color_score']]\n",
  95      "y = fruits['fruit_label']\n",
  96      "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)\n",
  97      "\n",
  98      "cmap = cm.get_cmap('gnuplot')\n",
  99      "scatter = pd.scatter_matrix(X_train, c= y_train, marker = 'o', s=40, hist_kwds={'bins':15}, figsize=(9,9), cmap=cmap)"
 100     ]
 101    },
 102    {
 103     "cell_type": "code",
 104     "execution_count": null,
 105     "metadata": {
 106      "collapsed": false
 107     },
 108     "outputs": [],
 109     "source": [
 110      "# plotting a 3D scatter plot\n",
 111      "from mpl_toolkits.mplot3d import Axes3D\n",
 112      "\n",
 113      "fig = plt.figure()\n",
 114      "ax = fig.add_subplot(111, projection = '3d')\n",
 115      "ax.scatter(X_train['width'], X_train['height'], X_train['color_score'], c = y_train, marker = 'o', s=100)\n",
 116      "ax.set_xlabel('width')\n",
 117      "ax.set_ylabel('height')\n",
 118      "ax.set_zlabel('color_score')\n",
 119      "plt.show()"
 120     ]
 121    },
 122    {
 123     "cell_type": "markdown",
 124     "metadata": {},
 125     "source": [
 126      "### Create train-test split"
 127     ]
 128    },
 129    {
 130     "cell_type": "code",
 131     "execution_count": null,
 132     "metadata": {
 133      "collapsed": true
 134     },
 135     "outputs": [],
 136     "source": [
 137      "# For this example, we use the mass, width, and height features of each fruit instance\n",
 138      "X = fruits[['mass', 'width', 'height']]\n",
 139      "y = fruits['fruit_label']\n",
 140      "\n",
 141      "# default is 75% / 25% train-test split\n",
 142      "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)"
 143     ]
 144    },
 145    {
 146     "cell_type": "markdown",
 147     "metadata": {},
 148     "source": [
 149      "### Create classifier object"
 150     ]
 151    },
 152    {
 153     "cell_type": "code",
 154     "execution_count": null,
 155     "metadata": {
 156      "collapsed": true
 157     },
 158     "outputs": [],
 159     "source": [
 160      "from sklearn.neighbors import KNeighborsClassifier\n",
 161      "\n",
 162      "knn = KNeighborsClassifier(n_neighbors = 5)"
 163     ]
 164    },
 165    {
 166     "cell_type": "markdown",
 167     "metadata": {},
 168     "source": [
 169      "### Train the classifier (fit the estimator) using the training data"
 170     ]
 171    },
 172    {
 173     "cell_type": "code",
 174     "execution_count": null,
 175     "metadata": {
 176      "collapsed": false
 177     },
 178     "outputs": [],
 179     "source": [
 180      "knn.fit(X_train, y_train)"
 181     ]
 182    },
 183    {
 184     "cell_type": "markdown",
 185     "metadata": {},
 186     "source": [
 187      "### Estimate the accuracy of the classifier on future data, using the test data"
 188     ]
 189    },
 190    {
 191     "cell_type": "code",
 192     "execution_count": null,
 193     "metadata": {
 194      "collapsed": false
 195     },
 196     "outputs": [],
 197     "source": [
 198      "knn.score(X_test, y_test)"
 199     ]
 200    },
 201    {
 202     "cell_type": "markdown",
 203     "metadata": {},
 204     "source": [
 205      "### Use the trained k-NN classifier model to classify new, previously unseen objects"
 206     ]
 207    },
 208    {
 209     "cell_type": "code",
 210     "execution_count": null,
 211     "metadata": {
 212      "collapsed": false
 213     },
 214     "outputs": [],
 215     "source": [
 216      "# first example: a small fruit with mass 20g, width 4.3 cm, height 5.5 cm\n",
 217      "fruit_prediction = knn.predict([[20, 4.3, 5.5]])\n",
 218      "lookup_fruit_name[fruit_prediction[0]]"
 219     ]
 220    },
 221    {
 222     "cell_type": "code",
 223     "execution_count": null,
 224     "metadata": {
 225      "collapsed": false
 226     },
 227     "outputs": [],
 228     "source": [
 229      "# second example: a larger, elongated fruit with mass 100g, width 6.3 cm, height 8.5 cm\n",
 230      "fruit_prediction = knn.predict([[100, 6.3, 8.5]])\n",
 231      "lookup_fruit_name[fruit_prediction[0]]"
 232     ]
 233    },
 234    {
 235     "cell_type": "markdown",
 236     "metadata": {},
 237     "source": [
 238      "### Plot the decision boundaries of the k-NN classifier"
 239     ]
 240    },
 241    {
 242     "cell_type": "code",
 243     "execution_count": null,
 244     "metadata": {
 245      "collapsed": false
 246     },
 247     "outputs": [],
 248     "source": [
 249      "from adspy_shared_utilities import plot_fruit_knn\n",
 250      "\n",
 251      "plot_fruit_knn(X_train, y_train, 5, 'uniform')   # we choose 5 nearest neighbors"
 252     ]
 253    },
 254    {
 255     "cell_type": "markdown",
 256     "metadata": {},
 257     "source": [
 258      "### How sensitive is k-NN classification accuracy to the choice of the 'k' parameter?"
 259     ]
 260    },
 261    {
 262     "cell_type": "code",
 263     "execution_count": null,
 264     "metadata": {
 265      "collapsed": false
 266     },
 267     "outputs": [],
 268     "source": [
 269      "k_range = range(1,20)\n",
 270      "scores = []\n",
 271      "\n",
 272      "for k in k_range:\n",
 273      "    knn = KNeighborsClassifier(n_neighbors = k)\n",
 274      "    knn.fit(X_train, y_train)\n",
 275      "    scores.append(knn.score(X_test, y_test))\n",
 276      "\n",
 277      "plt.figure()\n",
 278      "plt.xlabel('k')\n",
 279      "plt.ylabel('accuracy')\n",
 280      "plt.scatter(k_range, scores)\n",
 281      "plt.xticks([0,5,10,15,20]);"
 282     ]
 283    },
 284    {
 285     "cell_type": "markdown",
 286     "metadata": {},
 287     "source": [
 288      "### How sensitive is k-NN classification accuracy to the train/test split proportion?"
 289     ]
 290    },
 291    {
 292     "cell_type": "code",
 293     "execution_count": null,
 294     "metadata": {
 295      "collapsed": false
 296     },
 297     "outputs": [],
 298     "source": [
 299      "t = [0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2]\n",
 300      "\n",
 301      "knn = KNeighborsClassifier(n_neighbors = 5)\n",
 302      "\n",
 303      "plt.figure()\n",
 304      "\n",
 305      "for s in t:\n",
 306      "\n",
 307      "    scores = []\n",
 308      "    for i in range(1,1000):\n",
 309      "        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1-s)\n",
 310      "        knn.fit(X_train, y_train)\n",
 311      "        scores.append(knn.score(X_test, y_test))\n",
 312      "    plt.plot(s, np.mean(scores), 'bo')\n",
 313      "\n",
 314      "plt.xlabel('Training set proportion (%)')\n",
 315      "plt.ylabel('accuracy');"
 316     ]
 317    },
 318    {
 319     "cell_type": "code",
 320     "execution_count": null,
 321     "metadata": {
 322      "collapsed": true
 323     },
 324     "outputs": [],
 325     "source": []
 326    }
 327   ],
 328   "metadata": {
 329    "anaconda-cloud": {},
 330    "kernelspec": {
 331     "display_name": "Python 3",
 332     "language": "python",
 333     "name": "python3"
 334    },
 335    "language_info": {
 336     "codemirror_mode": {
 337      "name": "ipython",
 338      "version": 3
 339     },
 340     "file_extension": ".py",
 341     "mimetype": "text/x-python",
 342     "name": "python",
 343     "nbconvert_exporter": "python",
 344     "pygments_lexer": "ipython3",
 345     "version": "3.5.2"
 346    }
 347   },
 348   "nbformat": 4,
 349   "nbformat_minor": 1
 350  }
 351