Skip to content
Snippets Groups Projects
ProgrammingAssignment1.ipynb 15.6 KiB
Newer Older
Zeynep Hakguder's avatar
pa1
Zeynep Hakguder committed
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# *k*-Nearest Neighbor\n",
    "\n",
    "We'll implement *k*-Nearest Neighbor (*k*-NN) algorithm for this assignment. We will use the **madelon** dataset as in Programming Assignment 0.  \n",

Zeynep Hakguder's avatar
pa1
Zeynep Hakguder committed
    "\n",
    "A skeleton of a general supervised learning model is provided in \"model.ipynb\". The functions that will be implemented there will be indicated in this notebook. \n",
    "\n",
    "### Assignment Goals:\n",
    "In this assignment, we will:\n",
    "* implement 'Euclidean' and 'Manhattan' distance metrics \n",
    "* use the validation dataset to find a good value for *k*\n",
    "* evaluate our model with respect to performance measures:\n",
    "    * accuracy, generalization error and ROC curve\n",
    "* try to assess if *k*-NN is suitable for the dataset you used\n",
    "\n",
    "### Note:\n",
    "\n",
    "You are not required to follow this exact template. You can change what parameters your functions take or partition the tasks across functions differently. However, make sure there are outputs and implementation for items listed in the rubric for each task. Also, indicate in code with comments which task you are attempting."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# GRADING\n",
    "\n",
    "You will be graded on parts that are marked with **TODO** comments. Read the comments in the code to make sure you don't miss any.\n",
Zeynep Hakguder's avatar
pa1
Zeynep Hakguder committed
    "\n",
    "### Mandatory for 478 & 878:\n",
    "\n",
    "|   | Tasks                      | 478 | 878 |\n",
    "|---|----------------------------|-----|-----|\n",
    "| 1 | Implement `distance`       |  15 |  15 |\n",
    "| 2 | Implement `k-NN` methods   |  35 |  30 |\n",
Zeynep Hakguder's avatar
pa1
Zeynep Hakguder committed
    "| 3 | Model evaluation           |  25 |  20 |\n",
    "| 5 | ROC curve analysis         |  25 |  25 |\n",
Zeynep Hakguder's avatar
pa1
Zeynep Hakguder committed
    "\n",
    "### Mandatory for 878, bonus for 478\n",
    "\n",
    "|   | Tasks          | 478 | 878 |\n",
    "|---|----------------|-----|-----|\n",
    "| 4 | Optimizing *k* | 10  | 10  |\n",
Zeynep Hakguder's avatar
pa1
Zeynep Hakguder committed
    "\n",
    "### Bonus for 478/878\n",
    "\n",
    "|   | Tasks          | 478 | 878 |\n",
    "|---|----------------|-----|-----|\n",
    "| 6 | Assess suitability of *k*-NN | 10  | 10  |\n",
