All right. 1
So, if you haven't fired up to put a notebook in a while then open your Anaconda command line 2
if you're in Windows, or go to terminal if you're on Mac and then type in the command "jupyter notebook" 3
and hit Enter. Your browser should fire up and drop you off on local host with your folders. Here, 4
you want to navigate to the MLProjects folder that you've set up earlier and there you want to create 5
a new Python 3 notebook. 6
And, 7
we're gonna click up here what says Untitled. 8
I'm gonna rename it to "0 3 9
Gradient, 10
(oh man I can't type), gradient descent." 11
We're gonna hit "Rename" and now we're ready to go. For our first cell in our new notebook, 12
we're not gonna be typing any Python code. 13
Instead we're going to be making our notebook a little bit more pretty, a little bit more readable by 14
inserting a heading. 15
So what I'm going to do is I'm going to go to "Cell" and then change the "Cell Type" from "Code" to "Markdown". 16
So select "Markdown" here and you'll notice that the "in" here has disappeared because this cell will 17
now no longer be evaluated as code. 18
So we said we're gonna make a title. 19
Our first title is gonna be "Notebook Imports and Packages". 20
And, we hit Shift+Enter. So now we can see here this is just plain text. And in the cell below 21
we're gonna be adding our import statements. 22
So the first thing that we're gonna import is our old friend matplotlib pyplot - we're going to be doing 23
some plotting in this notebook. 24
So I'm going to write "import matplotlib.pyplot as plt" - I'm going to stick with the same naming convention 25
here. 26
And we're also going to import another library, another package, called numpy. 27
And this is commonly referred to as "np". 28
So a lot of people use np and we'll use that as well. 29
So I'm going to write "import numpy as np" and we're also going to add the good ole Jupyter notebook statement 30
"% 31
matplotlib 32
inline". 33
So I'm going to be doing some plotting, so we're gonna add this matplotlib inline statement so that we can 34
export our plots very, very nicely. I'm going to hit Shift+Enter now and before we go on I want to revert back 35
to this markdown up here. 36
I'd really like to have a section heading, and let me show you how we can get that, because "Notebook Imports 37
and Packages" is very, very small. 38
If we put a hashtag in front of this and hit Space then immediately the font becomes a lot bigger, bolder 39
and blue and this is because this hashtag is telling Jupyter that now "Notebook Imports and Packages" should 40
be considered as a heading. It's not actually gonna look like this when I hit Shift+Enter, it's going 41
to look like this - it's gonna be big and bold. 42
So this is a very nice way to create section headings in your notebook. 43
Now if you wanted this a little smaller and one level down in the size and boldness then you can put 44
two hashtags and you can see how the markdown adjusts really nicely and will look like this in contrast. 45
Right. 46
And you can even try three. 47
Right. 48
So there's different levels of boldness and size that you can you can play with for your section headings 49
to keep your notebook organized. 50
So I go with one, and this way we can find our imports very quickly. 51
Now we're gonna dive straight into our first example. 52
Now the thing I'm going to do though is, I'm going to take this cell here and I'm going to modify it to 53
be a markdown cell as well. 54
So this is my chance to share a little bit about what Jupyter notebook can do. So I'm going to say "Example 1", 55
"Example 1" and example 1 is going to be about a very, very, very simple cost function and the cost 56
function is going to look like this. 57
It's going to be f(x) = x^2 + x + 1. 58
Let's see what this markdown would actually look like. 59
Would look something like this but, um, the cool thing about markdown is that you can actually make the 60
mathematical notation look a lot better. 61
So you can add a dollar sign in the front and you can add a dollar sign in the back. 62
And if I press Shift+Enter now, then you can see that indeed we have this formatting here in the markdown 63
cell looking like an actual equation. 64
So this is gonna be Example 1 is 65
going to look like this. 66
I can make this bigger as well so I can give it a section heading if you will. 67
And now it's going to look like this so this is gonna be a simple cost function. 68
All right. 69
Here we go. 70
Now, 71
if this is the first time that you're seeing this then you might be confused about what are these dollar 72
symbols, right? 73
Well this is a markup notation from a system called LaTeX and LaTeX uses tags like these dollar 74
signs to mark a particular section as a mathematical notation. If I double up on the dollar 75
signs, then I'm giving it a different kind of tag. 76
And you can see that you'll have a different formatting as well. 77
So in this case it's centered, so single dollar sign is in line 78
and the other tag the double dollar sign is for display. 79
So we're gonna be using a LaTeX a little more in this module and 80
you might actually see this in in many, many other places as well - it's super popular for writing 81
mathematical equations or scientific papers especially in academia. 82
The best analogy that I can think of of how LaTeX works is that it works really similar to XML 83
or HTML. 84
So if you were to go to a Web site like say example.com and you right click on it and you go to 85
"View Page Source", then you'll see the HTML documents. 86
So while the website will look like this - "Example 87
Domain" is actually surrounded by two HTML tags - title and title, 88
so a beginning tag and an end tag. And LaTeX sort of works a bit like this as well. 89
You've got a markup, meaning these tags, that kind of give structure to your document. 90
So it's through these tags that the Jupyter notebook knows how to format a particular section of text 91
in the in the markup cells. Now that we've added the markup and our section heading, 92
we can now actually, you know, write the Python code for this function and it would look like this "def 93
f(x):", new Line, "return x", double multiplication sign for the power, 94
so "**2+x+1" . 95
And that's our function. 96
Right? 97
This is our function in Python code. 98
Now what we're going to do is we're going to generate the data. 99
I'm just going to add a little comment here "Make Data" and the way we're going to do this is by using num- 100
py. 101
So our data is going to sit in a variable, I'm going to call it x_1 - "_1" 102
because this is our first example, so x_1 is gonna be equal to something and it's 103
gonna get its value from a numpy function - numpy was np and the function we're gonna call is "lin- 104
space". 105
Yeah. 106
"np.linspace(start = - 3, stop = 3)" 107
and then "num" is gonna equal, say, 100. I'm going to hit Shift+Enter and explain what I did just there. 108
So the linspace function is something that comes from the numpy library - I'm going to pull up the 109
documentation for you guys and I've got a couple of arguments that I gave it - I gave it a start value, 110
a stop value and a value for this third one called num. Now linspace creates a sequence, a sequence 111
of numbers that creates our data for us and it creates a sequence of numbers between the start value 112
and the stop value and the number of samples is set by that third parameter so that third value, the num 113
value. Back in our Python notebook we can actually take a look at what this would look like 114
so if I put x_1 here and hit Shift+Enter then we can see what it is that we've actually got 115
back; we've got back an array that starts at -3 and goes to 3 and has a hundred individual 116
data points - 117
one hundred individual values that are spaced out equally between -3 and 3. 118
If this, you know, was I don't know the number 10 instead of 100 then I'd get an array with much fewer 119
values, right? 120
Just 1,2,3,4,5,6,7,8,9,10. 121
Right? 122
So you can see this linspace function here is very, very handy for generating data. 123
And one of the things I quite like doing is actually adding the names for the arguments 124
when I make a call to my function because I just find it's so much more readable than having it like 125
-3, 3, 10. 126
Right? 127
That's much harder to read, especially when you're coming back to it and you might 128
not remember it so well. 129
So x_1 is an array with approximately 100 values. 130
We can even give it more, we can give it like say 500. 131
Right? 132
I'm going to hit Shift+Enter 133
And that's us having made our data. Now that we've got our data, 134
let's plot it, right? 135
Let's graph it using our function, so we can add the plot by using our matplotlib - plt, so we can actually 136
do this in one line. 137
Right? 138
We can say "plt.plot", and then "(x_1, )", and then what, 139
f(x_1)" - we're going to feed the actual data that we have in x_1 into our 140
function here and we are going to plot f(x_1) - this is our y right. 141
And x_1 - this is our x. 142
So let's call plt.show(). 143
See what happens. 144
And here it is. 145
This is what our function actually looks like - it's a parabola. I'm going to actually center this a little bit as well, 146
so I'm going to set the axes by writing "plt.xlim()", and I'm going to give it a range of 147
-3 148
uh, to, I don't know, 3. 149
Right. 150
This is what we generated our data from, right - from -3 to 3 and the y axis, 151
we can set with "plt.ylim(0,8)" 152
See what we get. 153
This is starting to look a little bit better. 154
I'm going add two labels as well with "plt.xlabel()" and then the string 'X', I'm going to give it a font size of 155
16. And for the y axis. 156
we're gonna just write f(x), so "plt.ylabel()" and then I'm going to give it the string 157
"f(x)", font size is also equal to 16. 158
There we go. 159
All right so that's the plot of our function. 160
That's pretty good, right? 161
So we've got a cost function - 162
sn example of one: x^2+x+1. 163
And the way we've kind of broken this down is we've created a function, a Python function, right? 164
Also called it f(x) - less confusing. 165
We've created some data, and the reason we had to do this was so that we could generate a nice graph, 166
right? 167
So x_1 used numpy's linspace and then we've just used matplotlib again to graph our 168
data here. In the next lesson, 169
we're gonna set the stage for minimizing our cost. If f(x) is our cost, 170
then the lowest cost will be at the bottom of this graph. 171
Right? 172
So it'll be somewhere around here. 173
All we have to do now is find out what's the lowest cost and for what value of x is our cost 174
the lowest.