The indispensable Jupyter Notebook usage tips

Pycharm is preferred if you are developing in Python; But if you want to use Python for data analysis, data mining, and hot machine learning and artificial intelligence projects, Jupyter Notebook is the place to go, because it’s always been the best tool for data science.

Even now out of Jupyter Lab, but also based on the basis of Jupyter Notebook for extension. This article introduces several techniques for using Jupyter Notebook

Install and start Jupyter Notebook

Install Jupyter Notebook

My own Jupyter Notebook is installed automatically through Anaconda, and it is highly recommended to use Anaconda to install various Python tools and packages, including Jupyter Notebook, to solve various environmental problems.

In Windows installation Jupyter notebook can see this in the zhihu article: zhuanlan.zhihu.com/p/44357064,…

conda install jupyter notebook   # installation

# PIP installed and activated
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple flask
pip install jupyter_contrib_nbextensions 
jupyter contrib nbextension install --user
Copy the code

Start the Jupyter notebook

In the directory we want to boot directly enter the following command (macOS) :

$ jupyter notebook   # Enter Jupyter Notebook to launch immediately
Copy the code

If you are running Windows, you can now start it in the /Download directory on drive D:

1. Now enter CMD in the search bar

2. Enter the COMMAND line interface (CLI) in the directory and enter jupyter notebook

The tutorials for installing and starting Jupyter Notebook are self-searchable online and aren’t a puzzle or the focus of this article.

Subject to use

Install different themes

Install and use different themes in Jupyter Notebook

# installation
pip install jupyterthemes
pip install --upgrade jupyterthemes
Copy the code

Switch themes

# See all color themes --list
jt -l

# Select theme --themeJt-t Topic name# restore the default theme --recover
jt -r
Copy the code

Install and use plug-ins

Installing a plug-in

In order to use Jupyter Notebook, we must install the plug-in.

Install Jupyter's configurator
pip install jupyter_nbextensions_configurator

Start the configurator
jupyter nbextensions_configurator enable --user
Copy the code

The use of plug-in

After installing the plug-in, we can launch Jupyter Notebook and see the installed extension:

Use skills

Use the Markdown

In Jupyter Notebook, you can use Markdown directly:

1, in the edit box operation: the mouse arrow on the left of the edit box

The one on the left turns blue, and by pressing M (on the keyboard), it becomes Markdown

2. Set through the toolbar:

The default format for each cell is code

We change the format of the cell: select the tag

Latex formulas

Since the Jupyter Notebook can use Markdown, we can edit the LaTex formula in the Jupyter Notebook. For example, enter the following text in the Markdown cell:

$$ P(A \mid B) = \frac{P(B \mid A) \, P(A)}{P(B)} $$
Copy the code

LaTeX formulas are generated:

Quickly generate cells

The figure below shows that there is no third cell, so we place the cursor to the left of the second cell

When the edit bar turns blue: Press THE A key on the keyboard to generate A new cell above, or B key to generate A new blank cell below:

Generate side column record

As mentioned earlier, Markdown can be used in Jupyter Notebook. Markd itself has a directory level, so it can also be used in Jupyter Notebook.

In the above example, the directory level starts at three levels:

# # # import library
### Use caseBased case # # # # # # # # to change the position and color scroll # # # # # # # # illustrations pie chart # # # # # circular pie chart # # # # # more pie chart # # # # # # # # # # rose diagram embedded pie chartCopy the code

If we have an update operation at the directory level, we can use this button to update:

If you want to change the location of the directory and move it to the right, you can drag it:

Place the cursor at this height to show the cross and drag it directly to the right:

What it looks like: There is also a hide button in the upper right corner

When we set up the directory, we click on any directory level and jump directly to the corresponding location.

Generate article directories

If we treat the entire IPynb file as a single article, we can also generate a table of contents for the entire article: click the button in the image

Just check Add notebookToCcell and make up the title yourself

Generate the appearance of the article directory, the same click on a directory will be the corresponding jump:

The highlighted

If we want to highlight headings at a level in the directory that we think is important, we can do as follows:

Suppose we want to highlight multiple and embedded pie charts:

After adding highlights:

Output multiple variables

By default, notebook prints only the result of the last variable. In this example, variable A is not printed

By setting the InteractiveShell astnodeinteractivity parameter to all, can let all variables or statement can be displayed, add two lines of code output can achieve the effect of multiple variables:

from IPython.core.interactiveshell import InteractiveShell 

InteractiveShell.ast_node_interactivity = 'all'
Copy the code

The code number

Sometimes too much code can be written in a single cell. We can put a label in front of the code so that if there is an error, we can quickly locate the problem:

Serial number can be set by shortcut keys:

Spellcheck Spellchecker

This is an extension to check for spelling mistakes, which can quickly catch mistakes in our spelling.

The main thing needed is that this plugin works in Markdown format:

This is wrong question

The Variable Inspector

This is a plug-in that examines variables, looking at their names, types, sizes, shapes, values, and so on.

The variable shape parameter in matrix operations, very practical, and this window can also adjust the size, sorting and other functions, very recommended

Magic command

Here are a few common IPython magic commands recorded at 👇 :

Magic command role
%pwd View the current working directory
%cd Change the current working directory
%ls View the directory file list
%writefile Written to the file
%run Run the script; % run can execute Python code from a.py file or specify an ipynb file
%whos View current variable
%reset Know current variables
%timeit Test single line of code run time

Jupyter Notebook two modes

There are two modes in Jupyter Notebook:

  • Command mode: Keyboard input runs program commands with cells in blue
  • Edit mode: allows you to type code or text into cells that are green

Command mode

Esc will take you into command mode, you can use the arrow keys to navigate on the notebook, command line mode operation:

  1. A on the keyboard inserts A new cell above the current cell, and B inserts A new cell below.
  2. M on the keyboard changes the current cell to Markdown format, and Y changes it back to code format
  3. D+D: Press twice to delete the current cell
  4. Shift-enter: Runs this cell and selects the next cell
  5. Ctrl-enter: Runs this unit
  6. Alt-enter: Runs this cell and inserts a new cell under it
  7. Shift-k: Expand the selected unit
  8. Shift-j: Enlarge the lower cell

Edit mode

The Enter key on the keyboard switches from command mode back to edit mode for a given cell.

  1. Shift + Tab: Displays the docstring (document) of the object just entered in the code unit
  2. Ctrl + Shift + -: Divides the current cell into two parts from where the cursor is
  3. Esc + F: Find and replace code, not output
  4. Esc + O: Toggles cell output
  5. Shift + J or Shift + Down: Selects the next cell Down
  6. Shift + K or Shift + Up: Selects the upward cell
  7. Shift + M: Merges multiple cells
  8. Ctrl-m: Enters the command mode
  9. Shift-enter: Runs this cell and selects the next cell
  10. Ctrl-enter: Runs this unit
  11. Alt-enter: Run this unit and insert a unit below

conclusion

This article starts with the installation of Jupyter Notebook, briefly introduces its installation and startup, installation and switch of different themes, and then introduces the installation and use of extensions in detail. Finally, it summarizes two modes of Jupyter Notebook. Hopefully, this article will help readers learn how to use Notebook and get started with the data analysis tool.