{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "%autosave 10" ], "language": "python", "metadata": {}, "outputs": [ { "javascript": [ "IPython.notebook.set_autosave_interval(10000)" ], "metadata": {}, "output_type": "display_data" }, { "output_type": "stream", "stream": "stdout", "text": [ "Autosaving every 10 seconds\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline\n", "from matplotlib import pyplot" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Linear Regression With One Variable" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Problem" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose you observe $m$ data points $(x^{(i)}, y^{(i)})$ and you develop the hypothesis that these data points were generated by the following model:\n", "\n", "$$h_{\\theta}(x) = <\\theta,x> = \\theta_0 + \\theta_1 x_1$$\n", "\n", "A mathematically tractable measure of how well your model reproduces the observed data is the following cost function\n", "\n", "$$J(\\theta) = \\frac{1}{2m} \\sum_{i=1}^m \\left( h_\\theta(x^{(i)}) - y^{(i)} \\right)^2$$\n", "\n", "We now wish to choose the *best* hypothesis from our pool of hypotheses $h_{\\theta}$ as measured by our cost function $J$.\n", "To do so we need to modify parameter values $\\theta$ so that $J$ assumes its global minimum.\n", "\n", "Given the fact that we chose $J$ as a quadratic function we know that there must be one unique minimum\n", "(this type of function has a special name ...)." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Gradient Descent" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One way to approximate the minimum of $J$ in $\\theta$-space numerically is to start in a random selected point in $\\theta$-space\n", "and then take small successive steps that always move us in the direction of greatest change (the gradient).\n", "\n", "The gradient of $J$ (with respect to $\\theta$) is\n", "\n", "\\begin{equation}\n", "\\nabla J = \\frac{1}{m} \\sum_{i=1}^m\n", "\\begin{pmatrix}\n", "\\left( h_{\\theta}(x^{(i)}) - y^{(i)} \\right) \\\\\n", "\\left( h_{\\theta}(x^{(i)}) - y^{(i)} \\right) x_1^{(i)}\n", "\\end{pmatrix}.\n", "\\end{equation}\n", "\n", "This now allows us to take small successive steps in the direction of $\\nabla J$ and continue doing so until $\\nabla J = (0, 0)$\n", "or is numerically close to the zero vector:\n", "\n", "$$\\theta^{k+1} = \\theta^k - \\alpha \\nabla J.$$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import csv" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "data = []\n", "with open('data/ex1data1.txt', 'r') as f:\n", " data_reader = csv.reader(f, delimiter=',')\n", " for row in data_reader:\n", " data.append((float(row[0]), float(row[1])))\n", "data = numpy.asarray(data)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "data[:5]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "array([[ 6.1101, 17.592 ],\n", " [ 5.5277, 9.1302],\n", " [ 8.5186, 13.662 ],\n", " [ 7.0032, 11.854 ],\n", " [ 5.8598, 6.8233]])" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "pyplot.scatter(data[:,0], data[:,1])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "" ] }, { "metadata": {}, "output_type": "display_data", "png": "iVBORw0KGgoAAAANSUhEUgAAAXMAAAD9CAYAAABOd5eOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XlcVPX6wPHPMAwwA6ICigqKaWaC4K6paVgSgZl75lI3\nl3Zz69deV25dtcxMbb+5tKCZllY3t8xC09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Xz5s1lUVWl0bBhQzlx4oS7wyh3a9eulc2bN0vz5s0Lto0aNUpee+01ERF57bXXZPTo\n0e4Kr1xd7lokJSXJq6++6sao3OPYsWOyY8cOERE5c+aMNGnSRLZu3VolvxtXuhZF/W6UyXD+TZs2\nERUVRVhYGN7e3gwcOJClS5eWRVWVilTBfvldunShZs2aF21btmwZd999NwBDhw6tMt+Ny10LqJrf\ni9DQUJo3bw5AQEAAMTExHDlypEp+N650LaBo340ySeaHDx+mfv36Be/Dw8M5fPhwWVRVaRgMhoI/\nH9944w13h+NW6enpBd1aQ0JCLnqYXhVV9fEav/32G6mpqdx4441V/rvx57Xo0qULULTvRpkkc+1b\nfqmNGzeyefNmVq9ezdy5c/nmm2/cHZKqAB555BH27dvHrl27aNy4MaNHj3Z3SOXKarXSv39/ZsyY\nQWBgoLvDcSur1cqAAQOYMWMG1apVK/J3o0ySeXh4OIcOHSp4f+jQoYvu1Kui2rVrA1CrVi369+9P\namqqmyNyn1q1apGRkQHk3aX/eW2qopCQEAwGAwaDgQceeKBKfS9yc3Pp168fQ4YMoXfv3kDV/W78\neS0GDx5ccC2K+t0ok2Terl07du7cyZEjR8jNzWXhwoUkJCSURVWVgs1mw2azAXD27FlWrFhRpfvl\nJyYmFowWTk5OJjEx0c0RuU9VHa8hIowYMYLIyEjGjRtXsL0qfjeudC2K/N0og4ezIiKybNkyiYqK\nkmbNmsmkSZPKqppKYf/+/RITEyMtWrSQJk2ayPPPP+/ukMrNXXfdJXXr1hWTySTh4eEyZ84cOXHi\nhHTv3l2io6MlLi5OsrKy3B1mufjfazF79mwZOnSoxMTEyPXXXy/x8fFy+PBhd4dZLr7//nsxGAzS\nokULadmypbRs2VKWL19eJb8bl7sWy5YtK/J3o1SG8yullHIvXWlIKaU8gCZzpZTyAJrMlVLKA2gy\nV0opD6DJXCmlPIAmc6WU8gD/D9aZp8rWxpfIAAAAAElFTkSuQmCC\n", "text": [ "" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "theta = numpy.zeros(shape=(2,))\n", "alpha = 0.001\n", "\n", "m = len(data[:,0])\n", "h = lambda theta, x: numpy.add(theta[0], theta[1]*x)\n", "J = lambda theta: (1./(2.*m))*numpy.sum(numpy.power(h(theta,data[:,0])-data[:,1], 2))\n", "\n", "feature_vector = numpy.column_stack((numpy.ones(shape=(m,)), data[:,0]))\n", "nabla_J = lambda theta: (1./m)*numpy.sum(numpy.column_stack((h(theta,data[:,0])-data[:,1],h(theta,data[:,0])-data[:,1]))*feature_vector)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "feature_vector[:5]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "array([[ 1. , 6.1101],\n", " [ 1. , 5.5277],\n", " [ 1. , 8.5186],\n", " [ 1. , 7.0032],\n", " [ 1. , 5.8598]])" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "print('Before GD, J = %g' % J(theta))\n", "for i in range(10000):\n", " theta = theta - alpha*nabla_J(theta)\n", "print('After GD, J = %g' % J(theta))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Before GD, J = 32.0727\n", "After GD, J = 6.42089" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "theta" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "array([ 0.72088159, 0.72088159])" ] } ], "prompt_number": 11 } ], "metadata": {} } ] }