Evaluating the model

We have used a learning algorithm to estimate a model's parameters from the training data. How can we assess whether our model is a good representation of the real relationship? Let's assume that you have found another page in your pizza journal. We will use the entries on this page as a test set to measure the performance of our model:

Test Instance

Diameter (in inches)

Observed price (in dollars)

Predicted price (in dollars)

1

8

11

9.7759

2

9

8.5

10.7522

3

11

15

12.7048

4

16

18

17.5863

5

12

11

13.6811

Several measures can be used to assess our model's predictive capabilities. We will evaluate our pizza-price predictor using r-squared. R-squared measures how well the observed values of the response variables are predicted by the model. More concretely, r-squared is the proportion of the variance in the response variable that is explained by the model. An r-squared score of one indicates that the response variable can be predicted without any error using the model. An r-squared score of one half indicates that half of the variance in the response variable can be predicted using the model. There are several methods to calculate r-squared. In the case of simple linear regression, r-squared is equal to the square of the Pearson product moment correlation coefficient, or Pearson's r.

Using this method, r-squared must be a positive number between zero and one. This method is intuitive; if r-squared describes the proportion of variance in the response variable explained by the model, it cannot be greater than one or less than zero. Other methods, including the method used by scikit-learn, do not calculate r-squared as the square of Pearson's r, and can return a negative r-squared if the model performs extremely poorly. We will follow the method used by scikit-learn to calculate r-squared for our pizza-price predictor.

First, we must measure the total sum of the squares. Evaluating the model is the observed value of the response variable for the Evaluating the model test instance, and Evaluating the model is the mean of the observed values of the response variable

Evaluating the model
Evaluating the model

Next, we must find the residual sum of the squares. Recall that this is also our cost function.

Evaluating the model
Evaluating the model

Finally, we can find r-squared using the following formula:

Evaluating the model
Evaluating the model

An r-squared score of 0.6620 indicates that a large proportion of the variance in the test instances' prices is explained by the model. Now, let's confirm our calculation using scikit-learn. The score method of LinearRegression returns the model's r-squared value, as seen in the following example:

>>> from sklearn.linear_model import LinearRegression
>>> X = [[6], [8], [10], [14],   [18]]
>>> y = [[7], [9], [13], [17.5], [18]]
>>> X_test = [[8],  [9],   [11], [16], [12]]
>>> y_test = [[11], [8.5], [15], [18], [11]]
>>> model = LinearRegression()
>>> model.fit(X, y)
>>> print 'R-squared: %.4f' % model.score(X_test, y_test)
R-squared: 0.6620
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset