Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save saxenaiway/2935aceceff7cfb1820d44d0c122a08d to your computer and use it in GitHub Desktop.
Save saxenaiway/2935aceceff7cfb1820d44d0c122a08d to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<center>\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/Logos/organization_logo/organization_logo.png\" width=\"300\" alt=\"cognitiveclass.ai logo\" />\n",
"</center>\n",
"\n",
"# Model Evaluation and Refinement\n",
"\n",
"Estimated time needed: **30** minutes\n",
"\n",
"## Objectives\n",
"\n",
"After completing this lab you will be able to:\n",
"\n",
"- Evaluate and refine prediction models\n"
]
},
{
"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>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This dataset was hosted on IBM Cloud object click <a href=\"https://cocl.us/DA101EN_object_storage\">HERE</a> for free storage.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"# Import clean data \n",
"path = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DA0101EN-SkillsNetwork/labs/Data%20files/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 \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df=df._get_numeric_data()\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Libraries for plotting \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%capture\n",
"! pip install ipywidgets"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import interact, interactive, fixed, interact_manual"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Functions for plotting</h2>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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": {},
"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>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y_data = df['price']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"drop price data in x data\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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>. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.10, 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. \n"
]
},
{
"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",
"\n",
"</div>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\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",
"\n",
"</details>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's import <b>LinearRegression</b> from the module <b>linear_model</b>.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.linear_model import LinearRegression"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We create a Linear Regression object:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lre=LinearRegression()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"we fit the model using the feature horsepower \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lre.fit(x_train[['horsepower']], y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's Calculate the R^2 on the test data:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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 40% of the data for training data\n",
"</b>\n",
"</div>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"x_train1, x_test1, y_train1, y_test1 = train_test_split(x_data, y_data, test_size=0.4, random_state=0)\n",
"lre.fit(x_train1[['horsepower']],y_train1)\n",
"lre.score(x_test1[['horsepower']],y_test1)\n",
"\n",
"```\n",
"\n",
"</details>\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. \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Cross-validation Score</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets import <b>model_selection</b> from the module <b>cross_val_score</b>.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Rcross"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can calculate the average and standard deviation of our estimate:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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'. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"Rc=cross_val_score(lre,x_data[['horsepower']], y_data,cv=2)\n",
"Rc.mean()\n",
"\n",
"```\n",
"\n",
"</details>\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 for testing and the other folds are used for training. First import the function:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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>\n"
]
},
{
"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.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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: \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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. \n"
]
},
{
"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. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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. \n"
]
},
{
"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>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's use 55 percent of the data for training and the rest for testing:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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>. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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\".\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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. \n"
]
},
{
"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. \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" R^2 of the training data:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"poly.score(x_train_pr, y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" R^2 of the test data:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.\n"
]
},
{
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following function will be used in the next section; please run the cell.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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",
"\n",
"</div>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"pr1=PolynomialFeatures(degree=2)\n",
"\n",
"```\n",
"\n",
"</details>\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>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"x_train_pr1=pr1.fit_transform(x_train[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg']])\n",
"\n",
"x_test_pr1=pr1.fit_transform(x_test[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg']])\n",
"\n",
"\n",
"```\n",
"\n",
"</details>\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",
"-->\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>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"x_train_pr1.shape #there are now 15 features\n",
"\n",
"\n",
"```\n",
"\n",
"</details>\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>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"poly1=LinearRegression().fit(x_train_pr1,y_train)\n",
"\n",
"\n",
"```\n",
"\n",
"</details>\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>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"yhat_test1=poly1.predict(x_test_pr1)\n",
"\n",
"Title='Distribution Plot of Predicted Value Using Test Data vs Data Distribution of Test Data'\n",
"\n",
"DistributionPlot(y_test, yhat_test1, \"Actual Values (Test)\", \"Predicted Values (Test)\", Title)\n",
"\n",
"```\n",
"\n",
"</details>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #4f): </h1>\n",
"\n",
"<b>Using the distribution plot above, explain in words about the two regions were the predicted prices are less accurate than the actual prices</b>\n",
"\n",
"</div>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"#The predicted value is higher than actual value for cars where the price $10,000 range, conversely the predicted price is lower 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",
"</details>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"ref3\">Part 3: Ridge regression</h2> \n"
]
},
{
"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.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Let's perform a degree two polynomial transformation on our data. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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>.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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 \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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>.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"RigeModel.fit(x_train_pr, y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Similarly, you can obtain a prediction: \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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 \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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 Alpha that minimizes the test error, for example, we can use a for loop. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Rsqu_test = []\n",
"Rsqu_train = []\n",
"dummy1 = []\n",
"Alpha = 10 * np.array(range(0,1000))\n",
"for alpha in Alpha:\n",
" RigeModel = Ridge(alpha=alpha) \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 \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"width = 12\n",
"height = 10\n",
"plt.figure(figsize=(width, height))\n",
"\n",
"plt.plot(Alpha,Rsqu_test, label='validation data ')\n",
"plt.plot(Alpha,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 validation data, and the red line represents the R^2 of the training data. The x-axis represents the different values of Alpha. \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here the model is built and tested on the same data. So the training and test data are the same.\n",
"\n",
"The red line in figure 6 represents the R^2 of the training data. \n",
"As Alpha increases the R^2 decreases. \n",
"Therefore as Alpha increases the model performs worse on the training data. \n",
"\n",
"The blue line represents the R^2 on the validation data. \n",
"As the value for Alpha increases the R^2 increases and converges at a point \n"
]
},
{
"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",
"\n",
"</div>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"RigeModel = Ridge(alpha=10) \n",
"RigeModel.fit(x_train_pr, y_train)\n",
"RigeModel.score(x_test_pr, y_test)\n",
"\n",
"```\n",
"\n",
"</details>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"ref4\">Part 4: Grid Search</h2>\n"
]
},
{
"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.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's import <b>GridSearchCV</b> from the module <b>model_selection</b>.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import GridSearchCV"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We create a dictionary of parameter values:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"RR=Ridge()\n",
"RR"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a ridge grid search object \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Grid1 = GridSearchCV(RR, parameters1,cv=4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fit the model \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"BestRR=Grid1.best_estimator_\n",
"BestRR"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We now test our model on the test data \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\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",
"\n",
"```\n",
"\n",
"</details>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Thank you for completing this lab!\n",
"\n",
"## Author\n",
"\n",
"<a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a>\n",
"\n",
"### Other Contributors\n",
"\n",
"<a href=\"https://www.linkedin.com/in/mahdi-noorian-58219234/\" target=\"_blank\">Mahdi Noorian PhD</a>\n",
"\n",
"Bahare Talayian\n",
"\n",
"Eric Xiao\n",
"\n",
"Steven Dong\n",
"\n",
"Parizad\n",
"\n",
"Hima Vasudevan\n",
"\n",
"<a href=\"https://www.linkedin.com/in/fiorellawever/\" target=\"_blank\">Fiorella Wenver</a>\n",
"\n",
"<a href=\" https://www.linkedin.com/in/yi-leng-yao-84451275/ \" target=\"_blank\" >Yi Yao</a>.\n",
"\n",
"## Change Log\n",
"\n",
"| Date (YYYY-MM-DD) | Version | Changed By | Change Description |\n",
"| ----------------- | ------- | ---------- | ----------------------------------- |\n",
"| 2020-10-30 | 2.3 | Lakshmi | Changed URL of csv |\n",
"| 2020-10-05 | 2.2 | Lakshmi | Removed unused library imports |\n",
"| 2020-09-14 | 2.1 | Lakshmi | Made changes in OverFitting section |\n",
"| 2020-08-27 | 2.0 | Lavanya | Moved lab to course repo in GitLab |\n",
"\n",
"<hr>\n",
"\n",
"## <h3 align=\"center\"> © IBM Corporation 2020. All rights reserved. <h3/>\n"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment