L1: Jupyter Notebook
Table of Contents
We will be using Jupyter Notebook for the signal processing and machine learning portion of our course. Jupyter Notebook is a popular data science platform for analyzing, processing, classifying, modeling, and visualizing data. While Notebook supports multiple languages (like R, Julia), we’ll be using Python (specifically, Python 3).
Jupyter Notebook installation and configuration
To install, Jupyter Notebook, follow the installation instructions below (or read the official quickstart guide).
Importantly, please use Anaconda to install Jupyter Notebook and all dependencies. This is the officially recommended way to install and use Notebook (link).
Step 1: Download Anaconda
Download and install Anaconda (latest Python 3 version)
Step 2: Run the server
Open your terminal (on linux or Mac) or the Anaconda Prompt (on Windows) and type:
> jupyter notebook
See “Running the Notebook” in the official Jupyter Notebook docs.
Installing notebook extensions
We strongly recommend installing the jupyter_contrib_nbextensions
notebook extensions, which contains a collection of community-contributed (unofficial) extensions for Jupyter Notebook, including a table of contents generator and navigation pane called Table of Contents (2). See figure below.
TOC2 provides two primary benefits: first, it auto-generates (and updates in real-time) a clickable table of contents based on the header markdown in the Notebook, which greatly simplifies navigation (you will want this). Second, it shows, which cells are queued for execution (in yellow in the navigation pane) and which cells are executing (in red).
Screenshot of a Jupyter Notebook with the nbextension called Table of Contents (2), which auto-generates a navigation pane based on markdown headers.
Feel free to follow the official installation instructions here or our simplified version below:
Step 1: Use conda to install nbextensions
To install the nbextensions
, open your terminal (on linux or Mac) or the Anaconda Prompt (on Windows) and type:
> conda install -c conda-forge jupyter_contrib_nbextensions
Step 2: Open Jupyter Notebook and configure extensions
Restart Jupyter Notebook and you should see a tab called Nbextensions
on the home screen. Click on that tab and then you should see all nbextensions. Click on Table of Contents (2)
to configure and Enable it. See screenshots below.
Step 3: Try out TOC
Make some headers in markdown and then click on the Table of Contents
button in the toolbar to see an auto-generated and clickable navigation sidebar. You can also access the TOC via the Navigate
menu item.
Example of TOC running in Jupyter Notebook
How to use Jupyter Notebook
There are many introductory tutorials and videos to Jupyter Notebook online. We’ll quickly demo Notebook in class but if you want to learn more, start by reading the official introduction to Jupyter Notebook (link). You could also consult this Datacamp tutorial or this Dataquest tutorial.
Again, you will learn Notebook as you go through the lessons and work on your assignments.
Please follow the Python style guide for variable and function naming conventions and other formatting. This will make your code easier to follow and debug. And, of course, we all often (accidentally or intentionally) intermix styles from other programming languages—this is especially true in this course as we toggle between so many languages.
Some useful tips
Please share your own tips and I’ll add them to this list.
- Hit
shift-enter
to execute the current cell. - Regularly save your Notebooks using
ctrl-s
(on Windows) orcmd-s
(on a Mac). Notebooks are also regularly autosaved. - Before turning in or sharing a notebook, it’s useful (and important) to restart the kernel and rerun all cells. Why? Because the most common mistake I’ve seen with Jupyter Notebook is code that refers to variables that are in memory but were renamed or deleted (either intentionally or accidentally) thereby breaking the code! How? Go to the file menu, then
Kernel -> Restart & Run All
- Hit
esc
to enter “command” mode (the cell will turn blue) and then:- The
m
key to switch the current cell to markdown - The
a
orb
keys to insert a new cell above or below the current cell, respectively - The
d
key twice to delete the current cell - The
f
key to find in your code but not outputs
- The
shift-tab
will show you the dcostring for the object you have just typed into code- To select multiple cells, enter command mode (
esc
) and then useshift-j
(orshift-down
) to select cells below the current cell. Once cells are selected, you can copy or cut and paste them somewhere else. - Because Jupyter Notebook is built on the IPython kernel so you can use all of the IPython magic commands, including:
- Type
%who
in a cell to list all variables in global scope. As noted above, determining variables loaded into memory is, perhaps, the single most confusing thing about using Notebooks. What cell did I execute again? - Type
%who <data type>
will list only variables of that type e.g.,%who str
- Type
- How fast is my code? You can use the magic command:
%%time
at the beginning of a cell to determine how long it takes to execute said cell. You can also use%prun
to show much time your program spent in each function. - Suppress final output of a cell. By default, Notebook will display the output of the function of the final line on your cell. Sometimes, you don’t want this! In those cases, just add a semicolon to the end of the line
- You can execute shell commands inside your notebook by prefixing an exclamation point
!ls *.csv
- You can use LaTeX formulas in a Markdown cell, which will be rendered using MathJax (just like we use on this website!)
For more tips, see this dataquest article.
Next Lesson
In the next lesson, you will learn about the Python programming language and gain practical experience using Jupyter Notebook.