diff --git a/ProgrammingAssignment_3/model.ipynb b/ProgrammingAssignment_3/model.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..078ca4288d1d0bb5eab481da5d04c7d56ebe0f6a --- /dev/null +++ b/ProgrammingAssignment_3/model.ipynb @@ -0,0 +1,114 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# JUPYTER NOTEBOOK TIPS\n", + "\n", + "Each rectangular box is called a cell. \n", + "* ctrl+ENTER evaluates the current cell; if it contains Python code, it runs the code, if it contains Markdown, it returns rendered text.\n", + "* alt+ENTER evaluates the current cell and adds a new cell below it.\n", + "* If you click to the left of a cell, you'll notice the frame changes color to blue. You can erase a cell by hitting 'dd' (that's two \"d\"s in a row) when the frame is blue." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Supervised Learning Model Skeleton\n", + "\n", + "We'll use this skeleton for implementing different supervised learning algorithms." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "class Model:\n", + " \n", + " def fit(self):\n", + " \n", + " raise NotImplementedError\n", + " \n", + " def predict(self, test_points):\n", + " raise NotImplementedError" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## General supervised learning performance related functions " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"conf_matrix\" function that takes as input an array of true labels (*true*) and an array of predicted labels (*pred*)." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def conf_matrix(true, pred):\n", + " '''\n", + " Args: \n", + " true: ndarray\n", + " nx1 array of true labels for test set\n", + " pred: ndarray \n", + " nx1 array of predicted labels for test set\n", + " Returns:\n", + " ndarray\n", + " '''\n", + " \n", + " tp = tn = fp = fn = 0\n", + " # calculate true positives (tp), true negatives(tn)\n", + " # false positives (fp) and false negatives (fn)\n", + " \n", + " size = len(true)\n", + " for i in range(size):\n", + " if true[i]==1:\n", + " if pred[i] == 1: \n", + " tp += 1\n", + " else: \n", + " fn += 1\n", + " else:\n", + " if pred[i] == 0:\n", + " tn += 1 \n", + " else:\n", + " fp += 1 \n", + " \n", + " # returns the confusion matrix as numpy.ndarray\n", + " return np.array([tp,fn, fp, tn])" + ] + } + ], + "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 +}