In this section, we will develop several models that will predict the price of the car using the variables or features. This is just an estimate but should give us an objective idea of how much the car should cost.

Some questions we want to ask in this module

- do I know if the dealer is offering fair value for my trade-in?
- do I know if I put a fair value on my car?

Data Analytics, we often use **Model Development** to help us predict future observations from the data we have.

A Model will help us understand the exact relationship between different variables and how these variables are used to predict the result.

Import libraries

In [2]:

```
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
```

load data and store in dataframe df:

This dataset was hosted on IBM Cloud object click HERE for free storage.

In [3]:

```
# path of data
path = 'https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/automobileEDA.csv'
df = pd.read_csv(path)
df.head()
```

Out[3]:

One example of a Data Model that we will be using is

Simple Linear Regression is a method to help us understand the relationship between two variables:

- The predictor/independent variable (X)
- The response/dependent variable (that we want to predict)(Y)

The result of Linear Regression is a **linear function** that predicts the response (dependent) variable as a function of the predictor (independent) variable.

$$Y:Response\text{}Variable\phantom{\rule{0ex}{0ex}}X:Predictor\text{}Variables$$

**Linear function:**

$$Yhat=a+bX$$

- a refers to the
**intercept**of the regression line0, in other words: the value of Y when X is 0 - b refers to the
**slope**of the regression line, in other words: the value with which Y changes when X increases by 1 unit

In [4]:

```
from sklearn.linear_model import LinearRegression
```

In [5]:

```
lm = LinearRegression()
lm
```

Out[5]:

For this example, we want to look at how highway-mpg can help us predict car price. Using simple linear regression, we will create a linear function with "highway-mpg" as the predictor variable and the "price" as the response variable.

In [6]:

```
X = df[['highway-mpg']]
Y = df['price']
```

Fit the linear model using highway-mpg.

In [7]:

```
lm.fit(X,Y)
```

Out[7]:

We can output a prediction

In [8]:

```
Yhat=lm.predict(X)
Yhat[0:5]
```

Out[8]:

In [9]:

```
lm.intercept_
```

Out[9]:

In [10]:

```
lm.coef_
```

Out[10]:

As we saw above, we should get a final linear model with the structure:

$$Yhat=a+bX$$

Plugging in the actual values we get:

**price** = 38423.31 - 821.73 x **highway-mpg**

In [11]:

```
# Write your code below and press Shift+Enter to execute
lm1 = LinearRegression()
lm1
```

Out[11]:

Double-click **here** for the solution.

In [12]:

```
# Write your code below and press Shift+Enter to execute
X1 = df[['engine-size']]
Y1 = df['price']
```

Double-click **here** for the solution.

In [13]:

```
# Write your code below and press Shift+Enter to execute
lm1.fit(X1,Y1)
lm1.coef_
```

Out[13]:

In [14]:

```
# Write your code below and press Shift+Enter to execute
lm1.intercept_
```

Out[14]:

Double-click **here** for the solution.

Double-click **here** for the solution.

What if we want to predict car price using more than one variable?

If we want to use more variables in our model to predict car price, we can use **Multiple Linear Regression**.
Multiple Linear Regression is very similar to Simple Linear Regression, but this method is used to explain the relationship between one continuous response (dependent) variable and **two or more** predictor (independent) variables.
Most of the real-world regression models involve multiple predictors. We will illustrate the structure by using four predictor variables, but these results can generalize to any integer:

$$Y:Response\text{}Variable\phantom{\rule{0ex}{0ex}}{X}_{1}:Predictor\text{}Variable\text{}1\phantom{\rule{0ex}{0ex}}{X}_{2}:Predictor\text{}Variable\text{}2\phantom{\rule{0ex}{0ex}}{X}_{3}:Predictor\text{}Variable\text{}3\phantom{\rule{0ex}{0ex}}{X}_{4}:Predictor\text{}Variable\text{}4\phantom{\rule{0ex}{0ex}}$$

