In this lecture, I'm going to talk about Jupiter notebook.
Occasionally I get this strange question from students asking why I don't do things in Jupiter 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 Jupiter notebook or not.
Let me repeat that.
Using Jupiter notebook is exactly the same as not using Jupiter notebook.
There's no difference other than the fact that it looks different.
In other words, the screen is a different color.
Obviously such a difference is trivial in this lecture.
I'm going to demonstrate how that's the case.
One major reason I dislike Jupiter 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 you open
a 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 group
and a 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 crosstab 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 script from the
command line.
And now every Friday at nine forty five 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 Jupiter
notebook for something like this.
I think one perceived advantage of Jupiter notebook is that you can see the results of intermediate
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 print out the results after you enter a command,
ie Python is called a Reppel, which stands for read eval print loop.
And that's generally what they all do, no matter what language you're in.
So whether that's Python, Ruby, P or any other language, the keyword 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 Jupiter 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 coding, 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 Jupiter 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 Jupiter notebook, there's absolutely nothing
stopping you from doing so.
In other words, using Jupiter 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 Jupiter Notebook
and the rest of his lecture.
I'm going to prove to you that you can take any script 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 Jupiter notebook.
Let's begin.
OK, so let's say I am in the folder Numpty 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 idea for writing Python code now about using a notebook.
Well, let's start up a Jupiter notebook.
So I'm going to go Jupiter notebook.
All right, so now I've got Jupiter, a 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 type
function.
So let me try that.
Cool, so I get Escala and utils, that 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 Jupiter 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 pie, 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 targets.
OK, still, what we expect the target names looks good.
Target shape, it should be 569, 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.
How 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 those are some predictions, and this was an alternative way to calculate the accuracy of the predictions,
so we get 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 intermediate 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 group in a notebook as it does everywhere
else.
That's why I always say python code is python code, no matter where it is.
If you want to use you put a notebook to run the course code.
There's absolutely nothing stopping you from doing so.