jupyter notebook some tips

This document is intended to illustrate the use of some method of jupyter notebook

Reference links:

  1. https://www.jianshu.com/p/f21595816abf

Some basic instructions:

%代表magic方法
!代表shell用法

These two methods can facilitate the use jupyter notebook

And use of the command line magic magic command unit is different, the command line magic is %beginning, and the magic command unit is %%beginning.

Usage code auto-correlation function of the reference and filled

In the very beginning, enter the following statement
using the following tabkeys can be

Detailed usage

(Ps personally I think that very few small practical operation, not repeated Baidu Encyclopedia)

  • Only input variables or function of the first few letters, press the tab may auto-complete

  • Function has been entered, the specific use of continuous press shift + tab can query function (press has been 4 times)

  • View correlation function usage available ?numpy.randommethods

%config IPCompleter.greedy=True
import numpy as np
?np.random

Local file directly run Python

scenes to be used

Python did not want to load a local file in the cell Jupyter Notebook, you want to run directly.

Code schematically:

%run xxx.py Python文件的绝对路径,并且可以将相应文件里的变量保存在jupyter notebook中
%load xxx.py Python文件的绝对路径,可以将python的代码加载到当前的cell

运行相应的文件,其结果不保存在当前的cell里
!python3 Python文件的绝对路径
!python Python文件的绝对路径
!python Python文件的绝对路径

Precautions

Python file suffix is ".py".
"% Run" followed by the absolute path Python file.
"! Python3" for executing Python
3.x version of the code.
"! Python" for the implementation of Python
2.x version of the code.
"! Python3" and "! Python" belong! Shell command syntax that performs syntax of shell commands in Jupyter Notebook in.
After entering the command can be executed by the control return command, execution will not display the contents of the local file Python directly display the results.

!python E:\1spyder3document\test3.py
%whos
1
2
3
4
Variable   Type      Data/Info
------------------------------
np         module    <module 'numpy' from 'C:\<...>ges\\numpy\\__init__.py'>
%run E:\1spyder3document\test3.py
%whos
1
2
3
4
Variable        Type       Data/Info
------------------------------------
i               int        4
np              module     <module 'numpy' from 'C:\<...>ges\\numpy\\__init__.py'>
x_lane_0_vehs   ndarray    5: 5 elems, type `float64`, 40 bytes
x_lane_1_vehs   ndarray    5: 5 elems, type `float64`, 40 bytes
x_lane_2_vehs   ndarray    5: 5 elems, type `float64`, 40 bytes

%load E:\1spyder3document\test3.py

The results are as follows squares #

# %load E:\1spyder3document\test3
"""
Created on Tue Jun 25 21:45:35 2019

@author: chwei
"""
import numpy as np
np.random.seed(2)
i=0
while True:
    i=i+1
    x_lane_0_vehs = np.sort(np.append( np.random.uniform(0, 100, [5 - 1]), 500  ))
    x_lane_1_vehs = np.sort( np.random.uniform(0, 100, [5 ]))
    x_lane_2_vehs = np.sort( np.random.uniform(0, 100, [5 ]))
    
    print(i)
    if x_lane_2_vehs[0] <= 5:
        
        break


1
2
3
4

Get the current position in the Jupyter Notebook

① use scene
you want to get the current location in Jupyter Notebook absolute path.

② method

%pwd
!pwd

③ attention to
get the current position is a position where the notebook was created Jupyter Notebook, and the location is the absolute path.
"! Pwd" belong! Shell command syntax, that is Jupyter
execute shell command syntax Notebook.

%pwd

'E:\\jupyter_learning'
  1. Use shell commands Notebook Jupyter
    ① a method - in the cell in a notebook
    ⑴ grammar
!shell命令

Notebook cell in Jupyter Notebook in English with an exclamation point "!" Followed by shell commands to execute shell commands.

!dir -a
 驱动器 E 中的卷是 MyPassportSabrina
 卷的序列号是 0E92-8488

 E:\1PIL_learning 的目录



找不到文件

Gets the value of the variable name and variable

(This method is suitable for the effective spreading jupyter notebook similar to the embodiment spyder WYSIWYG)

%who 显示所有变量的名称
%whos 查看所有变量的名称和值
%reset 清除所有的变量
%who_ls
%who
%who list
%whos
i	 np	 x_lane_0_vehs	 x_lane_1_vehs	 x_lane_2_vehs	 
No variables match your requested type.
Variable        Type       Data/Info
------------------------------------
i               int        4
np              module     <module 'numpy' from 'C:\<...>ges\\numpy\\__init__.py'>
x_lane_0_vehs   ndarray    5: 5 elems, type `float64`, 40 bytes
x_lane_1_vehs   ndarray    5: 5 elems, type `float64`, 40 bytes
x_lane_2_vehs   ndarray    5: 5 elems, type `float64`, 40 bytes

Test code running time

%time 测试代码运行的时间 行
%%time  单元
%%time
for i in range(10):
    print(i)
0
1
2
3
4
5
6
7
8
9
Wall time: 1.99 ms

Function to be explored

Use Jupyter notebook plug

pip install jupyter_contrib_nbextensions && jupyter contrib nbextension install

1.Table of Contents
This extension is generally for the entire file directory of a lot of time. First check the plug-in Nbextensions tab, then you can see the expand button in the toolbar. If we use MarkDown set up our title, click on the extension in the notebook, it will generate a table of contents on the left, click on the left side of the gear, you can add a cell at the very top designed to display the directory. Click the link at the top left and you can quickly jump to the appropriate location. Directory can also be folded. On the menu bar also noted that at this time more than a "Navigate" label also shows how the directory.

  1. Variable inspector

The plug-in can help us see the name, type, size and values ​​of all variables in the current notebook. Eliminating the need to perform df.shape, type () and other statements, instead of the previously mentioned functions to perform magic of "% whos", the reader can try yourself.

  1. Code folding

As the name suggests, the plug can be folded a certain code, such as during class, def keyword, etc., but when the subject code and very long, folded code is easy to read, it also allows jupyter notebook like an IDE.

5.Execute time

The plug-in may display the execution time of each cell in the code.

In addition there are a number of other common plug-in extensions, such as Notify, Collapsible headings, etc., readers can explore on their own view, and configurations.

?str.replace
%pdb
Automatic pdb calling has been turned ON
Published 36 original articles · won praise 0 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_38102912/article/details/101288872