$$a:intercept\phantom{\rule{0ex}{0ex}}{b}_{1}:coefficients\text{}of\text{}Variable\text{}1\phantom{\rule{0ex}{0ex}}{b}_{2}:coefficients\text{}of\text{}Variable\text{}2\phantom{\rule{0ex}{0ex}}{b}_{3}:coefficients\text{}of\text{}Variable\text{}3\phantom{\rule{0ex}{0ex}}{b}_{4}:coefficients\text{}of\text{}Variable\text{}4\phantom{\rule{0ex}{0ex}}$$

The equation is given by

$$Yhat=a+{b}_{1}{X}_{1}+{b}_{2}{X}_{2}+{b}_{3}{X}_{3}+{b}_{4}{X}_{4}$$

From the previous section we know that other good predictors of price could be:

- Horsepower
- Curb-weight
- Engine-size
- Highway-mpg

In [15]:

```
Z = df[['horsepower', 'curb-weight', 'engine-size', 'highway-mpg']]
```

Fit the linear model using the four above-mentioned variables.

In [16]:

```
lm.fit(Z, df['price'])
```

Out[16]:

What is the value of the intercept(a)?

In [17]:

```
lm.intercept_
```

Out[17]:

What are the values of the coefficients (b1, b2, b3, b4)?

In [18]:

```
lm.coef_
```

Out[18]:

What is the final estimated linear model that we get?

As we saw above, we should get a final linear function with the structure:

$$Yhat=a+{b}_{1}{X}_{1}+{b}_{2}{X}_{2}+{b}_{3}{X}_{3}+{b}_{4}{X}_{4}$$

What is the linear function we get in this example?

**Price** = -15678.742628061467 + 52.65851272 x **horsepower** + 4.69878948 x **curb-weight** + 81.95906216 x **engine-size** + 33.58258185 x **highway-mpg**

In [19]:

```
# Write your code below and press Shift+Enter to execute
Z = df[['normalized-losses', 'highway-mpg']]
lm.fit(Z, df['price'])
```

Out[19]:

Double-click **here** for the solution.

In [20]:

```
# Write your code below and press Shift+Enter to execute
lm.coef_
```

Out[20]:

Double-click **here** for the solution.

Now that we've developed some models, how do we evaluate our models and how do we choose the best one? One way to do this is by using visualization.

import the visualization package: seaborn

In [21]:

```
# import the visualization package: seaborn
import seaborn as sns
%matplotlib inline
```

When it comes to simple linear regression, an excellent way to visualize the fit of our model is by using **regression plots**.

This plot will show a combination of a scattered data points (a **scatter plot**), as well as the fitted **linear regression** line going through the data. This will give us a reasonable estimate of the relationship between the two variables, the strength of the correlation, as well as the direction (positive or negative correlation).

Let's visualize Horsepower as potential predictor variable of price:

In [22]:

```
width = 12
height = 10
plt.figure(figsize=(width, height))
sns.regplot(x="highway-mpg", y="price", data=df)
plt.ylim(0,)
```

Out[22]:

We can see from this plot that price is negatively correlated to highway-mpg, since the regression slope is negative. One thing to keep in mind when looking at a regression plot is to pay attention to how scattered the data points are around the regression line. This will give you a good indication of the variance of the data, and whether a linear model would be the best fit or not. If the data is too far off from the line, this linear model might not be the best model for this data. Let's compare this plot to the regression plot of "peak-rpm".

In [23]:

```
plt.figure(figsize=(width, height))
sns.regplot(x="peak-rpm", y="price", data=df)
plt.ylim(0,)
```

Out[23]:

Comparing the regression plot of "peak-rpm" and "highway-mpg" we see that the points for "highway-mpg" are much closer to the generated line and on the average decrease. The points for "peak-rpm" have more spread around the predicted line, and it is much harder to determine if the points are decreasing or increasing as the "highway-mpg" increases.

In [24]:

```
# Write your code below and press Shift+Enter to execute
df[["peak-rpm","highway-mpg","price"]].corr()
```

Out[24]:

Double-click **here** for the solution.

A good way to visualize the variance of the data is to use a residual plot.

What is a **residual**?

