{ "metadata": { "name": "WA3. Linear Regression - Analysis Worksheet" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import pylab as pl\n", "import numpy as np\n", "#from sklearn import datasets, linear_model\n", "import pandas as pd\n", "import statsmodels.api as sm\n", "\n", "# import the cleaned up dataset\n", "df = pd.read_csv('../datasets/loanf.csv')\n", "\n", "intrate = df['Interest.Rate']\n", "loanamt = df['Loan.Amount']\n", "fico = df['FICO.Score']\n", "\n", "# reshape the data from a pandas Series to columns \n", "# the dependent variable\n", "y = np.matrix(intrate).transpose()\n", "# the independent variables shaped as columns\n", "x1 = np.matrix(fico).transpose()\n", "x2 = np.matrix(loanamt).transpose()\n", "\n", "# put the two columns together to create an input matrix \n", "# if we had n independent variables we would have n columns here\n", "x = np.column_stack([x1,x2])\n", "\n", "# create a linear model and fit it to the data\n", "X = sm.add_constant(x)\n", "model = sm.OLS(y,X)\n", "f = model.fit()\n", "\n", "print 'Coefficients: ', f.params[0:2]\n", "print 'Intercept: ', f.params[2]\n", "print 'P-Values: ', f.pvalues\n", "print 'R-Squared: ', f.rsquared\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Coefficients: [-0.08844242 0.00021075]\n", "Intercept: 72.8827983168\n", "P-Values: [ 0.00000000e+000 5.96972978e-203 0.00000000e+000]\n", "R-Squared: 0.656632624649\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.core.display import HTML\n", "def css_styling():\n", " styles = open(\"../styles/custom.css\", \"r\").read()\n", " return HTML(styles)\n", "css_styling()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "" ], "output_type": "pyout", "prompt_number": 1, "text": [ "" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }