Format: application/octet-stream
⬇ Download File (13.4 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.3** 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      "# Assignment 1 - Introduction to Machine Learning"
  19     ]
  20    },
  21    {
  22     "cell_type": "markdown",
  23     "metadata": {},
  24     "source": [
  25      "For this assignment, you will be using the Breast Cancer Wisconsin (Diagnostic) Database to create a classifier that can help diagnose patients. First, read through the description of the dataset (below)."
  26     ]
  27    },
  28    {
  29     "cell_type": "code",
  30     "execution_count": 1,
  31     "metadata": {
  32      "collapsed": true
  33     },
  34     "outputs": [],
  35     "source": [
  36      "import numpy as np\n",
  37      "import pandas as pd\n",
  38      "from sklearn.datasets import load_breast_cancer\n",
  39      "\n",
  40      "cancer = load_breast_cancer()\n",
  41      "\n",
  42      "#print(cancer.DESCR) # Print the data set description"
  43     ]
  44    },
  45    {
  46     "cell_type": "markdown",
  47     "metadata": {},
  48     "source": [
  49      "The object returned by `load_breast_cancer()` is a scikit-learn Bunch object, which is similar to a dictionary."
  50     ]
  51    },
  52    {
  53     "cell_type": "code",
  54     "execution_count": 2,
  55     "metadata": {},
  56     "outputs": [
  57      {
  58       "data": {
  59        "text/plain": [
  60         "dict_keys(['feature_names', 'target_names', 'DESCR', 'data', 'target'])"
  61        ]
  62       },
  63       "execution_count": 2,
  64       "metadata": {},
  65       "output_type": "execute_result"
  66      }
  67     ],
  68     "source": [
  69      "cancer.keys()"
  70     ]
  71    },
  72    {
  73     "cell_type": "markdown",
  74     "metadata": {},
  75     "source": [
  76      "### Question 0 (Example)\n",
  77      "\n",
  78      "How many features does the breast cancer dataset have?\n",
  79      "\n",
  80      "*This function should return an integer.*"
  81     ]
  82    },
  83    {
  84     "cell_type": "code",
  85     "execution_count": 3,
  86     "metadata": {},
  87     "outputs": [
  88      {
  89       "data": {
  90        "text/plain": [
  91         "30"
  92        ]
  93       },
  94       "execution_count": 3,
  95       "metadata": {},
  96       "output_type": "execute_result"
  97      }
  98     ],
  99     "source": [
 100      "# You should write your whole answer within the function provided. The autograder will call\n",
 101      "# this function and compare the return value against the correct solution value\n",
 102      "def answer_zero():\n",
 103      "    # This function returns the number of features of the breast cancer dataset, which is an integer. \n",
 104      "    # The assignment question description will tell you the general format the autograder is expecting\n",
 105      "    return len(cancer['feature_names'])\n",
 106      "\n",
 107      "# You can examine what your function returns by calling it in the cell. If you have questions\n",
 108      "# about the assignment formats, check out the discussion forums for any FAQs\n",
 109      "answer_zero() "
 110     ]
 111    },
 112    {
 113     "cell_type": "markdown",
 114     "metadata": {},
 115     "source": [
 116      "### Question 1\n",
 117      "\n",
 118      "Scikit-learn works with lists, numpy arrays, scipy-sparse matrices, and pandas DataFrames, so converting the dataset to a DataFrame is not necessary for training this model. Using a DataFrame does however help make many things easier such as munging data, so let's practice creating a classifier with a pandas DataFrame. \n",
 119      "\n",
 120      "\n",
 121      "\n",
 122      "Convert the sklearn.dataset `cancer` to a DataFrame. \n",
 123      "\n",
 124      "*This function should return a `(569, 31)` DataFrame with * \n",
 125      "\n",
 126      "*columns = *\n",
 127      "\n",
 128      "    ['mean radius', 'mean texture', 'mean perimeter', 'mean area',\n",
 129      "    'mean smoothness', 'mean compactness', 'mean concavity',\n",
 130      "    'mean concave points', 'mean symmetry', 'mean fractal dimension',\n",
 131      "    'radius error', 'texture error', 'perimeter error', 'area error',\n",
 132      "    'smoothness error', 'compactness error', 'concavity error',\n",
 133      "    'concave points error', 'symmetry error', 'fractal dimension error',\n",
 134      "    'worst radius', 'worst texture', 'worst perimeter', 'worst area',\n",
 135      "    'worst smoothness', 'worst compactness', 'worst concavity',\n",
 136      "    'worst concave points', 'worst symmetry', 'worst fractal dimension',\n",
 137      "    'target']\n",
 138      "\n",
 139      "*and index = *\n",
 140      "\n",
 141      "    RangeIndex(start=0, stop=569, step=1)"
 142     ]
 143    },
 144    {
 145     "cell_type": "code",
 146     "execution_count": 4,
 147     "metadata": {
 148      "collapsed": true
 149     },
 150     "outputs": [],
 151     "source": [
 152      "def answer_one():\n",
 153      "    \n",
 154      "    # Your code here\n",
 155      "    \n",
 156      "    return # Return your answer\n",
 157      "\n",
 158      "\n",
 159      "answer_one()"
 160     ]
 161    },
 162    {
 163     "cell_type": "markdown",
 164     "metadata": {},
 165     "source": [
 166      "### Question 2\n",
 167      "What is the class distribution? (i.e. how many instances of `malignant` (encoded 0) and how many `benign` (encoded 1)?)\n",
 168      "\n",
 169      "*This function should return a Series named `target` of length 2 with integer values and index =* `['malignant', 'benign']`"
 170     ]
 171    },
 172    {
 173     "cell_type": "code",
 174     "execution_count": 5,
 175     "metadata": {
 176      "collapsed": true
 177     },
 178     "outputs": [],
 179     "source": [
 180      "def answer_two():\n",
 181      "    cancerdf = answer_one()\n",
 182      "    \n",
 183      "    # Your code here\n",
 184      "    \n",
 185      "    return # Return your answer\n",
 186      "\n",
 187      "\n",
 188      "answer_two()"
 189     ]
 190    },
 191    {
 192     "cell_type": "markdown",
 193     "metadata": {},
 194     "source": [
 195      "### Question 3\n",
 196      "Split the DataFrame into `X` (the data) and `y` (the labels).\n",
 197      "\n",
 198      "*This function should return a tuple of length 2:* `(X, y)`*, where* \n",
 199      "* `X`*, a pandas DataFrame, has shape* `(569, 30)`\n",
 200      "* `y`*, a pandas Series, has shape* `(569,)`."
 201     ]
 202    },
 203    {
 204     "cell_type": "code",
 205     "execution_count": null,
 206     "metadata": {
 207      "collapsed": true
 208     },
 209     "outputs": [],
 210     "source": [
 211      "def answer_three():\n",
 212      "    cancerdf = answer_one()\n",
 213      "    \n",
 214      "    # Your code here\n",
 215      "    \n",
 216      "    return X, y"
 217     ]
 218    },
 219    {
 220     "cell_type": "markdown",
 221     "metadata": {},
 222     "source": [
 223      "### Question 4\n",
 224      "Using `train_test_split`, split `X` and `y` into training and test sets `(X_train, X_test, y_train, and y_test)`.\n",
 225      "\n",
 226      "**Set the random number generator state to 0 using `random_state=0` to make sure your results match the autograder!**\n",
 227      "\n",
 228      "*This function should return a tuple of length 4:* `(X_train, X_test, y_train, y_test)`*, where* \n",
 229      "* `X_train` *has shape* `(426, 30)`\n",
 230      "* `X_test` *has shape* `(143, 30)`\n",
 231      "* `y_train` *has shape* `(426,)`\n",
 232      "* `y_test` *has shape* `(143,)`"
 233     ]
 234    },
 235    {
 236     "cell_type": "code",
 237     "execution_count": null,
 238     "metadata": {
 239      "collapsed": true
 240     },
 241     "outputs": [],
 242     "source": [
 243      "from sklearn.model_selection import train_test_split\n",
 244      "\n",
 245      "def answer_four():\n",
 246      "    X, y = answer_three()\n",
 247      "    \n",
 248      "    # Your code here\n",
 249      "    \n",
 250      "    return X_train, X_test, y_train, y_test"
 251     ]
 252    },
 253    {
 254     "cell_type": "markdown",
 255     "metadata": {},
 256     "source": [
 257      "### Question 5\n",
 258      "Using KNeighborsClassifier, fit a k-nearest neighbors (knn) classifier with `X_train`, `y_train` and using one nearest neighbor (`n_neighbors = 1`).\n",
 259      "\n",
 260      "*This function should return a * `sklearn.neighbors.classification.KNeighborsClassifier`."
 261     ]
 262    },
 263    {
 264     "cell_type": "code",
 265     "execution_count": null,
 266     "metadata": {
 267      "collapsed": true
 268     },
 269     "outputs": [],
 270     "source": [
 271      "from sklearn.neighbors import KNeighborsClassifier\n",
 272      "\n",
 273      "def answer_five():\n",
 274      "    X_train, X_test, y_train, y_test = answer_four()\n",
 275      "    \n",
 276      "    # Your code here\n",
 277      "    \n",
 278      "    return # Return your answer"
 279     ]
 280    },
 281    {
 282     "cell_type": "markdown",
 283     "metadata": {},
 284     "source": [
 285      "### Question 6\n",
 286      "Using your knn classifier, predict the class label using the mean value for each feature.\n",
 287      "\n",
 288      "Hint: You can use `cancerdf.mean()[:-1].values.reshape(1, -1)` which gets the mean value for each feature, ignores the target column, and reshapes the data from 1 dimension to 2 (necessary for the precict method of KNeighborsClassifier).\n",
 289      "\n",
 290      "*This function should return a numpy array either `array([ 0.])` or `array([ 1.])`*"
 291     ]
 292    },
 293    {
 294     "cell_type": "code",
 295     "execution_count": null,
 296     "metadata": {
 297      "collapsed": true
 298     },
 299     "outputs": [],
 300     "source": [
 301      "def answer_six():\n",
 302      "    cancerdf = answer_one()\n",
 303      "    means = cancerdf.mean()[:-1].values.reshape(1, -1)\n",
 304      "    \n",
 305      "    # Your code here\n",
 306      "    \n",
 307      "    return # Return your answer"
 308     ]
 309    },
 310    {
 311     "cell_type": "markdown",
 312     "metadata": {},
 313     "source": [
 314      "### Question 7\n",
 315      "Using your knn classifier, predict the class labels for the test set `X_test`.\n",
 316      "\n",
 317      "*This function should return a numpy array with shape `(143,)` and values either `0.0` or `1.0`.*"
 318     ]
 319    },
 320    {
 321     "cell_type": "code",
 322     "execution_count": null,
 323     "metadata": {
 324      "collapsed": true
 325     },
 326     "outputs": [],
 327     "source": [
 328      "def answer_seven():\n",
 329      "    X_train, X_test, y_train, y_test = answer_four()\n",
 330      "    knn = answer_five()\n",
 331      "    \n",
 332      "    # Your code here\n",
 333      "    \n",
 334      "    return # Return your answer"
 335     ]
 336    },
 337    {
 338     "cell_type": "markdown",
 339     "metadata": {},
 340     "source": [
 341      "### Question 8\n",
 342      "Find the score (mean accuracy) of your knn classifier using `X_test` and `y_test`.\n",
 343      "\n",
 344      "*This function should return a float between 0 and 1*"
 345     ]
 346    },
 347    {
 348     "cell_type": "code",
 349     "execution_count": null,
 350     "metadata": {
 351      "collapsed": true
 352     },
 353     "outputs": [],
 354     "source": [
 355      "def answer_eight():\n",
 356      "    X_train, X_test, y_train, y_test = answer_four()\n",
 357      "    knn = answer_five()\n",
 358      "    \n",
 359      "    # Your code here\n",
 360      "    \n",
 361      "    return # Return your answer"
 362     ]
 363    },
 364    {
 365     "cell_type": "markdown",
 366     "metadata": {},
 367     "source": [
 368      "### Optional plot\n",
 369      "\n",
 370      "Try using the plotting function below to visualize the differet predicition scores between training and test sets, as well as malignant and benign cells."
 371     ]
 372    },
 373    {
 374     "cell_type": "code",
 375     "execution_count": null,
 376     "metadata": {
 377      "collapsed": true
 378     },
 379     "outputs": [],
 380     "source": [
 381      "def accuracy_plot():\n",
 382      "    import matplotlib.pyplot as plt\n",
 383      "\n",
 384      "    %matplotlib notebook\n",
 385      "\n",
 386      "    X_train, X_test, y_train, y_test = answer_four()\n",
 387      "\n",
 388      "    # Find the training and testing accuracies by target value (i.e. malignant, benign)\n",
 389      "    mal_train_X = X_train[y_train==0]\n",
 390      "    mal_train_y = y_train[y_train==0]\n",
 391      "    ben_train_X = X_train[y_train==1]\n",
 392      "    ben_train_y = y_train[y_train==1]\n",
 393      "\n",
 394      "    mal_test_X = X_test[y_test==0]\n",
 395      "    mal_test_y = y_test[y_test==0]\n",
 396      "    ben_test_X = X_test[y_test==1]\n",
 397      "    ben_test_y = y_test[y_test==1]\n",
 398      "\n",
 399      "    knn = answer_five()\n",
 400      "\n",
 401      "    scores = [knn.score(mal_train_X, mal_train_y), knn.score(ben_train_X, ben_train_y), \n",
 402      "              knn.score(mal_test_X, mal_test_y), knn.score(ben_test_X, ben_test_y)]\n",
 403      "\n",
 404      "\n",
 405      "    plt.figure()\n",
 406      "\n",
 407      "    # Plot the scores as a bar chart\n",
 408      "    bars = plt.bar(np.arange(4), scores, color=['#4c72b0','#4c72b0','#55a868','#55a868'])\n",
 409      "\n",
 410      "    # directly label the score onto the bars\n",
 411      "    for bar in bars:\n",
 412      "        height = bar.get_height()\n",
 413      "        plt.gca().text(bar.get_x() + bar.get_width()/2, height*.90, '{0:.{1}f}'.format(height, 2), \n",
 414      "                     ha='center', color='w', fontsize=11)\n",
 415      "\n",
 416      "    # remove all the ticks (both axes), and tick labels on the Y axis\n",
 417      "    plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='off', labelbottom='on')\n",
 418      "\n",
 419      "    # remove the frame of the chart\n",
 420      "    for spine in plt.gca().spines.values():\n",
 421      "        spine.set_visible(False)\n",
 422      "\n",
 423      "    plt.xticks([0,1,2,3], ['Malignant\\nTraining', 'Benign\\nTraining', 'Malignant\\nTest', 'Benign\\nTest'], alpha=0.8);\n",
 424      "    plt.title('Training and Test Accuracies for Malignant and Benign Cells', alpha=0.8)"
 425     ]
 426    },
 427    {
 428     "cell_type": "markdown",
 429     "metadata": {},
 430     "source": [
 431      "Uncomment the plotting function to see the visualization.\n",
 432      "\n",
 433      "**Comment out** the plotting function when submitting your notebook for grading. "
 434     ]
 435    },
 436    {
 437     "cell_type": "code",
 438     "execution_count": null,
 439     "metadata": {
 440      "collapsed": true
 441     },
 442     "outputs": [],
 443     "source": [
 444      "#accuracy_plot() "
 445     ]
 446    },
 447    {
 448     "cell_type": "code",
 449     "execution_count": null,
 450     "metadata": {
 451      "collapsed": true
 452     },
 453     "outputs": [],
 454     "source": []
 455    }
 456   ],
 457   "metadata": {
 458    "coursera": {
 459     "course_slug": "python-machine-learning",
 460     "graded_item_id": "f9SY5",
 461     "launcher_item_id": "oxndk",
 462     "part_id": "mh1Vo"
 463    },
 464    "kernelspec": {
 465     "display_name": "Python 3",
 466     "language": "python",
 467     "name": "python3"
 468    },
 469    "language_info": {
 470     "codemirror_mode": {
 471      "name": "ipython",
 472      "version": 3
 473     },
 474     "file_extension": ".py",
 475     "mimetype": "text/x-python",
 476     "name": "python",
 477     "nbconvert_exporter": "python",
 478     "pygments_lexer": "ipython3",
 479     "version": "3.5.2"
 480    }
 481   },
 482   "nbformat": 4,
 483   "nbformat_minor": 0
 484  }
 485