The difference between the observed value (y) and the predicted value (Yhat) is called the residual (e). When we look at a regression plot, the residual is the distance from the data point to the fitted regression line.

So what is a **residual plot**?

A residual plot is a graph that shows the residuals on the vertical y-axis and the independent variable on the horizontal x-axis.

What do we pay attention to when looking at a residual plot?

We look at the spread of the residuals:

- If the points in a residual plot are **randomly spread out around the x-axis**, then a **linear model is appropriate** for the data. Why is that? Randomly spread out residuals means that the variance is constant, and thus the linear model is a good fit for this data.

In [25]:

```
width = 12
height = 10
plt.figure(figsize=(width, height))
sns.residplot(df['highway-mpg'], df['price'])
plt.show()
```

*What is this plot telling us?*

We can see from this residual plot that the residuals are not randomly spread around the x-axis, which leads us to believe that maybe a non-linear model is more appropriate for this data.

How do we visualize a model for Multiple Linear Regression? This gets a bit more complicated because you can't visualize it with regression or residual plot.

One way to look at the fit of the model is by looking at the **distribution plot**: We can look at the distribution of the fitted values that result from the model and compare it to the distribution of the actual values.

First lets make a prediction

In [26]:

```
Y_hat = lm.predict(Z)
```

In [27]:

```
plt.figure(figsize=(width, height))
ax1 = sns.distplot(df['price'], hist=False, color="r", label="Actual Value")
sns.distplot(Yhat, hist=False, color="b", label="Fitted Values" , ax=ax1)
plt.title('Actual vs Fitted Values for Price')
plt.xlabel('Price (in dollars)')
plt.ylabel('Proportion of Cars')
plt.show()
plt.close()
```

We can see that the fitted values are reasonably close to the actual values, since the two distributions overlap a bit. However, there is definitely some room for improvement.

**Polynomial regression** is a particular case of the general linear regression model or multiple linear regression models.

We get non-linear relationships by squaring or setting higher-order terms of the predictor variables.

There are different orders of polynomial regression:

$$Yhat=a+{b}_{1}{X}^{2}+{b}_{2}{X}^{2}$$

$$Yhat=a+{b}_{1}{X}^{2}+{b}_{2}{X}^{2}+{b}_{3}{X}^{3}\phantom{\rule{0ex}{0ex}}$$

$$Y=a+{b}_{1}{X}^{2}+{b}_{2}{X}^{2}+{b}_{3}{X}^{3}....\phantom{\rule{0ex}{0ex}}$$

We saw earlier that a linear model did not provide the best fit while using highway-mpg as the predictor variable. Let's see if we can try fitting a polynomial model to the data instead.

We will use the following function to plot the data:

In [28]:

```
def PlotPolly(model, independent_variable, dependent_variabble, Name):
x_new = np.linspace(15, 55, 100)
y_new = model(x_new)
plt.plot(independent_variable, dependent_variabble, '.', x_new, y_new, '-')
plt.title('Polynomial Fit with Matplotlib for Price ~ Length')
ax = plt.gca()
ax.set_facecolor((0.898, 0.898, 0.898))
fig = plt.gcf()
plt.xlabel(Name)
plt.ylabel('Price of Cars')
plt.show()
plt.close()
```

lets get the variables

In [29]:

```
x = df['highway-mpg']
y = df['price']
```

Let's fit the polynomial using the function **polyfit**, then use the function **poly1d** to display the polynomial function.

In [30]:

```
# Here we use a polynomial of the 3rd order (cubic)
f = np.polyfit(x, y, 3)
p = np.poly1d(f)
print(p)
```

Let's plot the function

In [31]:

```
PlotPolly(p, x, y, 'highway-mpg')
```

In [32]:

```
np.polyfit(x, y, 3)
```

Out[32]:

We can already see from plotting that this polynomial model performs better than the linear model. This is because the generated polynomial function "hits" more of the data points.

In [33]:

```
# Write your code below and press Shift+Enter to execute
f1 = np.polyfit(x, y, 11)
p1 = np.poly1d(f1)
print(p)
PlotPolly(p1,x,y, 'Length')
```

Double-click **here** for the solution.

