Now I hope that you can't wait to calculate the mean squared error. From the previous cell up here, 1
we already know what the lowest cost theta zero and theta one parameters are for our regression. 2
And this is thanks to the scikit learn package. 3
But let's work out what the mean squared error actually is for these two values. 4
And for that we're going to need our estimated y values. 5
Looking at our equation up here we need to calculate our y hat. 6
What's this y hat equal to? Well, our y hat is gonna be equal to theta_0 + theta_1*x. 7
This is the equation that underlines our linear regression in this case - our y hat is based on the intercept 8
plus the slope, 9
times our X values. 10
Let's translate that to Python code. So I'm going to create a variable called y_hat 11
and I'm gonna set it equal to our theta_0 value which was this one here. 12
I'm going to copy this. 13
Come down here. 14
Paste it in, add a plus sign. 15
Take the theta_1 value, 16
copy and paste it in and then write 17
"* x_5". 18
This after all are all the X's in our dataset. 19
And that way we can calculate all the predicted values in our dataset. 20
Let's print these out for good measure. 21
So I'm going to say "print" and then the string "'Est values y_hat are: ', 22
y_5". 23
Let me hit Shift+Enter and let's see what these look like. 24
So here they are. 25
These are all the predicted values. Now, 26
one thing that you can do is if you don't want this first value here on the same line as your string 27
what you can do is you can come in here with a string is and insert a special character - 28
the new line character. 29
So that's backslash and then 30
"n". When I hit shift enter now that 0.969 will move to a new line, "\n" is 31
like a special character for hitting return on your keyboard. 32
So how do these predicted values compared to our actual values? 33
Let's print those out as well. 34
So I'm going to say "print" and then write "In comparison, the actual y values are \n, y_ 35
5". 36
These are actual values. 37
There we go. 38
Ideally we want these estimated values to be as close to these actual values as possible. 39
And I'm looking at this. 40
They're actually not too far off. 41
This is not too bad. 42
Given this theta_0 and this theta_1. 43
So now that we know how to calculate our y hat values, let's work out the mean square error of the regression. 44
And I think this actually makes a really good challenge. 45
So can you write a Python function that takes two inputs y and y hat and returns the mean squared error? 46
And after you've done that, after you've written this MSE function can you call this function and print 47
out the mean squared error with the y hat calculated above, so feed in this y hat value into your MSE 48
function and print out what the mean squared error is. 49
Now remember the mean squared error equation looks like this. 50
We've got that formula in LaTeX notation, so all you need to do is translate it into Python code and 51
look up what code to use for that pesky summation symbol right here. 52
I'll give you a few seconds to pause the video and try this on your own 53
All right. 54
Ready? 55
So I hope you figured this out. 56
Now in terms of the solution. 57
I'm not just going to show you one solution. 58
I'm going to show you three different ways that you can implement this, because there's actually many 59
ways you can skin a cat as they say. 60
So while you have the video paused and were solving the challenge, 61
I was busy looking up possible uses for all that cat skin. 62
And the best one I came across was a Japanese instrument called shamisen. 63
Apparently the drum on this banjo was actually traditionally made from from cat skin. 64
So uh there's your random fact of the day. If you've come across any other uses do let me know in the 65
comments section. 66
The Python code that you will have written will probably look something like this. 67
You're going to have have "def mse():" and then for the parameters you'll have y and say y_hat, colon. And the 68
first possible approach would look something like this. 69
You might have a variable say call it "mse_calc" and that would have been equal to 1 divided by 7 because 70
we've got 7 data points and you multiply that by the sum, 71
this is an inbuilt Python function; 72
"sum()" and then inside the sum you'll have another set of parentheses and there you'll have 73
"y-y_hat" to the power of 2. 74
So **2. 75
And because this function needs an output you would return the results of your calculation. 76
So mse_calc and this is one possible way to do it. 77
Let me call this function and print out the mean squared error of y hat that we calculated. 78
So it'll be "mse(y_5,", because these are the actual y values and then I'll supply y_hat that we 79
calculated a few cells above. I'm going to hit Shift+Enter and see what we get. We get 0.95 approximately. 80
Now there is one improvement that we can make to this function. 81
So this 1/7 will probably be bothering you a little bit because it's hardcoded and 82
it would only work or calculate the mean squared error correctly 83
if we had 7 data points - if we had 8 or 9 then the mean square error formula wouldn't be apt 84
anymore, wouldn't be correct anymore with this Python code. 85
So I'm going to comment this out. 86
But I'm going to copy, paste it below, 87
and what I want to do is I'm going to replace this 1/7 with some different code. 88
I'm going to say "1/y.size" - y.size will return the length or the number of 89
samples that are fed in to the place holder for our y values. 90
So this will also work. 91
Let me hit Shift+Enter on this cell and the one below to prove just that. Writing the code this way makes 92
the MSE function much more general because it figures out the number of samples inside of the function. 93
It doesn't have to be supplied as an extra parameter and it's not hardcoded in the instructions themselves. 94
So this is already quite nice but there is another way you could have done this as well, maybe you did 95
a bit of googling and you figured out that numpy actually has a function called "average" and this does 96
both the summation and the dividing by the number of samples for us. 97
So let's see what this would look like. 98
This would be "mse_calc = np.average()" and then it needs two things - 99
it needs the function itself that it's averaging and if it should be averaging the rows, the columns 100
or the whole thing. 101
So first things first, the function its averaging would be (y-y_hat)**2 102
and the way to tell it whether it should average the rows or the columns would be by supplying the "axis" 103
argument, so "axis = 0" sums up the rows instead of both the rows and columns which is not what 104
we want. 105
So when I hit Shift+Enter on this and then run the cell below again, we can see that this works as well. 106
But just because we've printed out a mean square error in this cell here doesn't mean it's correct. 107
So let's check it against an inbuilt function from scikit learn. 108
So we wrap this in a print statement. Write 109
print and then the string 110
"Manually calculated MSE is:" and then we have our mse function in here. To use the inbuilt function 111
that inbuilt mean square error function from scikit learn, 112
we're gonna have to import it. 113
I'll have to scroll to the very top and then here we're gonna add another import statement, we're going to say 114
"from sklearn.metrics import mean_squared_error . 115
Sorry sorry. 116
I hope you're not too upset and will forgive me that I made you do all this work even though there is 117
an inbuilt function for this already. Let's call this mean_squared_error function with both our 118
manually calculated y_hat as well as from the output of the regression directly. 119
So where we were adding our print statement let's add another one let's add "print('MSE 120
regression using manual calc is:', )", and then "mean_squared_error()" and it will 121
supply two inputs: y_5, our actual y values and y_hat, which we've calculated 122
above just here. 123
And now let me copy this line and paste it below. 124
And this one will read "'MSE regression is', mean_squared_error(y_5,)" and instead of our manually calculated 125
one we can use "regr.predict(x_5)" and when we hit Shift+Enter, 126
we see that indeed all the outputs agree with one another. Our manually calculated mean squared error is 127
the same as what we get from the machine learning package. 128
So we've dissected and implemented this cost function correctly. 129
Brilliant. 130
Now it's time to plot our cost function and visualize it. We'll do that in the next lesson. 131
I'll see you there.