0 1 00:00:00,840 --> 00:00:07,650 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 2 00:00:07,710 --> 00:00:12,320 updates the values array after we've updated the parameters. 2 3 00:00:12,450 --> 00:00:22,500 So, I'm going to save values_array is going to be equal to np.append, I'm going to supply 3 4 00:00:22,620 --> 00:00:25,020 three inputs to these parentheses. 4 5 00:00:25,020 --> 00:00:30,870 I'm going to supply the array itself and that's going to be the values_array. I'm not going to 5 6 00:00:30,870 --> 00:00:35,700 supply the arguments by keywords here, just to stay on the same line and make this code a little bit 6 7 00:00:35,700 --> 00:00:41,880 shorter. Then the values that we're going to append are gonna be our params - 7 8 00:00:41,910 --> 00:00:43,490 Yeah, our params array. 8 9 00:00:43,740 --> 00:00:49,680 But before we append them, we're gonna have to reshape them because we can't append a one dimensional 9 10 00:00:49,680 --> 00:00:52,050 array to a two dimensional array. 10 11 00:00:52,080 --> 00:01:03,480 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 12 00:01:03,480 --> 00:01:10,290 to 0 meaning I want to append my new values as a row. And that's it. 12 13 00:01:10,350 --> 00:01:16,490 That's the values array updating every time the loop is running. 13 14 00:01:16,510 --> 00:01:24,230 Now what we can do is we can scroll back up to where we've initially graphed our bowl shape. 14 15 00:01:24,310 --> 00:01:26,620 I'm going to take this cell here. 15 16 00:01:26,620 --> 00:01:30,700 I'm going to copy it and then down here for our 3D gradient descent, 16 17 00:01:30,720 --> 00:01:37,030 I'm going to paste it below this cell. And then I'm going to add a single line of code here, 17 18 00:01:37,070 --> 00:01:39,300 I'm going to add my scatter plot. 18 19 00:01:39,470 --> 00:01:46,830 So this is gonna be ax.scatter, and then I have to supply X values, 19 20 00:01:46,880 --> 00:01:53,310 it's not gonna be x_4, but it's gonna be the x values from our values array. 20 21 00:01:53,480 --> 00:02:02,620 So it's gonna be values_array[], all the x values I can select with a colon, then 21 22 00:02:02,620 --> 00:02:08,190 a comma and then a 0 because they're in the first column right. 22 23 00:02:08,200 --> 00:02:12,390 So all the rows and the first column - "values_array 23 24 00:02:12,490 --> 00:02:16,610 [:,0]" 24 25 00:02:16,630 --> 00:02:23,770 Now I've got to add the y values. The y values in our array where in column two. 25 26 00:02:23,950 --> 00:02:29,140 So we can take our "values_array[ 26 27 00:02:29,140 --> 00:02:32,040 :,1]" 27 28 00:02:32,260 --> 00:02:36,790 This is selecting all the values in column number two. 28 29 00:02:36,790 --> 00:02:43,930 And now we have to provide the z values and we can do this by calculating our cost for all our values. 29 30 00:02:43,930 --> 00:02:50,920 So it was f of and there would have been our x and y values, but these are captured 30 31 00:02:56,220 --> 00:03:04,750 and here, except that this was column one and this is column zero. So that's really it. 31 32 00:03:04,760 --> 00:03:08,490 That's that's all we need for the scatter plot. 32 33 00:03:08,680 --> 00:03:13,270 But that said in terms of design I think we should make the dots a bit larger, 33 34 00:03:13,270 --> 00:03:18,860 setting the size equal to 50 and the color equal to red. 34 35 00:03:19,060 --> 00:03:24,190 So I'll have it just like this and because the line is quite long, 35 36 00:03:24,190 --> 00:03:27,550 I'll just hit Enter here and bring it down like this, 36 37 00:03:27,550 --> 00:03:34,260 so it's a bit more readable and we have the line wrapping over two lines instead. Okay, 37 38 00:03:34,290 --> 00:03:37,530 now all I have to do is run our cells. 38 39 00:03:37,620 --> 00:03:44,430 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 40 00:03:44,820 --> 00:03:52,280 which generates our plot with the values being plotted all the way down starting here and moving all 40 41 00:03:52,350 --> 00:03:59,660 the way down to the bottom of this bowl. And that's it, we've plotted our gradient descent. If you want 41 42 00:03:59,660 --> 00:04:07,340 to you can obviously come back up here and pick a different number of iterations, say you might only 42 43 00:04:07,340 --> 00:04:13,970 want to see the first 100 or so, and of course you can also start with the different starting points 43 44 00:04:14,180 --> 00:04:15,760 to see what those are like. 44 45 00:04:16,100 --> 00:04:22,620 If I run it with 100 iterations then I'm not going to be reaching the bottom of the bowl, 45 46 00:04:23,000 --> 00:04:25,270 I'm going to be stopping a little bit short of it. 46 47 00:04:26,080 --> 00:04:28,490 And if instead I only have 50 iterations, 47 48 00:04:33,200 --> 00:04:39,440 then you see that the first 50 because the slope is so flat up here, the adjustments are very, very small 48 49 00:04:39,820 --> 00:04:45,800 and we only gradually make our way from the edge of the bowl here. So you can play around with this and 49 50 00:04:46,010 --> 00:04:51,800 see how our gradient descent behaves when you have different starting positions and different number 50 51 00:04:51,800 --> 00:04:53,820 of iterations. 51 52 00:04:53,840 --> 00:04:57,160 And finally, I want to show you one last thing. 52 53 00:04:57,170 --> 00:05:06,170 So it turns out that np append isn't the only method that we can call to add our values to the bottom 53 54 00:05:06,530 --> 00:05:08,210 of this values array. 54 55 00:05:08,210 --> 00:05:13,080 There is another method called concatenate that we could use instead. 55 56 00:05:13,100 --> 00:05:14,400 Let me show you how that works, 56 57 00:05:14,420 --> 00:05:21,650 so that you've seen both and are familiar with both ways of adding data to an array or appending data 57 58 00:05:21,950 --> 00:05:24,580 from two different arrays to each other. 58 59 00:05:24,620 --> 00:05:32,900 I'm going to comment out this line of code here so that we don't do the operation twice, because otherwise 59 60 00:05:32,900 --> 00:05:35,430 we're gonna get some very funky results. 60 61 00:05:35,480 --> 00:05:42,680 So again we're going to start with our values array, and we're gonna set it equal to "np. 61 62 00:05:43,460 --> 00:05:45,080 concatenate". 62 63 00:05:45,170 --> 00:05:53,000 So the results from the concatenate method are going to be stored in our values_array. 63 64 00:05:53,060 --> 00:06:01,400 Now the concatenate method only really takes two inputs the first one is going gonna be the two arrays 64 65 00:06:01,670 --> 00:06:07,530 that need to be added together, that need to be strung together - concatenated. 65 66 00:06:07,540 --> 00:06:09,880 And the second again is the axis. 66 67 00:06:10,130 --> 00:06:17,270 So if you are adding it as a row or as a column. Now the axis again is going to be 0. 67 68 00:06:17,390 --> 00:06:25,500 And for the arrays, we're gonna provide that information as a tuple - hence the parentheses here. 68 69 00:06:25,520 --> 00:06:31,520 So if we wanted to accomplish exactly the same thing as the commented out line above, we have to provide 69 70 00:06:31,550 --> 00:06:40,870 our values_array and then our parameter array, but we'll have to reshape it all the same. 70 71 00:06:41,210 --> 00:06:46,740 So "reshape(1, 2)", and that's it. 71 72 00:06:46,910 --> 00:06:54,020 This line of code here will accomplish exactly the same thing as above, it's just two different functions 72 73 00:06:54,020 --> 00:06:59,650 you can call from numpy to run this gradient descent and then graph the results later. 73 74 00:06:59,760 --> 00:07:03,320 And just to prove that I'm not making this up, I'm going to change this, 74 75 00:07:03,320 --> 00:07:09,770 I'm going to change the maximum number of iterations to say 200, refresh the cell and then refresh 75 76 00:07:09,950 --> 00:07:12,940 my graph as well. 76 77 00:07:12,940 --> 00:07:19,820 Voila! There you go. In our next lesson we're gonna be looking at how to run our gradient descent when 77 78 00:07:19,820 --> 00:07:22,030 we have data points. 78 79 00:07:22,030 --> 00:07:22,250 Yeah. 79 80 00:07:22,610 --> 00:07:28,790 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 81 00:07:28,790 --> 00:07:35,520 of data points that we've collected and we're looking to calculate the best fit line. 81 82 00:07:35,630 --> 00:07:36,410 I'll see you there.