So the next question is: how well does the model fit to data? Now, as a challenge, 1
do you remember how to print out a regression's r-squared value from the previous lessons? 2
I want you to print out two different r-squared values. 3
I want you to print out the r-squared value for the training dataset and the r-squared value for the 4
testing dataset. 5
I'll give you a few seconds to pause the video and give this a shot. And here's the solution - to obtain 6
the r-squared, 7
we access it through the regression object's score method. 8
So my first print statement is gonna read 9
"'Training data r-squared: ', regr.score(X_train, y_ 10
train)". 11
So this will be the r-squared of the training dataset; and the r-squared of the testing dataset will 12
be "'Test data r-squared:', regr.score(X_test, y_ 13
test)". 14
So let's take a look at what these turn out to be. 15
For our training data we get an r-squared of 0.75 and for our test dataset we get 16
an r-squared of 0.67. 17
Now these values are actually both quite high. 18
I mean when you think about it, the real world is a very, very complicated place. 19
But we've been able to explain approximately 75 percent of the variance in our house prices with just 20
13 features. 13 things and we can explain a lot of what's going on in the real world. Now a question that 21
you might ask looking at these two values is: why is the r-squared lower for the test dataset? 22
And the answer is is that what we've done so far is calculate the thetas only on our training data. 23
The algorithm hasn't seen the test data before. 24
As such, the performance of our model on the data that a model hasn't seen is always gonna be worse. 25
It makes sense for the r-squared to be lower on the test data than on the training data. 26
However, running the model on data that's not in the sample actually gives us some idea of its predictive 27
power. 28
We get a little bit of a feeling for how our model would fare in the real world. 29
And it's very encouraging to see that the r-squared even on the test data is still quite high. 30
So our model actually performs relatively well. 31
Okay, so we spec'd out our model and we've run our first regression. 32
We did a quick sense check and looked at the performance. 33
Now we're getting to the evaluation stage. Now before we move on to the next lesson, 34
let's add another markdown cell above this one here and add the section heading "Multivariable Regression". 35
That way we can keep track of what we're doing. 36
The other thing I'm going to do is take these two lines of code, 37
cut them and paste them down here. 38
That way when I hit Shift+Enter we'll be able to see our data frame of coefficients. 39
Otherwise the output is hidden by the print statements. 40
There we go. 41
Now we're ready to move on. 42
I'll see you in the next lessons.