- Jupyter Notebook installation and configuration
- How to use Jupyter Notebook
- Next Lesson
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).
To install, Jupyter Notebook, follow the installation instructions below (or read the official quickstart guide).
Download and install Anaconda (latest Python 3 version)
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.
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:
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
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.
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
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.
Please share your own tips and I’ll add them to this list.
shift-enterto execute the current cell.
- Regularly save your Notebooks using
ctrl-s(on Windows) or
cmd-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
escto enter “command” mode (the cell will turn blue) and then:
mkey to switch the current cell to markdown
bkeys to insert a new cell above or below the current cell, respectively
dkey twice to delete the current cell
fkey to find in your code but not outputs
shift-tabwill show you the dcostring for the object you have just typed into code
- To select multiple cells, enter command mode (
esc) and then use
shift-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:
%whoin 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?
%who <data type>will list only variables of that type e.g.,
- How fast is my code? You can use the magic command:
%%timeat the beginning of a cell to determine how long it takes to execute said cell. You can also use
%prunto 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
- 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.
In the next lesson, you will learn about the Python programming language and gain practical experience using Jupyter Notebook.