text: {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Regression Week 3: Assessing Fit (polynomial regression)"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "In this notebook you will compare different regression models in order to assess which model fits best. We will be using polynomial regression as a means to examine this topic. In particular you will:\n",
      "* Write a function to take an SArray and a degree and return an SFrame where each column is the SArray to a polynomial value up to the total degree e.g. degree = 3 then column 1 is the SArray column 2 is the SArray squared and column 3 is the SArray cubed\n",
      "* Use matplotlib to visualize polynomial regressions\n",
      "* Use matplotlib to visualize the same polynomial degree on different subsets of the data\n",
      "* Use a validation set to select a polynomial degree\n",
      "* Assess the final fit using test data\n",
      "\n",
      "We will continue to use the House data from previous notebooks."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Fire up graphlab create"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": true
     },
     "outputs": [],
     "source": [
      "import graphlab"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Next we're going to write a polynomial function that takes an SArray and a maximal degree and returns an SFrame with columns containing the SArray to all the powers up to the maximal degree.\n",
      "\n",
      "The easiest way to apply a power to an SArray is to use the .apply() and lambda x: functions. \n",
      "For example to take the example array and compute the third power we can do as follows: (note running this cell the first time may take longer than expected since it loads graphlab)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "tmp = graphlab.SArray([1., 2., 3.])\n",
      "tmp_cubed = tmp.apply(lambda x: x**3)\n",
      "print tmp\n",
      "print tmp_cubed"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "We can create an empty SFrame using graphlab.SFrame() and then add any columns to it with ex_sframe['column_name'] = value. For example we create an empty SFrame and make the column 'power_1' to be the first power of tmp (i.e. tmp itself)."
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "ex_sframe = graphlab.SFrame()\n",
      "ex_sframe['power_1'] = tmp\n",
      "print ex_sframe"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Polynomial_sframe function"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Using the hints above complete the following function to create an SFrame consisting of the powers of an SArray up to a specific degree:"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": true
     },
     "outputs": [],
     "source": [
      "def polynomial_sframe(feature, degree):\n",
      "    # assume that degree >= 1\n",
      "    # initialize the SFrame:\n",
      "    poly_sframe = graphlab.SFrame()\n",
      "    # and set poly_sframe['power_1'] equal to the passed feature\n",
      "\n",
      "    # first check if degree > 1\n",
      "    if degree > 1:\n",
      "        # then loop over the remaining degrees:\n",
      "        # range usually starts at 0 and stops at the endpoint-1. We want it to start at 2 and stop at degree\n",
      "        for power in range(2, degree+1): \n",
      "            # first we'll give the column a name:\n",
      "            name = 'power_' + str(power)\n",
      "            # then assign poly_sframe[name] to the appropriate power of feature\n",
      "\n",
      "    return poly_sframe"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "To test your function consider the smaller tmp variable and what you would expect the outcome of the following call:"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "print polynomial_sframe(tmp, 3)"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Visualizing polynomial regression"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Let's use matplotlib to visualize what a polynomial regression looks like on some real data."
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": true
     },
     "outputs": [],
     "source": [
      "sales = graphlab.SFrame('kc_house_data.gl/')"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "As in Week 3, we will use the sqft_living variable. For plotting purposes (connecting the dots), you'll need to sort by the values of sqft_living. For houses with identical square footage, we break the tie by their prices."
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "sales = sales.sort(['sqft_living', 'price'])"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Let's start with a degree 1 polynomial using 'sqft_living' (i.e. a line) to predict 'price' and plot what it looks like."
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "poly1_data = polynomial_sframe(sales['sqft_living'], 1)\n",
      "poly1_data['price'] = sales['price'] # add price to the data since it's the target"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "NOTE: for all the models in this notebook use validation_set = None to ensure that all results are consistent across users."
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "model1 = graphlab.linear_regression.create(poly1_data, target = 'price', features = ['power_1'], validation_set = None)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "#let's take a look at the weights before we plot\n",
      "model1.get(\"coefficients\")"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": true
     },
     "outputs": [],
     "source": [
      "import matplotlib.pyplot as plt\n",
      "%matplotlib inline"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "plt.plot(poly1_data['power_1'],poly1_data['price'],'.',\n",
      "        poly1_data['power_1'], model1.predict(poly1_data),'-')"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Let's unpack that plt.plot() command. The first pair of SArrays we passed are the 1st power of sqft and the actual price we then ask it to print these as dots '.'. The next pair we pass is the 1st power of sqft and the predicted values from the linear model. We ask these to be plotted as a line '-'. \n",
      "\n",
      "We can see, not surprisingly, that the predicted values all fall on a line, specifically the one with slope 280 and intercept -43579. What if we wanted to plot a second degree polynomial?"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "poly2_data = polynomial_sframe(sales['sqft_living'], 2)\n",
      "my_features = poly2_data.column_names() # get the name of the features\n",
      "poly2_data['price'] = sales['price'] # add price to the data since it's the target\n",
      "model2 = graphlab.linear_regression.create(poly2_data, target = 'price', features = my_features, validation_set = None)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "model2.get(\"coefficients\")"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "plt.plot(poly2_data['power_1'],poly2_data['price'],'.',\n",
      "        poly2_data['power_1'], model2.predict(poly2_data),'-')"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The resulting model looks like half a parabola. Try on your own to see what the cubic looks like:"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": []
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Now try a 15th degree polynomial:"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": []
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "What do you think of the 15th degree polynomial? Do you think this is appropriate? If we were to change the data do you think you'd get pretty much the same curve? Let's take a look."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Changing the data and re-learning"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "We're going to split the sales data into four subsets of roughly equal size. Then you will estimate a 15th degree polynomial model on all four subsets of the data. Print the coefficients (you should use .print_rows(num_rows = 16) to view all of them) and plot the resulting fit (as we did above). The quiz will ask you some questions about these results.\n",
      "\n",
      "To split the sales data into four subsets, we perform the following steps:\n",
      "* First split sales into 2 subsets with `.random_split(0.5, seed=0)`. \n",
      "* Next split the resulting subsets into 2 more subsets each. Use `.random_split(0.5, seed=0)`.\n",
      "\n",
      "We set `seed=0` in these steps so that different users get consistent results.\n",
      "You should end up with 4 subsets (`set_1`, `set_2`, `set_3`, `set_4`) of approximately equal size. "
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": true
     },
     "outputs": [],
     "source": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Fit a 15th degree polynomial on set_1, set_2, set_3, and set_4 using sqft_living to predict prices. Print the coefficients and make a plot of the resulting model."
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": []
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": []
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": []
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Some questions you will be asked on your quiz:\n",
      "\n",
      "**Quiz Question: Is the sign (positive or negative) for power_15 the same in all four models?**\n",
      "\n",
      "**Quiz Question: (True/False) the plotted fitted lines look the same in all four plots**"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Selecting a Polynomial Degree"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Whenever we have a \"magic\" parameter like the degree of the polynomial there is one well-known way to select these parameters: validation set. (We will explore another approach in week 4).\n",
      "\n",
      "We split the sales dataset 3-way into training set, test set, and validation set as follows:\n",
      "\n",
      "* Split our sales data into 2 sets: `training_and_validation` and `testing`. Use `random_split(0.9, seed=1)`.\n",
      "* Further split our training data into two sets: `training` and `validation`. Use `random_split(0.5, seed=1)`.\n",
      "\n",
      "Again, we set `seed=1` to obtain consistent results for different users."
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": true
     },
     "outputs": [],
     "source": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Next you should write a loop that does the following:\n",
      "* For degree in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] (to get this in python type range(1, 15+1))\n",
      "    * Build an SFrame of polynomial data of train_data['sqft_living'] at the current degree\n",
      "    * hint: my_features = poly_data.column_names() gives you a list e.g. ['power_1', 'power_2', 'power_3'] which you might find useful for graphlab.linear_regression.create( features = my_features)\n",
      "    * Add train_data['price'] to the polynomial SFrame\n",
      "    * Learn a polynomial regression model to sqft vs price with that degree on TRAIN data\n",
      "    * Compute the RSS on VALIDATION data (here you will want to use .predict()) for that degree and you will need to make a polynmial SFrame using validation data.\n",
      "* Report which degree had the lowest RSS on validation data (remember python indexes from 0)\n",
      "\n",
      "(Note you can turn off the print out of linear_regression.create() with verbose = False)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "**Quiz Question: Which degree (1, 2, …, 15) had the lowest RSS on Validation data?**"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Now that you have chosen the degree of your polynomial using validation data, compute the RSS of this model on TEST data. Report the RSS on your quiz."
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "**Quiz Question: what is the RSS on TEST data for the model with the degree selected from Validation data?**"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": true
     },
     "outputs": [],
     "source": []
    }
   ],
   "metadata": {
    "kernelspec": {
     "display_name": "Python 2",
     "language": "python",
     "name": "python2"
    },
    "language_info": {
     "codemirror_mode": {
      "name": "ipython",
      "version": 2
     },
     "file_extension": ".py",
     "mimetype": "text/x-python",
     "name": "python",
     "nbconvert_exporter": "python",
     "pygments_lexer": "ipython2",
     "version": "2.7.11"
    }
   },
   "nbformat": 4,
   "nbformat_minor": 0
  }
  