Jupyter notebooks¶
25 min Foundations Needs: a computer you can install software on
A notebook lets you run code one piece at a time and see the result right underneath it. You will spend more hours in notebooks than in any other tool this year.
Why this matters¶
Machine learning is guesswork with evidence. You load some data, look at it, try something, look again. Running a whole script every time you want to check one number is slow and it hides what went wrong.
Notebooks fix that. Each piece of code stays on screen next to its output, so your experiment and your record of the experiment are the same document.
What a notebook is made of¶
The kernel is the part people forget, and it causes most notebook confusion. It is a Python process running in the background. It remembers every variable you have created, in the order you ran the cells, not the order they appear on screen.
Step 1: Install Anaconda¶
Anaconda is Python plus about 250 packages, including everything you need for the next few months. Installing it is easier than installing the pieces one by one.
Download the installer for your system from the Anaconda download page and run it.
Double click the .exe file and follow the installer.
Choose Just Me, keep the default folder, and leave Add Anaconda to PATH unchecked. Do allow it to register as your default Python.
Leaving it off PATH looks wrong but is correct. It stops Anaconda's Python from shadowing the system Python and breaking other software. You launch it from the Anaconda Prompt instead.
Double click the .pkg file and follow the installer. The defaults are
fine.
You are done when Anaconda Navigator opens from your applications menu.
Step 2: Launch Jupyter¶
Your browser opens at http://localhost:8888 showing your files. That page is
the dashboard, not a notebook yet.
Start Jupyter from the folder you want to work in
Jupyter can only see the folder it was started in and anything below it. Use
cd to get to your project folder first, then run jupyter notebook. It
saves a lot of hunting.
Step 3: Run your first cells¶
Click New, then Python 3. An empty cell appears.
Type this into it and press Shift+Enter.
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
data = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
df.head()
You should see a small table of flower measurements. If you do, every library you need for the next few months is installed and working.
Notice the last line has no print(). A notebook shows the value of the final
line in a cell automatically, and for a table it renders it properly instead of
dumping text.
Now add a second cell and run it:
It works because the kernel still remembers df from the first cell. That is
the whole idea.
The shortcuts worth learning¶
| Keys | What it does |
|---|---|
| Shift+Enter | Run the cell and move to the next one |
| Ctrl+Enter | Run the cell and stay where you are |
| B | New cell below (press Esc first) |
| A | New cell above |
| M | Turn this cell into notes (markdown) |
| Y | Turn it back into code |
| D D | Delete the cell |
Press Esc before the single letter shortcuts. Otherwise you just type the letter into your code.
Step 4: Keep your notebooks trustworthy¶
Cells remember their results, so a notebook can look like it works when it does not. You edited a cell, ran a later one, went back, and now the numbers on screen came from code that no longer exists.
There is one habit that prevents this. Before you trust a result or show it to anyone, use Kernel, then Restart and Run All. That wipes the kernel's memory and runs every cell from the top in order. If it still works, it really works.
Clear your outputs before committing to Git
A notebook stores its outputs inside the file, including every plot as
encoded image data. Commit it as is and your repository fills with noise
that nobody can read in a diff. Use Kernel, then Restart and Clear
Output, before you git add the notebook.
A few more things that will save you later:
- Name notebooks so the order is obvious:
01-explore-data.ipynb, then02-train-model.ipynb. - Write a markdown cell above each section saying what you are about to try. Your future self reads those, and so does your instructor.
- When a notebook gets long and the code is settled, move it into a
.pyfile. Notebooks are for figuring things out, not for keeping things.
Check yourself¶
- Create a notebook called
01-first-look.ipynb. - Load the iris data and show the first five rows.
- Add a markdown cell above it saying what the dataset contains.
- Add a cell that prints the average of each column.
- Run Restart and Run All and confirm everything still works from a clean start.
Next¶
You can now experiment quickly. Next you set up the editor for writing real programs, the ones with more than a few cells in them.
More depth when you want it: the Jupyter documentation is the official reference.