So in our loop, where we're running our gradient descent, we need to add one line. I'm going to add the line that 1
updates the values array after we've updated the parameters. 2
So, I'm going to save values_array is going to be equal to np.append, I'm going to supply 3
three inputs to these parentheses. 4
I'm going to supply the array itself and that's going to be the values_array. I'm not going to 5
supply the arguments by keywords here, just to stay on the same line and make this code a little bit 6
shorter. Then the values that we're going to append are gonna be our params - 7
Yeah, our params array. 8
But before we append them, we're gonna have to reshape them because we can't append a one dimensional 9
array to a two dimensional array. 10
So I'm going to reshape the parameters to be one row and two columns. And finally I'm going to say axis is equal 11
to 0 meaning I want to append my new values as a row. And that's it. 12
That's the values array updating every time the loop is running. 13
Now what we can do is we can scroll back up to where we've initially graphed our bowl shape. 14
I'm going to take this cell here. 15
I'm going to copy it and then down here for our 3D gradient descent, 16
I'm going to paste it below this cell. And then I'm going to add a single line of code here, 17
I'm going to add my scatter plot. 18
So this is gonna be ax.scatter, and then I have to supply X values, 19
it's not gonna be x_4, but it's gonna be the x values from our values array. 20
So it's gonna be values_array[], all the x values I can select with a colon, then 21
a comma and then a 0 because they're in the first column right. 22
So all the rows and the first column - "values_array 23
[:,0]" 24
Now I've got to add the y values. The y values in our array where in column two. 25
So we can take our "values_array[ 26
:,1]" 27
This is selecting all the values in column number two. 28
And now we have to provide the z values and we can do this by calculating our cost for all our values. 29
So it was f of and there would have been our x and y values, but these are captured 30
and here, except that this was column one and this is column zero. So that's really it. 31
That's that's all we need for the scatter plot. 32
But that said in terms of design I think we should make the dots a bit larger, 33
setting the size equal to 50 and the color equal to red. 34
So I'll have it just like this and because the line is quite long, 35
I'll just hit Enter here and bring it down like this, 36
so it's a bit more readable and we have the line wrapping over two lines instead. Okay, 37
now all I have to do is run our cells. 38
So I'm going to run this one which will generate our values array, and then I'm going to run this one here 39
which generates our plot with the values being plotted all the way down starting here and moving all 40
the way down to the bottom of this bowl. And that's it, we've plotted our gradient descent. If you want 41
to you can obviously come back up here and pick a different number of iterations, say you might only 42
want to see the first 100 or so, and of course you can also start with the different starting points 43
to see what those are like. 44
If I run it with 100 iterations then I'm not going to be reaching the bottom of the bowl, 45
I'm going to be stopping a little bit short of it. 46
And if instead I only have 50 iterations, 47
then you see that the first 50 because the slope is so flat up here, the adjustments are very, very small 48
and we only gradually make our way from the edge of the bowl here. So you can play around with this and 49
see how our gradient descent behaves when you have different starting positions and different number 50
of iterations. 51
And finally, I want to show you one last thing. 52
So it turns out that np append isn't the only method that we can call to add our values to the bottom 53
of this values array. 54
There is another method called concatenate that we could use instead. 55
Let me show you how that works, 56
so that you've seen both and are familiar with both ways of adding data to an array or appending data 57
from two different arrays to each other. 58
I'm going to comment out this line of code here so that we don't do the operation twice, because otherwise 59
we're gonna get some very funky results. 60
So again we're going to start with our values array, and we're gonna set it equal to "np. 61
concatenate". 62
So the results from the concatenate method are going to be stored in our values_array. 63
Now the concatenate method only really takes two inputs the first one is going gonna be the two arrays 64
that need to be added together, that need to be strung together - concatenated. 65
And the second again is the axis. 66
So if you are adding it as a row or as a column. Now the axis again is going to be 0. 67
And for the arrays, we're gonna provide that information as a tuple - hence the parentheses here. 68
So if we wanted to accomplish exactly the same thing as the commented out line above, we have to provide 69
our values_array and then our parameter array, but we'll have to reshape it all the same. 70
So "reshape(1, 2)", and that's it. 71
This line of code here will accomplish exactly the same thing as above, it's just two different functions 72
you can call from numpy to run this gradient descent and then graph the results later. 73
And just to prove that I'm not making this up, I'm going to change this, 74
I'm going to change the maximum number of iterations to say 200, refresh the cell and then refresh 75
my graph as well. 76
Voila! There you go. In our next lesson we're gonna be looking at how to run our gradient descent when 77
we have data points. 78
Yeah. 79
So this is the case where we've got a little bit more of a realistic cost function and we've got a bunch 80
of data points that we've collected and we're looking to calculate the best fit line. 81
I'll see you there.