8 easy to use Jupyter Notebook tips to explode!

Currently VScode, Pycharm, and Spyder are all very good Python editors. The Jupyter notebook based on IPython has interactive operations, which can bring great convenience to data analysis, intermediate results and visualization of the modeling process. There are still a large number of users who have been using it, and its status cannot be replaced.

In the previous article "I know you have heard of Jupyter Notebook, let me ask, would you really use it?" , I introduced eight efficiency-improving Jupyter Notebook plug-ins in detail.

In this article, I will share eight more Jupyter notebook tips to help you quickly become a Jupyter notebook skilled veteran.

1、IPython Magic

IPython's magic commands only need one line of code, and you can get double precision for your graphics output on a Retina screen (such as a MacBook). "Note: The following example will not work on non-Retina screens"
Insert picture description here
Insert picture description here

2. Suppression function

Sometimes it is convenient to suppress the output of the function in the last line, such as when drawing. To achieve the whole goal, you only need to add a semicolon at the end.

Insert picture description here
Insert picture description here

3. Beautiful display variables

The first thing about being beautiful is well known. When completing a Jupyter cell, if it is a variable name or a statement that does not assign a value to the output, Jupyter will still display the variable without the print statement. This is especially useful when dealing with Pandas DataFrames, the corresponding output will be neatly displayed as a table.

What is less known is that you can adjust the ast_note_interactivity kernel option to enable Jupyter to perform this operation on each variable or statement of its own, so you can see the variable values ​​of multiple statements at once.
Insert picture description here
Insert picture description here

4. Convenient link documents

In the built-in Help menu, you can find convenient links to online documentation of some common libraries, including NumPy, Pandas, SciPy and Matplotlib. Don't forget to append? In front of a library, method or variable, you can access the documentation for a quick reference of the corresponding syntax.
Insert picture description here

5. Install other kernels for Jupyter

A very good feature of Jupyter is that it can run the kernel for different languages. For example, here is how to install and run the R kernel.

Simple option: install R with Anaconda

If you are using Anaconda to manage your environment, then installing R is a simple matter. Just run the following code:

conda install -c r r-essentials

Complex option: manually install the R kernel

If you are not using Anaconda, the process will be slightly more complicated. First, if you don't have R installed, install R from CRAN first.

After completing the above installation, start the R console and run the following command:

install.packages(c('repr', 'IRdisplay', 'crayon', 'pbdZMQ', 'devtools'))
devtools::install_github('IRkernel/IRkernel')
IRkernel::installspec() # to register the kernel in the current R installation

6. IPython Magic-%env: Set environment variables

You can manage environment variables in your notebook without restarting the jupyter service process. Some libraries (such as theano) use environment variables to control performance, %env is the most convenient method. Running %env without any parameters will list all environment variables. The following statement sets environment variables.

%env OMP_NUM_THREADS=4

7. IPython Magic-%who: Display all variables

The %who command without any parameters will display all variables in the global scope. Passing in a parameter such as str will only list the corresponding type.

one = "for the money"
two = "for the show"
three = "to get ready now go cat go"
%who str
one three two

8. IPython Magic-%store: Pass variables in different notebooks

The %store command allows you to pass variables between two different notebooks.

data = 'this is the string I want to pass to different notebook'
%store data
del data # This has deleted the variable
Stored 'data' (str)

Now, in another notebook...

%store -r data
print(data)
this is the string I want to pass to different notebook
Recommended reading

For more exciting content, follow the WeChat public account "Python learning and data mining"

In order to facilitate technical exchanges, this account has opened a technical exchange group. If you have any questions, please add a small assistant WeChat account: connect_we. Remarks: The group is from CSDN, welcome to reprint, favorites, codewords are not easy, like the article, just like it! Thanks
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_38037405/article/details/108115390