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 or 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 put
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 is actually zero difference between writing code in a Jupiter
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 e-mails 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 into work late after you a party
too hard.
Thursday night.
Well that's very simple.
All I have to do is on my server create what's called a cron tab 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 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 prints out the results after you enter a command.
I python is called a repo which stands for read Val print loop and that's generally what they all do.
No matter what language you're in so whether that's Python Ruby HP 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 and 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 is 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 a however you want including Jupiter notebook in 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 Jupiter notebook.
Let's begin
OK.
So let's say I'm in the folder num pi 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 idea.
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 Jupiter notebook.
Well let's start up a Jupiter notebook so I'm gonna go Jupiter notebook.
All right.
So now I've got Jupiter 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 inputs 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.
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 SDK learned that you tells 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 and
you put a 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 actually be ok.
Looks good so far.
Check the target's
still.
What we expect the target names.
Looks good
target shape.
It should be five sixty nine.
Yeah let's check out the feature names
OK.
That's the same as well.
Now let's do our train test 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
S.C. how if you assign to a variable it doesn't print the result but if you have an expression then
it does print the result.
So there 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 scores 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 you put a 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 Jupiter
notebook to run the course code there is absolutely nothing stopping you from doing so.