All right. 1
So in this lesson it's finally time to plot our gradient descent. 2
Here's how we're gonna do it. 3
The first thing is we're going to add two more variables, these variables will collect all the values 4
that are being calculated within our for loop so that we can plot our algorithm's progress on the chart. 5
I'm gonna call the first one of these "plot_vals" and this is gonna be our thetas array reshaped into 6
a one by two array. 7
So one row, two columns. 8
This is where we're gonna add on all the theta values that we calculate in the for loop and then the second 9
variable is gonna be called "mse_vals" and this is going to start out simply as our first mean squared 10
error calculation from our initial guess. 11
So it's going to be "y_5, thetas[0] + thetas[1] * x_5". 12
Now that we've defined our two variables and given them some starting values I'm going to add a little 13
comment, I'm going to say "Collect data points for scatter plot" because that's ultimately what we're gonna 14
use these ones for. Now, within our loop what we need to do is append the new values to our numpy arrays 15
so I'm going to use our old friends concatenate and append. Let's use concatenate on plot_vals, so 16
"plot_vals" is gonna be equal to "np.concatenate()" and then we need to supply 17
two things - 18
the first is that tuple that's going to consist of the old value of plot_vals, so all the previous 19
values that we calculated in the loop plus our updated thetas array. 20
So this is gonna be "thetas.reshape(1,2)" and then outside of this tuple we're 21
gonna have to specify the "axis=0". What this line of code will do is it will take 22
the existing array of plot_vals, which is a 1 by 2 array and it will concatenate it with the new theta values 23
which we've reshaped into a 1 by 2 array. Now we need to capture our mean square error calculations. 24
So it's gonna be mse_vals, 25
and just to mix things up I want to use "np.append" and accomplish the very same thing, namely append 26
values to our array. 27
So first, I have to specify where I want to append the values to. 28
And this is going to be the mse_vals array and then the second thing I have to specify which 29
values I want to append. 30
So the values are gonna be equal to the new set of calculations. 31
So I'm gonna call our mse function, pass in the y_5, actual y values and then I'm going to pass 32
in that y hat values and if you remember with four linear regression this was theta zero which was fetus 33
square brackets zero plus theta is square brackets 1 times X on the score 5 and that's it. 34
Let me press shift enter and try to run this. 35
See if I've made any errors no. 36
So far so good. 37
Now it's time to pull lots the thing. 38
So in order to plotted Umno use an existing cell that we've already made copy it pasted in and then 39
had the scatter plot to modify the code scrolling up. 40
I'm going to take this cell right here. 41
Copy it. 42
Come down here and then I'm going to paste the cell below to add our gradient descent algorithms progress. 43
I just have to add one line of code a scatter plot someone to do that on the axes so a X don't scatter 44
parentheses. 45
And then I have to supply three things I have to supply the x values the y values and the z values. 46
So our X values are not chart where the theta zeros and we've stored this in a variable called plot 47
underscore values and we've stored that as the first column. 48
So to select the entire column I can add a colon comma and then a zero to select all the rows in column 49
0 within this array and then for our theta one values I'm going to select plot on the score vowels and 50
then I'm going to choose the second column so all the values in the second column. 51
So these are all the rows. 52
Once again a colon to select all the rows at column index 1. 53
And finally we need the values for the z axis. 54
And this was our MSE on a score vowels variable and that's pretty much it. 55
That's the key information already supplied. 56
Now in terms of sizing up all scatter plot I mean choose 80 for the size of the dots. 57
And in terms of color I'm going to go for I don't know black. 58
Now let me hit shift enter and see what this looks like. 59
Now if you're looking at this chart and you don't see any changes the problem might not actually be 60
with our code. 61
The problem might be with our visualization. 62
So I've chosen a black color for the dots and we've got quite a dark chart. 63
So let's change the color and also the transparency of our chart. 64
So I'll set the transparency of the plot surface with an alpha value having a pass and an alpha argument 65
of zero point four. 66
Now let me hit shift enter and see if we can track our gradient descent algorithm. 67
Now 68
ah now this is already much more clear. 69
Brilliant. 70
Now given that this is our crowning achievement for this lesson I think we're gonna have to style our 71
chant in a different way. 72
So I'm gonna go with a psychedelic theme by changing the color map from hot to Rainbow in honor of the 73
final race track in Mario Kart and take a look at our wonderful rainbow colored half pipe here. 74
Brilliant. 75
So we can clearly see our step size which is initially large as we're far away from the minimum decrease 76
as we're getting closer and closer. 77
And the interesting thing about our gradient descent here is that we seem to be reaching the bottom 78
of this half pipe but there's still some way to go. 79
The only thing is that the slope is so shallow that all the steps at this point are very very small. 80
So even though we progressed very very quickly to this point down here from there on we have to take 81
many many steps to get to the actual minimum. 82
Now all of this was a very very technical section. 83
We've covered quite a lot of mathematical topics and we've really peered under the hood of what goes 84
on in a machine learning algorithm. 85
We've looked at various different cost functions and we've reviewed some calculus and we've played with 86
an optimization algorithm by turning various knobs including the learning rates. 87
The starting point. 88
And finally even the cost function. 89
So where do we go from here. 90
Where do we go next. 91
In the next section we're going to return to do something a bit more practical. 92
We're going to look at a house price data set for the city of Boston in the United States and we're 93
going to get into the business of predicting how much a house is worth based on its characteristics 94
and the technique we're going to use. 95
It's called multi variable regression. 96
So congratulations for making it this far. 97
I hope you're ready for tackling the challenges coming up in the next lessons. 98
Looking out the window I can see that the almost never ending London drizzle seems to have stopped. 99
So I think I'm going to take this opportunity to pop out to the shops and restock the coffee that's 100
slowly running out here. 101
See you soon.