Now that we've shuffled and split our data into training and test datasets, we can finally run our regression 1
using scikit learn. To do that, 2
I'm going to scroll back up and import this capability, so I'm going to say "from sklearn.linear_ 3
model import LinearRegression", I'm going to hit Shift+Enter on this cell, 4
scroll back down where I left off, and then I'm going to create a regression object called "regr", set that 5
equal to "LinearRegression()" and then I'm going to use this regression object and call 6
the "fit" method on it, so "regr.fit()" and now what do I add between these parentheses? 7
What do I give has an argument to our fit method? 8
The data that we should supply is our training datasets, right? 9
So, X_train being the training features and y_train being the training target 10
values. And hitting Shift+Enter on this will train our model. Great! But we don't see any output on the 11
thetas that we've just estimated and also we don't even know how well this model fits our data. Now, 12
before printing out these values, before printing out the values of our theta parameters, let's make some 13
predictions on what we expect to see. Making these predictions is a very important step because it provides 14
a sense check on what the computer is going to spit out to us. We should never blindly trust the numbers 15
that we get back. That's how silly, silly mistakes are made. 16
Besides, we've done all this data exploration work up until now and we can use the knowledge that we 17
gained to make some predictions. 18
For example, we've used seaborn's pairplot to run individual regressions against our target prices. 19
Also, we've created this wonderful correlation matrix to show the correlations of the individual features 20
with our target. We know which correlations were positive and which correlations were negative. 21
So given that this is how our model looks like, let's make some predictions on the signs of the coefficients. 22
Let's predict what kind of signs we're going to see on these theta parameters. 23
So, get out a piece of paper and write down if you expect the theta parameter for RM to be positive 24
or negative, and then do the same thing for NOX, PTRATIO, CRIM, DIS and LSTAT. Did you pause 25
the video and write this down? 26
There is actually one more prediction that we said would make. 27
Remember how a few lessons ago, I asked you if you thought that being next to the Charles River was a good 28
thing for property prices or a bad thing? 29
Well now it's time to also add your prediction to the sign of the CHAS dummy variable as well. 30
Okay. 31
So here's the solution to this challenge. 32
I'm going to print out the value of our intercept and it's gonna be "regr.intercept_" 33
and then I'm going to print out the values of all the coefficients. And to format these values nicely I'm going to 34
actually put them in a data frame. 35
So I'm going to see "pd.DataFrame()" and then our first argument 36
is our data that we're gonna supply 37
and these are the coefficients which we can find under "regr.coef_". 38
The second argument that we're going to supply is called index, 39
so "index = ". 40
and then what 41
we're going to supply as an index are the column names, so "X_train.columns". 42
Yeah. 43
So these are the column names. 44
That way we can have the names of the coefficients next to the values. 45
Let me show you what this looks like. 46
Voila! We get something like this. 47
We can our intercept printed here and we get the name and the value of the different coefficients. 48
Now I'm going to come back up here in to the code where I'm creating our data frame and I'm going to 49
specify third argument called columns. 50
Yeah. 51
And I set that equal to "['COEF']". When I hit Shift+Enter now, you'll 52
see this little 0 here, 53
this the lack of a column name changed to the value "coef". 54
So "columns = " and then a list of 1 value, namely the string "coef" will add a column name to our data 55
frame here. 56
All right. 57
So we've got our results. 58
How do they stack up to your predictions? 59
Remember, we're just looking at the sign of the coefficients here for now. Are the actual coefficients 60
positive or negative according to our model? Did you predict the right sign for the coefficients? 61
Because what we see here is that more crime is bad for house prices and also more pollution is bad 62
for house prices. 63
More rooms on the other hand is good for house prices. 64
It's a positive coefficient. And more students in class is also a negative factor for the house prices 65
as is higher values for LSTAT. Now looking at this, 66
I think this is some really, really good news because the signs of our coefficients passed our first sense 67
check. All these signs that we've just talked about 68
makes sense logically. 69
So the things that we would have expected to be bad for property prices from a logic point of view indeed 70
have a negative coefficient associated with them. 71
Oh and, 72
do you remember how I asked you to make a prediction if living next to the Charles River was a good 73
thing or not? 74
Well, now we've got our answer. 75
Looking at the sign on the CHAS variable, we can see that living next to the Charles River is indeed 76
desirable and the properties that are on the river are more expensive than those that are are away from 77
it, all else equal. In fact we can put a number to it, 78
Bostonians are willing to pay a premium for a property that is next to the river and that premium is 79
equal to about 2000 dollars. 80
Remember, CHAS only has the values 0 or 1. When CHAS is equal to 1 then it's next to the river and 81
1 times approximately 2 is equal to 2, right? 82
And since our target values, our property prices, are in thousands, 83
this translates into 2000 dollars. 84
So what we've just done in Jupyter notebook is calculate all these theta parameters. 85
Our model now looks like this. Using our training data, 86
we've estimated all the values of these theta parameters and the beautiful thing about this linear model 87
is that all our coefficients have a very clear meaning - an increase in the number of rooms by one will 88
increase the price of the property by 3100 hundred dollars. 89
Our model is easy to understand and to interpret, it's the very opposite of a black box that just spits 90
out an answer 91
and we don't know where it came from.