The analytical expression for Multivariate Polynomial function gets complicated. For example, the expression for a second-order (degree=2)polynomial with two variables is given by:

$$Yhat=a+{b}_{1}{X}_{1}+{b}_{2}{X}_{2}+{b}_{3}{X}_{1}{X}_{2}+{b}_{4}{X}_{1}^{2}+{b}_{5}{X}_{2}^{2}$$

We can perform a polynomial transform on multiple features. First, we import the module:

In [34]:

```
from sklearn.preprocessing import PolynomialFeatures
```

We create a **PolynomialFeatures** object of degree 2:

In [35]:

```
pr=PolynomialFeatures(degree=2)
pr
```

Out[35]:

In [36]:

```
Z_pr=pr.fit_transform(Z)
```

The original data is of 201 samples and 4 features

In [37]:

```
Z.shape
```

Out[37]:

after the transformation, there 201 samples and 15 features

In [38]:

```
Z_pr.shape
```

Out[38]:

Data Pipelines simplify the steps of processing the data. We use the module **Pipeline** to create a pipeline. We also use **StandardScaler** as a step in our pipeline.

In [39]:

```
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
```

We create the pipeline, by creating a list of tuples including the name of the model or estimator and its corresponding constructor.

In [40]:

```
Input=[('scale',StandardScaler()), ('polynomial', PolynomialFeatures(include_bias=False)), ('model',LinearRegression())]
```

we input the list as an argument to the pipeline constructor

In [41]:

```
pipe=Pipeline(Input)
pipe
```

Out[41]:

We can normalize the data, perform a transform and fit the model simultaneously.

In [42]:

```
pipe.fit(Z,y)
```

Out[42]:

Similarly, we can normalize the data, perform a transform and produce a prediction simultaneously

In [43]:

```
ypipe=pipe.predict(Z)
ypipe[0:4]
```

Out[43]:

In [44]:

```
# Write your code below and press Shift+Enter to execute
Input=[('scale',StandardScaler()),('model',LinearRegression())]
pipe=Pipeline(Input)
pipe.fit(Z,y)
ypipe=pipe.predict(Z)
ypipe[0:10]
```

Out[44]:

</div>
Double-click **here** for the solution.

When evaluating our models, not only do we want to visualize the results, but we also want a quantitative measure to determine how accurate the model is.

Two very important measures that are often used in Statistics to determine the accuracy of a model are:

**R^2 / R-squared****Mean Squared Error (MSE)**

R squared, also known as the coefficient of determination, is a measure to indicate how close the data is to the fitted regression line.

The value of the R-squared is the percentage of variation of the response variable (y) that is explained by a linear model.

**Mean Squared Error (MSE)**

The Mean Squared Error measures the average of the squares of errors, that is, the difference between actual value (y) and the estimated value (ŷ).

Let's calculate the R^2

In [45]:

```
#highway_mpg_fit
lm.fit(X, Y)
# Find the R^2
print('The R-square is: ', lm.score(X, Y))
```

We can say that ~ 49.659% of the variation of the price is explained by this simple linear model "horsepower_fit".

Let's calculate the MSE

We can predict the output i.e., "yhat" using the predict method, where X is the input variable:

In [46]:

```
Yhat=lm.predict(X)
print('The output of the first four predicted value is: ', Yhat[0:4])
```

lets import the function **mean_squared_error** from the module **metrics**

In [47]:

```
from sklearn.metrics import mean_squared_error
```

we compare the predicted results with the actual results

In [48]:

```
mse = mean_squared_error(df['price'], Yhat)
print('The mean square error of price and predicted value is: ', mse)
```

Let's calculate the R^2

In [49]:

```
# fit the model
lm.fit(Z, df['price'])
# Find the R^2
print('The R-square is: ', lm.score(Z, df['price']))
```

We can say that ~ 80.896 % of the variation of price is explained by this multiple linear regression "multi_fit".

Let's calculate the MSE

we produce a prediction

In [50]:

```
Y_predict_multifit = lm.predict(Z)
```

we compare the predicted results with the actual results

In [51]:

