Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeTange/f3977ca5a8a2948813663a4adb9bcd35 to your computer and use it in GitHub Desktop.
Save MikeTange/f3977ca5a8a2948813663a4adb9bcd35 to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <a href=\"https://cocl.us/DA0101EN_edx_link_Notebook_link_top\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/Images/TopAd.png\" width=\"750\" align=\"center\">\n",
" </a>\n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://www.bigdatauniversity.com\"><img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/Images/CCLog.png\" width=300, align=\"center\"></a>\n",
"\n",
"<h1 align=center><font size=5>Data Analysis with Python</font></h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Module 5: Model Evaluation and Refinement</h1>\n",
"\n",
"We have built models and made predictions of vehicle prices. Now we will determine how accurate these predictions are. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Table of content</h1>\n",
"<ul>\n",
" <li><a href=\"#ref1\">Model Evaluation </a></li>\n",
" <li><a href=\"#ref2\">Over-fitting, Under-fitting and Model Selection </a></li>\n",
" <li><a href=\"#ref3\">Ridge Regression </a></li>\n",
" <li><a href=\"#ref4\">Grid Search</a></li>\n",
"</ul>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This dataset was hosted on IBM Cloud object click <a href=\"https://cocl.us/edx_DA0101EN_objectstorage\">HERE</a> for free storage."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"# Import clean data \n",
"path = 'https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/module_5_auto.csv'\n",
"df = pd.read_csv(path)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.to_csv('module_5_auto.csv')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" First lets only use numeric data "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [],
"source": [
"df=df._get_numeric_data()\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Libraries for plotting "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%capture\n",
"! pip install ipywidgets"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from IPython.display import display\n",
"from IPython.html import widgets \n",
"from IPython.display import display\n",
"from ipywidgets import interact, interactive, fixed, interact_manual"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Functions for plotting</h2>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def DistributionPlot(RedFunction, BlueFunction, RedName, BlueName, Title):\n",
" width = 12\n",
" height = 10\n",
" plt.figure(figsize=(width, height))\n",
"\n",
" ax1 = sns.distplot(RedFunction, hist=False, color=\"r\", label=RedName)\n",
" ax2 = sns.distplot(BlueFunction, hist=False, color=\"b\", label=BlueName, ax=ax1)\n",
"\n",
" plt.title(Title)\n",
" plt.xlabel('Price (in dollars)')\n",
" plt.ylabel('Proportion of Cars')\n",
"\n",
" plt.show()\n",
" plt.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def PollyPlot(xtrain, xtest, y_train, y_test, lr,poly_transform):\n",
" width = 12\n",
" height = 10\n",
" plt.figure(figsize=(width, height))\n",
" \n",
" \n",
" #training data \n",
" #testing data \n",
" # lr: linear regression object \n",
" #poly_transform: polynomial transformation object \n",
" \n",
" xmax=max([xtrain.values.max(), xtest.values.max()])\n",
"\n",
" xmin=min([xtrain.values.min(), xtest.values.min()])\n",
"\n",
" x=np.arange(xmin, xmax, 0.1)\n",
"\n",
"\n",
" plt.plot(xtrain, y_train, 'ro', label='Training Data')\n",
" plt.plot(xtest, y_test, 'go', label='Test Data')\n",
" plt.plot(x, lr.predict(poly_transform.fit_transform(x.reshape(-1, 1))), label='Predicted Function')\n",
" plt.ylim([-10000, 60000])\n",
" plt.ylabel('Price')\n",
" plt.legend()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 id=\"ref1\">Part 1: Training and Testing</h1>\n",
"\n",
"<p>An important step in testing your model is to split your data into training and testing data. We will place the target data <b>price</b> in a separate dataframe <b>y</b>:</p>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"y_data = df['price']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"drop price data in x data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x_data=df.drop('price',axis=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we randomly split our data into training and testing data using the function <b>train_test_split</b>. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"\n",
"\n",
"x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.15, random_state=1)\n",
"\n",
"\n",
"print(\"number of test samples :\", x_test.shape[0])\n",
"print(\"number of training samples:\",x_train.shape[0])\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The <b>test_size</b> parameter sets the proportion of data that is split into the testing set. In the above, the testing set is set to 10% of the total dataset. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #1):</h1>\n",
"\n",
"<b>Use the function \"train_test_split\" to split up the data set such that 40% of the data samples will be utilized for testing, set the parameter \"random_state\" equal to zero. The output of the function should be the following: \"x_train_1\" , \"x_test_1\", \"y_train_1\" and \"y_test_1\".</b>\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"x_train1, x_test1, y_train1, y_test1 = train_test_split(x_data, y_data, test_size=0.4, random_state=0) \n",
"print(\"number of test samples :\", x_test1.shape[0])\n",
"print(\"number of training samples:\",x_train1.shape[0])\n",
"\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's import <b>LinearRegression</b> from the module <b>linear_model</b>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from sklearn.linear_model import LinearRegression"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We create a Linear Regression object:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"lre=LinearRegression()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"we fit the model using the feature horsepower "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"lre.fit(x_train[['horsepower']], y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's Calculate the R^2 on the test data:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"lre.score(x_test[['horsepower']], y_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"we can see the R^2 is much smaller using the test data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"lre.score(x_train[['horsepower']], y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #2): </h1>\n",
"<b> \n",
"Find the R^2 on the test data using 90% of the data for training data\n",
"</b>\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"x_train1, x_test1, y_train1, y_test1 = train_test_split(x_data, y_data, test_size=0.1, random_state=0)\n",
"lre.fit(x_train1[['horsepower']],y_train1)\n",
"lre.score(x_test1[['horsepower']],y_test1)\n",
"\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Sometimes you do not have sufficient testing data; as a result, you may want to perform Cross-validation. Let's go over several methods that you can use for Cross-validation. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Cross-validation Score</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets import <b>model_selection</b> from the module <b>cross_val_score</b>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from sklearn.model_selection import cross_val_score"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We input the object, the feature in this case ' horsepower', the target data (y_data). The parameter 'cv' determines the number of folds; in this case 4. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Rcross = cross_val_score(lre, x_data[['horsepower']], y_data, cv=4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The default scoring is R^2; each element in the array has the average R^2 value in the fold:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Rcross"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can calculate the average and standard deviation of our estimate:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(\"The mean of the folds are\", Rcross.mean(), \"and the standard deviation is\" , Rcross.std())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use negative squared error as a score by setting the parameter 'scoring' metric to 'neg_mean_squared_error'. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"-1 * cross_val_score(lre,x_data[['horsepower']], y_data,cv=4,scoring='neg_mean_squared_error')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #3): </h1>\n",
"<b> \n",
"Calculate the average R^2 using two folds, find the average R^2 for the second fold utilizing the horsepower as a feature : \n",
"</b>\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"Rc=cross_val_score(lre,x_data[['horsepower']], y_data,cv=2)\n",
"Rc.mean()\n",
"\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also use the function 'cross_val_predict' to predict the output. The function splits up the data into the specified number of folds, using one fold to get a prediction while the rest of the folds are used as test data. First import the function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from sklearn.model_selection import cross_val_predict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We input the object, the feature in this case <b>'horsepower'</b> , the target data <b>y_data</b>. The parameter 'cv' determines the number of folds; in this case 4. We can produce an output:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"yhat = cross_val_predict(lre,x_data[['horsepower']], y_data,cv=4)\n",
"yhat[0:5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 id=\"ref2\">Part 2: Overfitting, Underfitting and Model Selection</h1>\n",
"\n",
"<p>It turns out that the test data sometimes referred to as the out of sample data is a much better measure of how well your model performs in the real world. One reason for this is overfitting; let's go over some examples. It turns out these differences are more apparent in Multiple Linear Regression and Polynomial Regression so we will explore overfitting in that context.</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's create Multiple linear regression objects and train the model using <b>'horsepower'</b>, <b>'curb-weight'</b>, <b>'engine-size'</b> and <b>'highway-mpg'</b> as features."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"lr = LinearRegression()\n",
"lr.fit(x_train[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg']], y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Prediction using training data:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"yhat_train = lr.predict(x_train[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg']])\n",
"yhat_train[0:5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Prediction using test data: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"yhat_test = lr.predict(x_test[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg']])\n",
"yhat_test[0:5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's perform some model evaluation using our training and testing data separately. First we import the seaborn and matplotlibb library for plotting."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"import seaborn as sns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's examine the distribution of the predicted values of the training data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Title = 'Distribution Plot of Predicted Value Using Training Data vs Training Data Distribution'\n",
"DistributionPlot(y_train, yhat_train, \"Actual Values (Train)\", \"Predicted Values (Train)\", Title)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Figure 1: Plot of predicted values using the training data compared to the training data. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So far the model seems to be doing well in learning from the training dataset. But what happens when the model encounters new data from the testing dataset? When the model generates new values from the test data, we see the distribution of the predicted values is much different from the actual target values. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Title='Distribution Plot of Predicted Value Using Test Data vs Data Distribution of Test Data'\n",
"DistributionPlot(y_test,yhat_test,\"Actual Values (Test)\",\"Predicted Values (Test)\",Title)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Figur 2: Plot of predicted value using the test data compared to the test data. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>Comparing Figure 1 and Figure 2; it is evident the distribution of the test data in Figure 1 is much better at fitting the data. This difference in Figure 2 is apparent where the ranges are from 5000 to 15 000. This is where the distribution shape is exceptionally different. Let's see if polynomial regression also exhibits a drop in the prediction accuracy when analysing the test dataset.</p>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from sklearn.preprocessing import PolynomialFeatures"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Overfitting</h4>\n",
"<p>Overfitting occurs when the model fits the noise, not the underlying process. Therefore when testing your model using the test-set, your model does not perform as well as it is modelling noise, not the underlying process that generated the relationship. Let's create a degree 5 polynomial model.</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's use 55 percent of the data for testing and the rest for training:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.45, random_state=0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will perform a degree 5 polynomial transformation on the feature <b>'horse power'</b>. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"pr = PolynomialFeatures(degree=5)\n",
"x_train_pr = pr.fit_transform(x_train[['horsepower']])\n",
"x_test_pr = pr.fit_transform(x_test[['horsepower']])\n",
"pr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's create a linear regression model \"poly\" and train it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"poly = LinearRegression()\n",
"poly.fit(x_train_pr, y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see the output of our model using the method \"predict.\" then assign the values to \"yhat\"."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"yhat = poly.predict(x_test_pr)\n",
"yhat[0:5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's take the first five predicted values and compare it to the actual targets. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(\"Predicted values:\", yhat[0:4])\n",
"print(\"True values:\", y_test[0:4].values)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will use the function \"PollyPlot\" that we defined at the beginning of the lab to display the training data, testing data, and the predicted function."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [],
"source": [
"PollyPlot(x_train[['horsepower']], x_test[['horsepower']], y_train, y_test, poly,pr)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Figur 4 A polynomial regression model, red dots represent training data, green dots represent test data, and the blue line represents the model prediction. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We see that the estimated function appears to track the data but around 200 horsepower, the function begins to diverge from the data points. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" R^2 of the training data:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"poly.score(x_train_pr, y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" R^2 of the test data:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"poly.score(x_test_pr, y_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We see the R^2 for the training data is 0.5567 while the R^2 on the test data was -29.87. The lower the R^2, the worse the model, a Negative R^2 is a sign of overfitting."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see how the R^2 changes on the test data for different order polynomials and plot the results:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Rsqu_test = []\n",
"\n",
"order = [1, 2, 3, 4]\n",
"for n in order:\n",
" pr = PolynomialFeatures(degree=n)\n",
" \n",
" x_train_pr = pr.fit_transform(x_train[['horsepower']])\n",
" \n",
" x_test_pr = pr.fit_transform(x_test[['horsepower']]) \n",
" \n",
" lr.fit(x_train_pr, y_train)\n",
" \n",
" Rsqu_test.append(lr.score(x_test_pr, y_test))\n",
"\n",
"plt.plot(order, Rsqu_test)\n",
"plt.xlabel('order')\n",
"plt.ylabel('R^2')\n",
"plt.title('R^2 Using Test Data')\n",
"plt.text(3, 0.75, 'Maximum R^2 ') "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We see the R^2 gradually increases until an order three polynomial is used. Then the R^2 dramatically decreases at four."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following function will be used in the next section; please run the cell."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def f(order, test_data):\n",
" x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=test_data, random_state=0)\n",
" pr = PolynomialFeatures(degree=order)\n",
" x_train_pr = pr.fit_transform(x_train[['horsepower']])\n",
" x_test_pr = pr.fit_transform(x_test[['horsepower']])\n",
" poly = LinearRegression()\n",
" poly.fit(x_train_pr,y_train)\n",
" PollyPlot(x_train[['horsepower']], x_test[['horsepower']], y_train,y_test, poly, pr)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following interface allows you to experiment with different polynomial orders and different amounts of data. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"interact(f, order=(0, 6, 1), test_data=(0.05, 0.95, 0.05))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #4a):</h1>\n",
"\n",
"<b>We can perform polynomial transformations with more than one feature. Create a \"PolynomialFeatures\" object \"pr1\" of degree two?</b>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"pr1=PolynomialFeatures(degree=2)\n",
"\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #4b): </h1>\n",
"\n",
"<b> \n",
" Transform the training and testing samples for the features 'horsepower', 'curb-weight', 'engine-size' and 'highway-mpg'. Hint: use the method \"fit_transform\" \n",
"?</b>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"x_train_pr1=pr.fit_transform(x_train[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg']])\n",
"\n",
"x_test_pr1=pr.fit_transform(x_test[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg']])\n",
"\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- The answer is below:\n",
"\n",
"x_train_pr1=pr.fit_transform(x_train[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg']])\n",
"x_test_pr1=pr.fit_transform(x_test[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg']])\n",
"\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #4c): </h1>\n",
"<b> \n",
"How many dimensions does the new feature have? Hint: use the attribute \"shape\"\n",
"</b>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"There are now 15 features: x_train_pr1.shape \n",
"\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #4d): </h1>\n",
"\n",
"<b> \n",
"Create a linear regression model \"poly1\" and train the object using the method \"fit\" using the polynomial features?</b>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"poly1=linear_model.LinearRegression().fit(x_train_pr1,y_train)\n",
"\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #4e): </h1>\n",
"<b>Use the method \"predict\" to predict an output on the polynomial features, then use the function \"DistributionPlot\" to display the distribution of the predicted output vs the test data?</b>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"yhat_test1=poly1.predict(x_test_pr1)\n",
"Title='Distribution Plot of Predicted Value Using Test Data vs Data Distribution of Test Data'\n",
"DistributionPlot(y_test, yhat_test1, \"Actual Values (Test)\", \"Predicted Values (Test)\", Title)\n",
"\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #4f): </h1>\n",
"\n",
"<b>Use the distribution plot to determine the two regions were the predicted prices are less accurate than the actual prices.</b>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"The predicted value is lower than actual value for cars where the price $ 10,000 range, conversely the predicted price is larger than the price cost in the $30, 000 to $40,000 range. As such the model is not as accurate in these ranges .\n",
" \n",
"-->\n",
"\n",
"<img src = \"https://ibm.box.com/shared/static/c35ipv9zeanu7ynsnppb8gjo2re5ugeg.png\" width = 700, align = \"center\">\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"ref3\">Part 3: Ridge regression</h2> "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" In this section, we will review Ridge Regression we will see how the parameter Alfa changes the model. Just a note here our test data will be used as validation data."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Let's perform a degree two polynomial transformation on our data. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"pr=PolynomialFeatures(degree=2)\n",
"x_train_pr=pr.fit_transform(x_train[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg','normalized-losses','symboling']])\n",
"x_test_pr=pr.fit_transform(x_test[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg','normalized-losses','symboling']])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Let's import <b>Ridge</b> from the module <b>linear models</b>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from sklearn.linear_model import Ridge"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's create a Ridge regression object, setting the regularization parameter to 0.1 "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"RigeModel=Ridge(alpha=0.1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Like regular regression, you can fit the model using the method <b>fit</b>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"RigeModel.fit(x_train_pr, y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Similarly, you can obtain a prediction: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"yhat = RigeModel.predict(x_test_pr)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's compare the first five predicted samples to our test set "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('predicted:', yhat[0:4])\n",
"print('test set :', y_test[0:4].values)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We select the value of Alfa that minimizes the test error, for example, we can use a for loop. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Rsqu_test = []\n",
"Rsqu_train = []\n",
"dummy1 = []\n",
"ALFA = 10 * np.array(range(0,1000))\n",
"for alfa in ALFA:\n",
" RigeModel = Ridge(alpha=alfa) \n",
" RigeModel.fit(x_train_pr, y_train)\n",
" Rsqu_test.append(RigeModel.score(x_test_pr, y_test))\n",
" Rsqu_train.append(RigeModel.score(x_train_pr, y_train))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can plot out the value of R^2 for different Alphas "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"width = 12\n",
"height = 10\n",
"plt.figure(figsize=(width, height))\n",
"\n",
"plt.plot(ALFA,Rsqu_test, label='validation data ')\n",
"plt.plot(ALFA,Rsqu_train, 'r', label='training Data ')\n",
"plt.xlabel('alpha')\n",
"plt.ylabel('R^2')\n",
"plt.legend()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Figure 6:The blue line represents the R^2 of the test data, and the red line represents the R^2 of the training data. The x-axis represents the different values of Alfa "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The red line in figure 6 represents the R^2 of the test data, as Alpha increases the R^2 decreases; therefore as Alfa increases the model performs worse on the test data. The blue line represents the R^2 on the validation data, as the value for Alfa increases the R^2 decreases. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #5): </h1>\n",
"\n",
"Perform Ridge regression and calculate the R^2 using the polynomial features, use the training data to train the model and test data to test the model. The parameter alpha should be set to 10.\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"RigeModel = Ridge(alpha=0) \n",
"RigeModel.fit(x_train_pr, y_train)\n",
"RigeModel.score(x_test_pr, y_test)\n",
"\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"ref4\">Part 4: Grid Search</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The term Alfa is a hyperparameter, sklearn has the class <b>GridSearchCV</b> to make the process of finding the best hyperparameter simpler."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's import <b>GridSearchCV</b> from the module <b>model_selection</b>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from sklearn.model_selection import GridSearchCV"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We create a dictionary of parameter values:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"parameters1= [{'alpha': [0.001,0.1,1, 10, 100, 1000, 10000, 100000, 100000]}]\n",
"parameters1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a ridge regions object:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"RR=Ridge()\n",
"RR"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a ridge grid search object "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Grid1 = GridSearchCV(RR, parameters1,cv=4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fit the model "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Grid1.fit(x_data[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg']], y_data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The object finds the best parameter values on the validation data. We can obtain the estimator with the best parameters and assign it to the variable BestRR as follows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"BestRR=Grid1.best_estimator_\n",
"BestRR"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We now test our model on the test data "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"BestRR.score(x_test[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg']], y_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #6): </h1>\n",
"Perform a grid search for the alpha parameter and the normalization parameter, then find the best values of the parameters\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"parameters2= [{'alpha': [0.001,0.1,1, 10, 100, 1000,10000,100000,100000],'normalize':[True,False]} ]\n",
"Grid2 = GridSearchCV(Ridge(), parameters2,cv=4)\n",
"Grid2.fit(x_data[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg']],y_data)\n",
"Grid2.best_estimator_\n",
"\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Thank you for completing this notebook!</h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"\n",
" <p><a href=\"https://cocl.us/DA0101EN_edx_link_Notebook_bottom\"><img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/Images/BottomAd.png\" width=\"750\" align=\"center\"></a></p>\n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>About the Authors:</h3>\n",
"\n",
"This notebook was written by <a href=\"https://www.linkedin.com/in/mahdi-noorian-58219234/\" target=\"_blank\">Mahdi Noorian PhD</a>, <a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a>, Bahare Talayian, Eric Xiao, Steven Dong, Parizad, Hima Vsudevan and <a href=\"https://www.linkedin.com/in/fiorellawever/\" target=\"_blank\">Fiorella Wenver</a> and <a href=\" https://www.linkedin.com/in/yi-leng-yao-84451275/ \" target=\"_blank\" >Yi Yao</a>.\n",
"\n",
"<p><a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a> is a Data Scientist at IBM, and holds a PhD in Electrical Engineering. His research focused on using Machine Learning, Signal Processing, and Computer Vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"<p>Copyright &copy; 2018 IBM Developer Skills Network. This notebook and its source code are released under the terms of the <a href=\"https://cognitiveclass.ai/mit-license/\">MIT License</a>.</p>"
]
}
],
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment