{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Supervised Learning Part 2 -- Regression Analysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In regression we are trying to predict a continuous output variable -- in contrast to the nominal variables we were predicting in the previous classification examples. \n", "\n", "Let's start with a simple toy example with one feature dimension (explanatory variable) and one target variable. We will create a dataset out of a sine curve with some noise:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.linspace(-3, 3, 100)\n", "print(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rng = np.random.RandomState(42)\n", "y = np.sin(4 * x) + x + rng.uniform(size=len(x))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(x, y, 'o');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Linear Regression\n", "=================\n", "\n", "The first model that we will introduce is the so-called simple linear regression. Here, we want to fit a line to the data, which \n", "\n", "One of the simplest models again is a linear one, that simply tries to predict the data as lying on a line. One way to find such a line is `LinearRegression` (also known as [*Ordinary Least Squares (OLS)*](https://en.wikipedia.org/wiki/Ordinary_least_squares) regression).\n", "The interface for LinearRegression is exactly the same as for the classifiers before, only that ``y`` now contains float values, instead of classes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we remember, the scikit-learn API requires us to provide the target variable (`y`) as a 1-dimensional array; scikit-learn's API expects the samples (`X`) in form a 2-dimensional array -- even though it may only consist of 1 feature. Thus, let us convert the 1-dimensional `x` NumPy array into an `X` array with 2 axes:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('Before: ', x.shape)\n", "X = x[:, np.newaxis]\n", "print('After: ', X.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again, we start by splitting our dataset into a training (75%) and a test set (25%):" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we use the learning algorithm implemented in `LinearRegression` to **fit a regression model to the training data**:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.linear_model import LinearRegression\n", "\n", "regressor = LinearRegression()\n", "regressor.fit(X_train, y_train)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After fitting to the training data, we paramerterized a linear regression model with the following values." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('Weight coefficients: ', regressor.coef_)\n", "print('y-axis intercept: ', regressor.intercept_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since our regression model is a linear one, the relationship between the target variable (y) and the feature variable (x) is defined as \n", "\n", "$$y = \\text{weight} \\times x + \\text{intercept .}$$\n", "\n", "Plugging in the min and max values into thos equation, we can plot the regression fit to our training data:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "min_pt = X.min() * regressor.coef_[0] + regressor.intercept_\n", "max_pt = X.max() * regressor.coef_[0] + regressor.intercept_\n", "\n", "plt.plot([X.min(), X.max()], [min_pt, max_pt])\n", "plt.plot(X_train, y_train, 'o');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similar to the estimators for classification in the previous notebook, we use the `predict` method to predict the target variable. And we expect these predicted values to fall onto the line that we plotted previously:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y_pred_train = regressor.predict(X_train)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(X_train, y_train, 'o', label=\"data\")\n", "plt.plot(X_train, y_pred_train, 'o', label=\"prediction\")\n", "plt.plot([X.min(), X.max()], [min_pt, max_pt], label='fit')\n", "plt.legend(loc='best')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we can see in the plot above, the line is able to capture the general slope of the data, but not many details." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, let's try the test set:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y_pred_test = regressor.predict(X_test)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(X_test, y_test, 'o', label=\"data\")\n", "plt.plot(X_test, y_pred_test, 'o', label=\"prediction\")\n", "plt.plot([X.min(), X.max()], [min_pt, max_pt], label='fit')\n", "plt.legend(loc='best');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again, scikit-learn provides an easy way to evaluate the prediction quantitatively using the ``score`` method. For regression tasks, this is the R2 score. Another popular way would be the Mean Squared Error (MSE). As its name implies, the MSE is simply the average squared difference over the predicted and actual target values\n", "\n", "$$MSE = \\frac{1}{n} \\sum_{i=1}^{n} (\\text{predicted}_i - \\text{true}_i)^2$$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "regressor.score(X_test, y_test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " EXERCISE:\n", " \n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# %load solutions/06B_lin_with_sine.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "KNeighborsRegression\n", "=======================\n", "As for classification, we can also use a neighbor based method for regression. We can simply take the output of the nearest point, or we could average several nearest points. This method is less popular for regression than for classification, but still a good baseline." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.neighbors import KNeighborsRegressor\n", "kneighbor_regression = KNeighborsRegressor(n_neighbors=1)\n", "kneighbor_regression.fit(X_train, y_train)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again, let us look at the behavior on training and test set:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y_pred_train = kneighbor_regression.predict(X_train)\n", "\n", "plt.plot(X_train, y_train, 'o', label=\"data\", markersize=10)\n", "plt.plot(X_train, y_pred_train, 's', label=\"prediction\", markersize=4)\n", "plt.legend(loc='best');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On the training set, we do a perfect job: each point is its own nearest neighbor!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y_pred_test = kneighbor_regression.predict(X_test)\n", "\n", "plt.plot(X_test, y_test, 'o', label=\"data\", markersize=8)\n", "plt.plot(X_test, y_pred_test, 's', label=\"prediction\", markersize=4)\n", "plt.legend(loc='best');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On the test set, we also do a better job of capturing the variation, but our estimates look much messier than before.\n", "Let us look at the R2 score:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "kneighbor_regression.score(X_test, y_test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Much better than before! Here, the linear model was not a good fit for our problem; it was lacking in complexity and thus under-fit our data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " EXERCISE:\n", " \n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# %load solutions/06A_knn_vs_linreg.py" ] } ], "metadata": { "anaconda-cloud": {}, "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 }