Python is used by Data Scientists and Machine Learning Engineers a lot. It is highly optimal for training and building datasets on various day-to-day ML algorithms, both supervised and unsupervised. However, which platform do most ML enthusiasts prefer for working with Python? The answer is Jupyter Notebook.
Jupyter Notebook is the perfect place to run your Python project codes and get your well-running outputs. It is one of the 10 most used IDEs, beating Sublime Text and Eclipse. Its cell-by-cell structure makes it easy to comprehend and divide different portions of the code while running it as a whole.
In this article, we will provide you with 10 wonderful tips & tricks for Jupyter Notebook which will improve your navigation experience and also improve the quality of your project codes. So, let’s get right into it!
10 Jupyter Notebook Tips and Tricks for Data Science:
Here are 10 Jupyter Notebook Tips and Tricks for Data Science. The various chosen methods revolve mostly around built-in commands, functions, kernels and much more. Let’s explore them one by one!
1) Running Shell Commands
As demonstrated here, you can use ! to execute Shell and Bash commands inside the Jupyter Notebook cell. It gives you additional capability to use tools and commands based on Linux or Unix.
!git push origin
Installing Python libraries while on the go is the most common usage for this command. You can install several Python packages like Pandas, Numpy, and Keras.
!pip install Keras
Another way to install Python libraries is by using the magic command ‘%’.
%pip install Pandas
This tip will be highly beneficial for you to install and import several useful Python packages as you keep working on your Machine Learning and Data Science projects. Imagine importing tons of functionalities in one go and improving your Jupyter Notebook experience.
2) Plotting in Notebooks
If you are working on a Data Science or Machine Learning project, a specific portion of your code will be reserved for plotting visualizations such as graphs and multivariate statistics. Plotting can be done in your Jupyter Notebook in a variety of ways.
- Plot.ly, a once-paid service that is now open-sourced, can produce some attractive plots.
- Seaborn activates Matplotlib, the de-facto standard, which is built on top of Matplotlib and facilitates the creation of more visually appealing plots. Without changing any code, you can make your matplotlib graphs “prettier” only by importing Seaborn.
- For matplotlib code, ‘mpld3’ offers an alternate renderer (using d3). It’s very efficient but lacks some details.
- For Python, ‘Altair’ is a relatively new declarative visualization package. It is simple to use and produces beautiful plots, although it lacks Matplotlib’s level of customization capability.
By utilizing these tips you will be able to plot several visualizations in your Python codes such as comparison graphs, data summaries, accuracy statistics, and much more.
Here are some amazing Python libraries that you must know about to master it.
3) Multimedia Outputs
Images, Videos, and Audio may all be viewed without the need to install extra Python packages.
All you need to do to access the Image, Video, and Audio features is import IPython.display. When working with unstructured information and machine learning applications, it is very helpful.
So go ahead and enjoy the fully functional multimodal capabilities offered by Jupyter Notebook. You will take your Data Science projects to another level with this functionality.
4) Keyboard Shortcuts
Any Notebook user will tell you that using keyboard shortcuts can save you a tonne of time. Keyboard shortcuts in Jupyter can be accessed through the Help > Keyboard Shortcuts menu at the top of the screen or by hitting H to enter command mode (more on that later). Every time you upgrade Jupyter, it’s important to check this because new shortcuts are always being introduced.
A VSCode-like command palette is the most convenient and widely used method for quickly accessing commands while on the run. The command palette can be accessed by pressing Ctrl + Shift + P. You can use it to search and run commands or to browse through all of the commands and find the one you want to run.
5) Adding Additional Kernels to Jupyter Notebook
While the Python kernel is well-known, you can run code in any language by installing different kernels.
For instance, you must install Almond and Scala libs inside the Scala environment to use the Scala programming language in the Jupyter Notebook.
Download almond and scala libs
(coursier is a scala tool used to install almond)
$ curl -Lo coursier https://git.io/coursier-cli && chmod +x coursier
(replace scala and almond versions if you need to)
$ ./coursier bootstrap \
-r jitpack \
-i user -I user:sh.almond:scala-kernel-api_2.12.11:0.10.0 \
sh.almond:scala-kernel_2.12.11:0.10.0 \
-o almond
Install Scala kernel in Jupyter
$ ./almond --install --id scala_2_12_11 --display-name "Scala 2.12.11"
Open Jupyter notebook and select Scala kernel
$ jupyter notebook
Some widely used available Kernels in Jupyter Notebook are:
- global-tf-python-3 /home/felipe/.local/share/jupyter/kernels/global-tf-python-3
- local_venv2 /home/felipe/.local/share/jupyter/kernels/local_venv2
- python2 /home/felipe/.local/share/jupyter/kernels/python2
- python36 /home/felipe/.local/share/jupyter/kernels/python36
- scala /home/felipe/.local/share/jupyter/kernels/scala
6) IPython Magic Commands
The Jupyter Notebook contains all of the IPython Magic commands. You can execute the code with additional capabilities thanks to these commands.
A single line of code can have additional functionality added to it with the help of line magic commands, which start with ‘%’ and end with the line command. Run “%lsmagic” to view all accessible magic commands; this command will display all available magic commands.
Here are the available magic commands in Jupyter Notebook:
%alias, %alias_magic, %autoawait, %autocall, %automagic, %autosave, %bookmark, %cat, %cd, %clear, %colors, %conda, %config, %connect_info, %cp, %debug, %dhist, %dirs, %doctest_mode, %ed, %edit, %env, %gui, %hist, %history, %killbgscripts, %ldir, %less, %lf, %lk, %ll, %load, %load_ext, %loadpy, %logoff, %logon, %logstart, %logstate, %logstop, %ls, %lsmagic, %lx, %macro, %magic, %man, %matplotlib, %mkdir, %more, %mv, %notebook, %page, %pastebin, %pdb, %pdef, %pdoc, %pfile, %pinfo, %pinfo2, %pip, %popd, %pprint, %precision, %prun, %psearch, %psource, %pushd, %pwd, %pycat, %pylab, %qtconsole, %quickref, %recall, %rehashx, %reload_ext, %rep, %rerun, %reset, %reset_selective, %rm, %rmdir, %run, %save, %sc, %set_env, %store, %sx, %system, %tb, %time, %timeit, %unalias, %unload_ext, %who, %who_ls, %whos, %xdel, %xmode
Let’s show a demonstration of the %time magic command which shows the running time of a specific code snippet in a cell.
def fibonacci(n): if n == 0 or n == 1: return n return fibonacci(n-1)+fibonacci(n-2) %time fibonacci(10)
Output:
CPU times: total: user 20 ns, sys:4 ns, total: 24 ns
Wall time: 26.2 ns
55
7) Simple Connections to the Documentation
You may easily access the online documentation for popular libraries such as NumPy, Pandas, SciPy, and Matplotlib by clicking on the Help menu. Remember that by preceding a variable, library, or method with ‘?’ for a fast syntactic reference, you can utilize the Docstring.
?str.replace()
Docstring:
S.replace(old, new[, count]) -> str
Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
Type: method_descriptor
8) Exporting Notebooks
Sharing notebooks not only helps you collaborate with others on your project but also increase the efficiency of your work. Your notebooks can be shared both locally and also via the cloud. It’s crucial to share the reports or code source along with the outcomes, and there are several ways to do this:
- Use the File > Download as > HTML option to convert Notebooks into HTML files.
- Use File > Download as > PDF to save notebooks as PDFs.
- Notebooks can be downloaded as Markdown or saved as a Markdown file.
- Build blogs with Pelican.
- Share the.ipynb file with others by uploading it to Google Colab.
- Use GitHub Gits to distribute the Notebook file to the general audience.
- Use nbviewer to render the notebook after hosting your file on an external server or the cloud.
These techniques will not only help you to share your source code but also make it available on a variety of platforms where several others can view and work on it.
9) Using LaTeX for Formulas
You must include statistical or mathematical equations when developing the data analytics report, and Jupyter Notebook allows you to render complex equations using Latex formulas.
To enclose your Latex formula, simply create a Markdown cell and use the dollar $ symbol, as demonstrated below.
$P(A \mid B) = \frac{P(B \mid A)P(A)}{P(B)}$
This is what happens to the equation as a result of the latex function:
10) Running Code from different Kernels
You can merge code from different kernels into a single notebook if you’d like. Using Magic instructions like these, you can use Python Jupyter Notebook to execute code from various kernels:
%%html
%%javascript
%%python3
%%ruby
%%perl
%%bash
Let’s try running a Javascript snippet in Jupyter Notebook using the %%javascript magic command:
%%javascript alert("This is a JavaScript alert!")
Output:
This is a JavaScript alert!
Simply enter the name of your kernel into IPython Magics at the beginning of each column that you wish to use that kernel for. You will get the benefits of running the same code under different kernels in one go.
Conclusion
These Jupyter Notebook tips and tricks will help you become more efficient and optimal with your Data Science and Machine Learning projects. It’s time to brush up your skills again and be the best in your career!