Zeynep Hakguder's avatar
pa1
Zeynep Hakguder committed
    "\n",
    "Points are broken down further below in Rubric sections. The **first** score is for 478, the **second** is for 878 students. There are a total of 100 points in this assignment and extra 20 bonus points for 478 students and 10 bonus points for 878 students."
   ]
  },
   {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# YOUR GRADE\n",
    "\n",
    "### Group Members:\n",
    "\n",
    "|   | Tasks                      | Points |\n",
    "|---|----------------------------|-----|\n",
    "| 1 | Implement `distance`       |     |\n",
    "| 2 | Implement `k-NN` methods   |     |\n",
    "| 3 | Model evaluation           |     |\n",
    "| 4 | Optimizing *k*             |     |\n",
    "| 5 | ROC curve analysis         |     |\n",
    "| 6 | Assess suitability of *k*-NN|    |\n",
Zeynep Hakguder's avatar
pa1
Zeynep Hakguder committed
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can use numpy for array operations and matplotlib for plotting for this assignment. Please do not add other libraries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Following code makes the Model class and relevant functions available from model.ipynb."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run 'model.ipynb'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TASK 1: Implement `distance` function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Choice of distance metric plays an important role in the performance of *k*-NN. Let's start with implementing a distance method  in the \"distance\" function in **model.ipynb**. It should take two data points and the name of the metric and return a scalar value."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Rubric:\n",
    "* Euclidean +7.5, +7.5\n",
    "* Manhattan +7.5, +7.5"
Zeynep Hakguder's avatar
pa1
Zeynep Hakguder committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Test `distance`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array(range(100))\n",
    "y = np.array(range(100, 200))\n",
    "dist_euclidean = distance(x, y, 'Euclidean')\n",
    "dist_manhattan = distance(x, y, 'Manhattan')\n",
    "print('Euclidean distance: {}, Manhattan distance: {}'.format(dist_euclidean, dist_manhattan))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Zeynep Hakguder's avatar
Zeynep Hakguder committed
    "## TASK 2: Implement *k*-NN Class Methods"
Zeynep Hakguder's avatar
pa1
Zeynep Hakguder committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can start implementing our *k*-NN classifier. *k*-NN class inherits Model class. Use the \"distance\" function you defined above. \"fit\" method takes *k* as an argument. \"predict\" takes as input an *mxd* array containing *d*-dimensional *m* feature vectors for examples and outputs the predicted class and the ratio of positive examples in *k* nearest neighbors."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Rubric:\n",
    "* correct implementation of fit method +5, +5\n",
    "* correct implementation of predict method +20, +15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class kNN(Model):\n",
    "    '''\n",
    "    Inherits Model class. Implements the k-NN algorithm for classification.\n",
    "    '''\n",
    "       \n",
    "    def fit(self, training_features, training_labels, classes, k, distance_f,**kwargs):\n",
    "        '''\n",
    "        Fit the model. This is pretty straightforward for k-NN.\n",
    "        Args:\n",
    "            training_features: ndarray\n",
    "            training_labels: ndarray\n",
    "            classes: ndarray\n",
    "                1D array containing unique classes in the dataset\n",
    "            k: int\n",
    "            distance_f: function\n",
    "            kwargs: dict\n",
    "                Contains keyword arguments that will be passed to distance_f\n",
    "        '''\n",
    "        # TODO\n",
    "        # set self.train_features, self.train_labels, self.classes, self.k, self.distance_f, self.distance_metric\n",
    "        \n",
    "        raise NotImplementedError\n",
    "\n",
    "        return\n",
    "    \n",
    "    \n",
    "    def predict(self, test_features):\n",
    "        '''\n",
    "        Args:\n",
    "            test_features: ndarray\n",
    "                mxd array containing features for the points to be predicted\n",
    "        Returns: \n",
    "            ndarray\n",
    "        '''\n",
    "        raise NotImplementedError\n",
    "        \n",
    "        pred = []\n",
    "        # TODO\n",
    "        \n",
    "        # for each point in test_features\n",
    "        # use your implementation of distance function\n",
    "        #  distance_f(..., distance_metric)\n",
    "        # to find the labels of k-nearest neighbors. \n",
    "\n",
    "        # you'll need proportion of the dominant class\n",
    "        # in k nearest neighbors\n",
    "        \n",
    "        return np.array(pred)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TASK 3: Build and Evaluate the Model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Rubric:\n",
    "* Reasonable accuracy values +10, +5\n",
    "* Reasonable confidence intervals on the error estimate +10, +10\n",
    "* Reasonable confusion matrix +5, +5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Preprocess the data files and partition the data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# initialize the model\n",
    "my_model = kNN()\n",
    "# obtain features and labels from files\n",
    "features, labels = preprocess(feature_file=..., label_file=...)\n",
    "# get class names (unique entries in labels)\n",
    "classes = np.unique(labels)\n",
    "# partition the data set\n",
    "val_indices, test_indices, train_indices = partition(size=..., t = 0.3, v = 0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Assign a value to *k* and fit the *k*-NN model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pass the training features and labels to the fit method\n",
    "kwargs_f = {'metric': 'Euclidean'}\n",
    "my_model.fit(training_features=..., training_labels-..., classes, k=10, distance_f=..., **kwargs_f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Computing the confusion matrix for *k* = 10\n",
    "Now that we have the true labels and the predicted ones from our model, we can build a confusion matrix and see how accurate our model is. Implement the \"conf_matrix\" function (in model.ipynb) that takes as input an array of true labels (*true*) and an array of predicted labels (*pred*). It should output a numpy.ndarray. You do not need to change the value of the threshold parameter yet."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# TODO\n",
    "\n",
    "# get model predictions\n",
    "pred_ratios = my_model.predict(features[test_indices])\n",
    "\n",
    "# For now, we will consider a data point as predicted in a class if more than 0.5 \n",
    "# of its k-neighbors are in that class.\n",
    "threshold = 0.5\n",
    "# convert predicted ratios to predicted labels\n",
    "pred_labels = None\n",
    "\n",
    "# show the distribution of predicted and true labels in a confusion matrix\n",
    "confusion = conf_matrix(...)\n",
    "confusion"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Evaluate your model on the test data and report your **accuracy**. Also, calculate and report the 95% confidence interval on the generalization **error** estimate."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# TODO\n",
    "# Calculate and report accuracy and generalization error with confidence interval here. Show your work in this cell.\n",
    "\n",
    "print('Accuracy: {}'.format(accuracy))\n",
    "print('Confidence interval: {}-{}'.format(lower_bound, upper_bound))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ## TASK 4: Plotting a learning curve\n",
    " \n",
    "A learning curve shows how error changes as the training set size increases. For more information, see [learning curves](https://www.dataquest.io/blog/learning-curves-machine-learning/).\n",
    "We'll plot the error values for training and validation data while varying the size of the training set. Report a good size for training set for which there is a good balance between bias and variance."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Rubric:\n",
    "* Correct training error calculation for different training set sizes +8, +8\n",
    "* Correct validation error calculation for different training set sizes +8, +8\n",
    "* Reasonable learning curve +4, +4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# train using %10, %20, %30, ..., 100% of training data\n",
    "training_proportions = np.arange(0.10, 1.01, 0.10)\n",
    "train_size = len(train_indices)\n",
    "training_sizes = np.int(np.ceil(train_size*proportion))\n",
    "\n",
    "# TODO\n",
    "error_train = []\n",
    "error_val = []\n",
    "\n",
    "# For each size in training_sizes\n",
    "for size in training_sizes:\n",
    "    # fit the model using \"size\" data point\n",
    "    # Calculate error for training and validation sets\n",
    "    # populate error_train and error_val arrays. \n",
    "    # Each entry in these arrays\n",
    "    # should correspond to each entry in training_sizes.\n",
    "\n",
    "# plot the learning curve\n",
    "plt.plot(training_sizes, error_train, 'r', label = 'training_error')\n",
    "plt.plot(training_sizes, error_val, 'g', label = 'validation_error')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TASK 5: Determining *k*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Rubric:\n",
    "* Accuracies reported with various *k* values +5, +5\n",
    "* Confusion matrices shown for various *k* values +5, +5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use the validation set to come up with a *k* value that results in better performance in terms of accuracy.\n",
    "\n",
    "Below calculate the accuracies for different values of *k* using the validation set. Report a good *k* value and use it in the analyses that follow this section. Report confusion matrix for the new value of *k*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# TODO\n",
    "\n",
    "# Change values of k. \n",
    "# Calculate accuracies for the validation set.\n",
    "# Report a good k value.\n",
    "# Calculate the confusion matrix for new k."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TASK 6: ROC curve analysis\n",
    "* Correct implementation +20, +20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Zeynep Hakguder's avatar
Zeynep Hakguder committed
    "ROC curves are a good way to visualize sensitivity vs. 1-specificity for varying cut off points. Now, implement, in **model.ipynb**, a \"ROC\" function. \"ROC\" takes a list containing different threshold values to try and returns two arrays; one where each entry is the sensitivity at a given threshold and the other where entries are 1-specificities."
Zeynep Hakguder's avatar
pa1
Zeynep Hakguder committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use the *k* value you found above, if you completed TASK 5, else use *k* = 10 to plot the ROC curve for values between 0.1 and 1.0."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# TODO\n",
    "# ROC curve\n",
    "roc_sens, roc_spec_ = ROC(true_labels=..., preds=..., np.arange(0.1, 1.0, 0.1))\n",
    "plt.plot(roc_sens, roc_spec_)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TASK 7: Assess suitability of *k*-NN to your dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use this cell to write about your understanding of why *k*-NN performed well if it did or why not if it didn't. What properties of the dataset could have affected the performance of the algorithm?"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}