How to optimize your Jupyter Notebook. Jupyter Notebook is nowadays probably the most used environment for solving Machine Learning/Data Science tasks in Python.
Jupyter Notebook is nowadays probably the most used environment for solving Machine Learning/Data Science tasks in Python.
Jupyter Notebook is a client-server application used for running notebook documents in the browser. Notebook documents are documents able to contain both code and rich text elements such as paragraphs, equations, etc...
In this article, I will walk you through some simple tricks on how to improve your experience with Jupyter Notebook. We will start from useful shortcuts and we will end up adding themes, automatically generated table of contents etc...
Shortcuts can be really useful to speed up writing your code, I will now walk you through some of the shortcuts I found most useful to use in Jupyter.
There are two possible way to interact with Jupyter Notebook: Command Mode and Edit Mode. Some shortcuts works only on one mode or another while others are common to both modes.
Some shortcuts which are common in both modes are:
In order to enter Jupyter command mode, we need to press Esc and then any of the following commands:
In order to instead enter Jupyter edit mode, we need to press Enter and successively any of the following commands:
Not many users are aware of this, but it is possible to run shell commands in a Jupyter notebook cell by adding an exclamation mark at the beginning of the cell. For example, running a cell with !ls will return all the items in the current working directory and running a cell with !pwd will instead print out the current directory file-path.
This same trick, can also be applied to install Python packages in Jupyter notebook.
!pip install numpy
If you are interested in changing how your Jupyter notebook looks like, it is possible to install a package with a collection of different themes. The default Jupyter theme looks like the one in Figure 1, in Figure 2 you will see how we will be able to personalise it's aspect.
Figure 1: Default Jupyter Notebook Theme
We can install our package, directly in the notebook used the trick I showed you in the previous section:
!pip install jupyterthemes
We can the run the following command to list the names of all the available themes:
!jt -l # Cell output: # Available Themes: # chesterish # grade3 # gruvboxd # gruvboxl # monokai # oceans16 # onedork # solarizedd # solarizedl
Finally, we can choose a theme using the following command (in this example I decided to use the solarized1 theme):
!jt -t solarizedl
Once ran this command and refreshed the page, our notebook should like like the one in Figure 2.
Figure 2: Solarized1 notebook Theme
In case you wish anytime to come back to the original Jupyter notebook theme, you can just run the following command and refresh your page.
Notebook extensions can be used to enhance user experience offering a wide variety of personalizations techniques.
! pip install jupyter_contrib_nbextensions ! jupyter contrib nbextension install --system
Once nbextensions is installed you will notice that there is an extra tab on your Jupyter notebook homepage (Figure 3).
Figure 3: Adding nbextensions to Jupyter notebook
By clicking on the Nbextensions tab, we will be provided with a list of available widgets. In my case, I decided to enable the ones shown in Figure 4.
Figure 4: nbextensions widgets options
Some of my favourite extensions are:
Auto-generate a table of contents from markdown headings (Figure 5).
Figure 5: Table of Contents
Sample codes to load common libraries and create sample plots which you can use as starting point for your data analysis (Figure 6).
Figure 6: Snippets
Code autocompletion for Jupyter Notebooks (Figure 7).
Figure 7: Code autocompletion
The nbextensions library provides many other extensions apart for these mention three, I encourage you to experiment and test any-other which can be of interest for you!
By default, the last output in a Jupyter Notebook cell is the only one that gets printed. If instead we want to automatically print all the commands without having to use print(), we can add the following lines of code at the beginning of the notebook.
from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = "all"
Additionally, it is possible to write LaTex in a Markdown cell by enclosing the text between dollar signs ($).
It is possible to create a slideshow presentation of a Jupyter Notebook by going to View -> Cell Toolbar -> Slideshow and then selecting the slides configuration for each cell in the notebook.
Finally, going to the terminal and typing the following commands the slideshow will be created.
pip install jupyter_contrib_nbextensions # and successively: jupyter nbconvert my_notebook_name.ipynb --to slides --post serve
Magics are commands which can be used to perform specific commands. Some examples are: inline plotting, printing the execution time of a cell, printing the memory consumption of running a cell, etc...
Magic commands which starts with just one % apply their functionality just for one single line of a cell (where the command is placed). Magic commands which instead starts with two %% are applied to the whole cell.
It is possible to print out all the available magic commands by using the following command:
How To Plot A Decision Boundary For Machine Learning Algorithms in Python, you will discover how to plot a decision surface for a classification machine learning algorithm.
You got intrigued by the machine learning world and wanted to get started as soon as possible, read all the articles, watched all the videos, but still isn’t sure about where to start, welcome to the club.
What is neuron analysis of a machine? Learn machine learning by designing Robotics algorithm. Click here for best machine learning course models with AI
Python For Machine Learning | Machine Learning With Python, you will be working on an end-to-end case study to understand different stages in the Machine Learning (ML) life cycle. This will deal with 'data manipulation' with pandas and 'data visualization' with seaborn. After this an ML model will be built on the dataset to get predictions. You will learn about the basics of scikit-learn library to implement the machine learning algorithm.
Python for Machine Learning | Machine Learning with Python, you'll be working on an end-to-end case study to understand different stages in the ML life cycle. This will deal with 'data manipulation' with pandas and 'data visualization' with seaborn. After this, an ML model will be built on the dataset to get predictions. You will learn about the basics of the sci-kit-learn library to implement the machine learning algorithm.