```
print('The mean square error of price and predicted value using multifit is: ', \
mean_squared_error(df['price'], Y_predict_multifit))
```

Let's calculate the R^2

let’s import the function **r2_score** from the module **metrics** as we are using a different function

In [52]:

```
from sklearn.metrics import r2_score
```

We apply the function to get the value of r^2

In [53]:

```
r_squared = r2_score(y, p(x))
print('The R-square value is: ', r_squared)
```

We can say that ~ 67.419 % of the variation of price is explained by this polynomial fit

We can also calculate the MSE:

In [54]:

```
mean_squared_error(df['price'], p(x))
```

Out[54]:

In the previous section, we trained the model using the method **fit**. Now we will use the method **predict** to produce a prediction. Lets import **pyplot** for plotting; we will also be using some functions from numpy.

In [55]:

```
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
```

Create a new input

In [56]:

```
new_input=np.arange(1, 100, 1).reshape(-1, 1)
```

Fit the model

In [57]:

```
lm.fit(X, Y)
lm
```

Out[57]:

Produce a prediction

In [58]:

```
yhat=lm.predict(new_input)
yhat[0:5]
```

Out[58]:

we can plot the data

In [59]:

```
plt.plot(new_input, yhat)
plt.show()
```

Now that we have visualized the different models, and generated the R-squared and MSE values for the fits, how do we determine a good model fit?

*What is a good R-squared value?*

When comparing models, **the model with the higher R-squared value is a better fit** for the data.

*What is a good MSE?*

When comparing models, **the model with the smallest MSE value is a better fit** for the data.

Simple Linear Regression: Using Highway-mpg as a Predictor Variable of Price.

- R-squared: 0.49659118843391759
- MSE: 3.16 x10^7

Multiple Linear Regression: Using Horsepower, Curb-weight, Engine-size, and Highway-mpg as Predictor Variables of Price.

- R-squared: 0.80896354913783497
- MSE: 1.2 x10^7

Polynomial Fit: Using Highway-mpg as a Predictor Variable of Price.

- R-squared: 0.6741946663906514
- MSE: 2.05 x 10^7

Usually, the more variables you have, the better your model is at predicting, but this is not always true. Sometimes you may not have enough data, you may run into numerical problems, or many of the variables may not be useful and or even act as noise. As a result, you should always check the MSE and R^2.

So to be able to compare the results of the MLR vs SLR models, we look at a combination of both the R-squared and MSE to make the best conclusion about the fit of the model.

**MSE**The MSE of SLR is 3.16x10^7 while MLR has an MSE of 1.2 x10^7. The MSE of MLR is much smaller.**R-squared**: In this case, we can also see that there is a big difference between the R-squared of the SLR and the R-squared of the MLR. The R-squared for the SLR (~0.497) is very small compared to the R-squared for the MLR (~0.809).

This R-squared in combination with the MSE show that MLR seems like the better model fit in this case, compared to SLR.

**MSE**: We can see that Polynomial Fit brought down the MSE, since this MSE is smaller than the one from the SLR.**R-squared**: The R-squared for the Polyfit is larger than the R-squared for the SLR, so the Polynomial Fit also brought up the R-squared quite a bit.

Since the Polynomial Fit resulted in a lower MSE and a higher R-squared, we can conclude that this was a better fit model than the simple linear regression for predicting Price with Highway-mpg as a predictor variable.

**MSE**: The MSE for the MLR is smaller than the MSE for the Polynomial Fit.**R-squared**: The R-squared for the MLR is also much larger than for the Polynomial Fit.

Comparing these three models, we conclude that **the MLR model is the best model** to be able to predict price from our dataset. This result makes sense, since we have 27 variables in total, and we know that more than one of those variables are potential predictors of the final car price.

This notebook was written by Mahdi Noorian PhD, Joseph Santarcangelo, Bahare Talayian, Eric Xiao, Steven Dong, Parizad, Hima Vsudevan and Fiorella Wenver and Yi Yao.

Joseph Santarcangelo 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.

Copyright © 2018 IBM Developer Skills Network. This notebook and its source code are released under the terms of the MIT License.