Now suppose we want to find the value of x that minimizes the cost - that minimizes f(x). So on the 1
graph, 2
we can already see that that point would be around here. 3
But let's think about this problem a bit more deeply. 4
Right? 5
So if X was equal to 2, if we were here on the graph, then the cost would be decreasing a lot as we're 6
moving down. 7
But when X is equal to 1, then that costs still decreasing, 8
but by not as much because the line just isn't as steep. 9
Right? 10
And by the same token when X is equal to 0, 11
that line is like flattening out even more. 12
So at this point the cost is only decreasing very, very slightly. 13
Then we'd reach our minimum and then the cost starts increasing again, 14
right, as we move up the curve. 15
So in other words, the slope or the steepness of this function, 16
yeah, is telling us when we've reached our minimum cost because that slope is the rate of change. 17
And when this slope stops changing, 18
well, on this graph we know that the cost is equal to the lowest value. 19
In other words the slope of this function is kind of equal to 0 at the point where the cost is the 20
lowest. 21
Now you may or may not remember from school how to find the slope of a function, 22
depends how long it's been, right? 23
But, as a reminder, the slope of a function is given by that functions derivative. 24
So let's add this section heading, down in the next cell. 25
I'm going to go to "Cell" > "Cell Type" > "Markdown", put two hash tags there, and then write "Slope & Derivatives" 26
because this is what this lesson is gonna be about. Now as a challenge, 27
can you create a Python function for the derivative of f(x) and call that function df(x). 28
I'm going to scroll back up for a second here. 29
So remember f(x) was equal to x^2+x+1. 30
I'll give you a few seconds to figure this out. 31
All right so here's the solution. 32
Yeah, if you were scratching your head at this point because your calculus is a little bit rusty, let 33
me show you what the solution is and also how we arrived at it. 34
So I'm going to define a function with the def keyword, then going to call it "df" and have one input, namely 35
x, colon, new line, "return 2*x+1", and that's it. 36
So how did I arrive at this? 37
How did I know it was gonna be 2x+1? So the answer is I used calculus. Now calculus and derivatives 38
are a pretty big topic but there's a couple of mathematical tricks or shortcuts that we can use to calculate 39
derivatives very, very quickly and one of these tricks is called the Power Rule and we'll be applying 40
it in several of the upcoming examples. 41
So here's a very quick reminder on how it works, if you have a function x^n, then the 42
derivative of this function is n * x ^ (n-1) 43
So that's a pretty simple rule, right? 44
Easy to remember. But before we move on let me give you a couple of quick examples. 45
So, say we have a cost function that is x^5 and we want to calculate this cost function's 46
derivative. 47
If we apply the power rule then we simply get 5*x^(5-1), which is 48
5*x^4. 49
And that's all there is to it. 50
That's the function of the derivative. 51
And here's another very, very quick example - we can apply the power rule to this function as well. 52
x^3+5*x^2+7. 53
So I'm going to go through these terms one by one - x^3 is pretty easy right? 54
Because x^3 will simply be 3*x(3-1) and then for that second 55
term, 5x^2 the five stays where it is and we have 2*x^2-1. 56
And then that 7 right - that 7 just disappears. 57
That 7 just drops because 7 is a constant. 58
It doesn't depend on X - constants don't change, and since a derivative is all about the rate of change 59
the constant drops out. 60
It's equal to 0. 61
So the 7 disappears. 62
So what we end up with is 3*x^2 63
+ 5*2*x, right, 10*x, and that's it. 64
Right. 65
That's the power rule for derivatives in a nutshell. So back in Jupyter notebook, 66
say we want a graph our derivative function that we've just worked out here. 67
We've worked hard to create it. 68
So let's visualize it side by side with our original cost function. 69
So what we want is we want to charts, side by side, and to do that we're going to have to use the so-called 70
subplot functionality from matplotlib. 71
So I'm gonna show you the subplot functionality and I'm also gonna show you how to size the two charts. 72
So this is something new. 73
This is something we haven't done before, but since a lot of the code will be very, very similar to what 74
we've had up here I'm just gonna very, very quickly go to "Edit" > "Copy Cell" and then go down here 75
and I'm going to go to "Edit" > "Paste Cell Above", so I can reuse some of this code that we've got here. And 76
I'm going to change my comment here and I say "Plot function and derivative side by side". 77
So this is what we're gonna do here, and uh, in order to size our plot, 78
yeah, 79
in order to size it we're gonna have to set the figure size of our plot. 80
So we're gonna write "plt.figure()", and then we're gonna say "figsize=" 81
and then we have to give it a width and height. 82
So let's see what happens if I put in, I don't know, "10,5" in the square brackets and hit Shift+ 83
Enter, so you can see now that the width is 10 and the height is 5. 84
I looked at the documentation for this and this is actually measured in inches of all things. 85
I don't really know. 86
I don't really know why that is but I hope it makes our American friends happy. 87
Now I'm going to modify this code a little bit more, so in order to have two plots side by side we're 88
gonna have to use this subplot functionality from matplotlib, so I'm going to use "plt.subplot" and then 89
I have to specify three numbers - a row, a column and an index. 90
So in this case I'm going to have two plots, side by side. 91
So they're going to be in the same row. 92
So one row is going to be two columns. 93
And the first plot, this cost function here is gonna be, at index 1, is gonna be my first plot. And now 94
it's time to add our second plot. 95
So, I'm going to say "plt.subplot" 96
And for the second plot it's gonna be still in the same row - row number one, still two columns, but this 97
one is gonna be at index 2. 98
So this is gonna be my second plot. 99
Now I haven't added it yet, I'm going to do that 100
now. To graph our derivative, I'm going to write "plt.plot" and we're going to feed in our 101
x_1 values and then we're gonna call our function. 102
Right. 103
So the first one was f(x_1) and this one is gonna be df(x_1). Let's see what happens 104
now. I'm going to press Shift+Enter. 105
Huh. 106
So I've got two plots 107
now. You see this? Two plots side by side and they're still occupying the same space as before, right. 108
The overall figure is still 10 by 5. 109
So we can see here the figure size hasn't changed, but now it's split with two plots, two subplots taking 110
up that same space. 111
So I can probably make use of this space here on the right a little bit better by maybe making this 112
15 by 5. 113
I think that should be about right. 114
Press Shift+Enter. 115
I get it like this. 116
Very nice. 117
Now what I'm going to do is add a few labels and style these graphs a little bit better so that if you 118
ever come back to this notebook in the future, you can make sense of these charts very quickly and very 119
easily. 120
So, I'm going to insert two comments first, right. 121
So this is the first chart and this is the cost function and down here is the second chart 122
of the derivative. 123
For the first chart let's explicitly state that it should be blue and that it should have a line width 124
of maybe 3 and for our second chart we're gonna give it the color sky blue and a line width of 125
5. 126
How does that look? 127
Pretty good. 128
Now let's add some title and X labels and Y labels to that second chart as well. 129
So I'm just gonna copy this code here and add it here but instead of having it say "Cost function", I'm 130
going to say "Slope of the Cost function" and on the x is still x, but on the y it's gonna be "df(x)" 131
and then I'm also gonna set the axes, so again I'm going to copy xlim and ylim and add this down here, 132
but I'm gonna have it go from maybe -2 to 3, and on the Y from say, 3 to 6. 133
Oh sorry, -3 to 6. 134
I'm going to hit Shift+Enter to see what it looks like. 135
So this is quite good. 136
This is very, very nice and clear but there's one small improvement that I'd really like to make. 137
We're interested primarily in where this minimum is here and one of the things that be quite nice to 138
see on this chart is where our slope, our sky blue line here, actually intersects the axis here. 139
So what we can do is we can style our chart to have a grid. 140
This is fairly easy to do so for our second chart for our derivative all we have to do is add "plt. 141
grid" and then two parentheses for the method name. 142
No need to add any arguments here. 143
Hit Shift+Enter and then you can see matplotlib applies a grid automatically to this subplot and now 144
it's very, very nice to see that our intersection of the slope of the derivative is going to be probably 145
somewhere at the midpoint between -1 and 0, so that's gonna be around here somewhere. 146
That seems about right. 147
So as a quick summary we've covered a three new things for styling graphs and charts. 148
One of them is this idea of sizing them. 149
The second thing is this idea of having plots side by side and that requires the subplot function which 150
needs to know how many rows, how many columns and what the index is. 151
And then also we've used the grid function for the first time to superimpose a grid on to the plot. 152
Now as a challenge, can you instead of having these plots side by side, stack them from top to bottom? 153
What would you have to change in the Python code in order to achieve this? In order to arrange the plots 154
in a different way. 155
I'll give you a few seconds to pause the video and try it out. Did you solve it? 156
Did you manage to get it? 157
Here's the solution. 158
The key on arranging the charts is gonna be where our subplots are, right? 159
So if you want to have them top to bottom, then you'd have to have two rows and one column and I'd have 160
to change this for both subplots. 161
So for the first chart and for the second chart I'm going to have to go 2 and 1 instead of 1 and 2. If 162
I press Shift+Enter now, we can see our charts arrange themselves from top to bottom. But one of the reasons 163
why they look terrible like this is because we've constrained our figure size to be still using the 164
same dimensions - 15 by 5, right? 165
So it's stretching out the charts across the whole space that it's allotted. 166
And that's why they look like this. 167
But if we changed our figure size with some new constraints say 5 and 15, right, then pressing Shift+Enter 168
will give us the charts like this. 169
In this case they're more tall than wide. 170
Now I quite like the original setup so I'm going to change this back to 15 and 5 and I'm going to go 171
with one row, two columns for my subplots - one row and two columns for both of them. 172
So this is how I would prefer to look at these two charts. 173
But yeah, if you figure this out super handy for being able to arrange the charts in your Jupyter notebook. 174
All right. 175
So I hope you're ready for the next lesson. 176
I'll see 177
you there. 178
Take care.