In this lecture, I'm going to talk about Jupyter Notebook.
Occasionally, I get this strange question from students asking why I don't do things in Jupyter notebook.
Let me explain why I think this is strange.
Firstly, it's my position that it's completely unnecessary and actually doesn't change anything to
use Jupyter Notebook or not.
Let me repeat that using Jupyter Notebook is exactly the same as not using Jupyter Notebook.
There's no difference other than the fact that it looks different.
In other words, the screen is a different color.
Obviously, it's such a difference is trivial.
In this lecture, I'm going to demonstrate how that's the case.
One major reason I dislike Jupyter Notebook is because it causes too many students to be unaware of
how Python really works.
If you're only comfortable inside notebook and when you see Python code in a text file or anywhere else
and you get scared or intimidated, that's not good.
That Python code that's in the text file is exactly the same as what would be in the notebook.
There's really nothing scary about it.
Programmers working at real jobs eventually need to write code that's going to be deployed and run automatically.
In other words, your final code is going to sit in a Python file that runs by itself without Jupyter
notebook.
So if you're going to have any hope of using these skills in a real job, you had better be comfortable
with writing Python code outside of Jupiter notebook.
You would also better be aware that there's actually zero difference between writing code in a Jupiter
notebook and writing code in Python or the console.
Here's one example I like of how you might use Python in the quote unquote real world.
Let's say you write a script that emails your boss to tell him you're going to be late for work.
And let's say you don't actually want to send this email manually, but you want it to get sent automatically
every Friday morning so that your boss doesn't yell at you for coming in to work late after you party
too hard Thursday night?
Well, that's very simple.
All I have to do is on my server create what's called a crontab in it.
I just enter the code for when I want this command to run and then to the right of that, I specify
the command that I want to run.
That's just Python space.
And then the script name, as you can see, it's simply how you would run this Python scripts from the
command line.
And now, every Friday at 9:45 a.m., this script is going to send the same email to your boss.
Tell him you'll be late.
Well, let's not get off track here.
The point of this is you really don't want to be using Jupyter Notebook for something like this.
I think one perceived advantage of Jupiter notebook is that you can see the results of intermedia calculations.
However, this is merely a perceived advantage and not a real advantage because you can do the exact
same thing even when you're not inside notebook.
Firstly, as I'm sure you've seen by now, I Python also prints out the results after you enter a command.
I Python is called a reptile, which stands for Read Avow Print Loop, and that's generally what they
all do.
No matter what language you're in.
So whether that's Python, Ruby or any other language, the key word here is print.
Why is that?
Well, one of my golden rules for writing and debugging code is when in doubt, print it out.
I can't tell you how many times I've gotten a question on the Q&A when it could have been easily answered
by inserting a print statement into the existing code.
Anyway, what's the point of this long discussion about printing things out?
Well, it's that if you think Jupyter Notebook is the only program that helps you do this, you need
to expand your horizons a little bit.
You should, in fact, always be doing this if you're not using an abundant amount of print statements
while quoting, you are not doing it right.
Remember, programming is not philosophy.
You're not supposed to be running a program in your head.
That's like trying to do long division in your head when you have a calculator in your hand.
So just by printing things out, you can be more efficient.
Stop trying to guess what a program will do and just let the program itself tell you what it's doing.
The key takeaway here is you should always be printing things out.
The fact that Jupyter Notebook shows you the result of each block of code is not simply a happy surprise.
But another important lesson here is that if you want to use Jupyter Notebook, there's absolutely nothing
stopping you from doing so.
In other words, using Jupyter Notebook is 100 percent compatible with everything we are already doing.
In fact, if you recall, your goal in these courses is not to run my code, but to write your own code.
And of course, since it's your code, you can write it however you want, including Jupyter Notebook
and the rest of his lecture.
I'm going to prove to you that you can take any scripts from our course repository, which we know runs
in the console because that's how I always demonstrate it and show you that this exact same code runs
in Jupyter Notebook.
Let's begin.
OK, so let's say I'm in the folder numpy class, and I'm interested in the code inside classification
example that PI, as you can see, what I have right now is this code inside a text editor.
Now, if you're not aware of what a text editor is, it's just a program that shows you the contents
of a text file and lets you edit those contents.
It's the ideal program for writing code.
Now, occasionally, if you're writing in a language like Java or Swift, you might want to use an I.D..
But even then, it's totally optional.
These days, I prefer to write Java in a plain text editor like sublime text as well.
In any case, normally one does not need to use an I.D. for writing Python code.
Now about Jupyter Notebook.
Well, let's start up a Jupyter notebook, so I'm going to go Jupyter Notebook.
All right, so now I've got Jupyter notebook running, so I'm going to start a new notebook.
Also, notice that we started the notebook in the same directory as the relevant Python file, so that's
something you want to keep in mind for the future.
Now, at this point, what I'm going to do is I'm going to prove that everything in this Python file
works exactly the same in the notebook as it does in the console.
So let's start with the imports.
Let's grab this one to.
All right.
Paste that in.
Run it.
So everything's fine so far.
Now let's load in the data.
OK, we've loaded in the data.
Now, I'm not sure what its type is, so I can check it by using the tight function.
So let me try that.
Cool, so I get S.K. learned utils bunch, so that's the type of the variable data.
Now, previously you recall that we did this example in Python, but as you can see, the result is
the same in Jupyter notebook.
Alternatively, if you wanted to run this Python file in the console, so you wanted to type.
Let's say.
So let's say you wanted to type in Python classification, example that PI, then you could just add
some print statements if you wanted to show those same lines while this file was running.
Now, I'm not going to go through such elaborate detail for the rest of this example, since you've
already seen it.
So let's just get through the rest.
Line by line.
Let's say I want to check out the keys in the data variable.
OK.
Looks good so far.
Let's check the shape of the data attribute.
OK.
Looks good so far.
Check the target's.
OK.
Still, what we expect the target names looks good.
Target shape, it should be five, sixty nine, yeah.
And let's check out the feature names.
OK.
That's the same as well.
Now let's do our train to split.
OK.
Now, let's instantiate and fit our model.
All right, now, let's check the train score.
And the test score.
All right.
Now, let's see how we can make new predictions.
So you see, have you assigned to a variable, it doesn't print the result, but if you have an expression,
then it does print the result.
So are some predictions, and this was an alternative way to calculate the accuracy of the predictions.
So we have the same answer as before, and we also have this other example where we can use a neural
network to do the same thing.
So let's build the model, train it and print the train score.
And let's print the test score as well.
All right, so everything works the exact same way as it does without using notebook.
Now, another thing I could have done was I could have just taken this whole thing and copied and pasted
it in and run it.
So that's another possible thing you can do.
But the downside of that is you don't get to see the entire media outputs, but again, that's what
print statements are for.
All right, so what can we conclude from this exercise?
Well, we can see that this code runs exactly the same inside Jupyter notebook as it does everywhere
else.
That's why I always say Python code is Python code, and no matter where it is, if you want to use
Jupyter Notebook to run the course code, there's absolutely nothing stopping you from doing so.