最近做实验一直是用Jupyter Notebook编程,有一种打草稿的便捷感,在dataquest上看到一篇博客总结了28种Jupyter Notebook的使用技巧。为了方便大家理解,对原文一个简略的地方进行了适当的解释和扩充。希望大家在用Jupyter Notebook编程时可以更加爽快。
Jupyter Notebook有很多的快捷键,编程时使用这些快捷键将提高你的编程效率。想知道Jupyter Notebook有哪些快捷键,你可以在它的下拉菜单Help>Keyboard Shortcuts
中找到。或者在command model
中按下H
查看。每次更新Jupyter的时候你都最好看看有哪些新的快捷键。Ctrl + Shift + P
调出command palette
。在这个对话框中你可以输入快捷功能的名字来使用快捷键,比如你想重启kernel
,那就在对话框中输入’restar’,command palette
会自动显示候选的功能。这个功能类似Mac上的Spotlight工具。
我的一些常用快捷键:
Esc
进入command mode
在command mode
下:A
/B
可以在上/下方插入新的cell,M
切换到Markdown模式下,Y
切回编程模式,D+D
删除当前cell
Enter
从command mode
返回edit mode
Shift + Tab
会显示你刚才输入对象的文档
Ctrl + Shift + -
将会分割你的cell
Esc + F
查找替换代码(不包含输出部分)
Esc + O
隐藏cell的输出
你还可以选对多个cell进行操作:Shift + J
或Shift + Down
向下选择,Shift + K
或Shift + Up
向上选择,Shift + M
合并多个cell
当你的cell最后是一个变量名,那么你不需要用print
就可以输出了。特别是你要输出Pandas DataFrames的时候,这很有用。ast_note_interactivity
参数为all
来一次性输出多个变量而不用print
。
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
from pydataset import data
quakes = data('quakes')
quakes.head()
quakes.tail()
# 输出的效果是将head和tail都输出,而不是只有tail输出
如果你希望所有Jupyter 的cell都这样输出,创建一个文件~/.ipython/profile_default/ipython_config.py并输入以下代码:
~/.ipython/profile_default/ipython_config.py
c = get_config()
# Run all nodes interactively
c.InteractiveShell.ast_node_interactivity = "all"
你可以在Help
菜单中看到一些常用库,如NumPy, Pandas, SciPy and Matplotlib的文档。不过你还可以在方法前面加?
来查看对应的文档。
# 执行下面这行代码在Jupyter Notebkook中
?str.replace()
# 将显示文档
Docstring:
S.replace(old, new[, count]) -> str
Return a copy of S with all occurrences of substringold replaced by new. If the optional argument count isgiven, only the first count occurrences are replaced.
Type: method_descriptor
常用的绘图库包括:matplotlib, Seaborn, mpld3, bokeh, plot.ly, Altair
由于Jupyter是基于IPython内核的,所以Jupyter可以使用IPython内核中的Magics
命令。%lsmagic
查看所有的magic命令。
%env
,设置环境变量
# Running %env without any arguments
# lists all environment variables
# The line below sets the environment
# variable
%env OMP_NUM_THREADS%env OMP_NUM_THREADS=4
# output
env: OMP_NUM_THREADS=4
%run
,执行python代码*.py
文件,你可以在Jupyter中执行它。
# this will execute and show the output from
# all code cells of the specified notebook
%run ./two-histograms.ipynb
%load
,导入外部脚本。
# 你有一个hello_world.py文件
# 内容是if __name__ == "__main__": print("Hello World!")
# 在Jupyter中先用%load载入
%load ./hello_world.py
# 运行%load ./hello_world.py命令后,在你的cell中就出现以下几行代码(你执行的%run语句会显示已经注释)
# %load ./hello_world.py
if __name__ == "__main__":
print("Hello World!")
%store
,在notebook之间传递变量。
# 在notebook A 中
data = 'this is the string I want to pass to different notebook'
%store data
del data # This has deleted the variable
# 在notebook B 中
%store -r data
print(data) # 显示this is the string I want to pass to different notebook
%who
,显示所有的变量
# 某个cell中有以下四行代码
one = "for the money"
two = "for the show"
three = "to get ready now go cat go"
%who str
# 输出为
one three two
%%time
和%timeit
%%time
将提供代码单次运行的信息,%%timeit
将默认运行你的代码100,000次,提供最快运行三次的平均结果。
%%writefile
和pycat
,导出单元格的内容/显示外部脚本的内容%%writefile
保存cell内容到外部文件。%pycat
正好相反。
%prun
,显示程序中每个函数的调用信息
%pdb
,代码调试
为视网膜(Retina)屏输出高分辨率图像
# 常规图像
x = range(1000)
y = [i ** 2 for i in x]
plt.plot(x,y)
plt.show();
# 视网膜(Retina)图像
%config InlineBackend.figure_format ='retina'
plt.plot(x,y)
plt.show();
在函数末尾加分号可以抑制函数的输出。
在shell命令前面加!
# 一些例子
!ls *.csv
!pip install numpy
!pip list | grep pandas
18.在markdown cell 中书写LaTeX时,它会被 MathJax 渲染成一个公式
如果想要的话,你可以在一个notebook中运行多种kernel的代码
# 支持:%%bash, %%HTML, %%python2, %%python3, %%ruby, %%perl
%%bash
for i in {1..5}
do
echo "i is $i"
done
Jupyter其实不止可以用于python编程,安装一个R内核它就可以用于R语言编程。
# 通过Anaconda安装
conda install -c r r-essentials
# 手动安装
# 你需要先从https://cloud.r-project.org下载安装R
# 然后在R控制台下运行以下代码
install.packages(c('repr', 'IRdisplay', 'crayon', 'pbdZMQ', 'devtools'))
devtools::install_github('IRkernel/IRkernel')
IRkernel::installspec() # to register the kernel in the current R installation
你可以安装rpy2用pip install rpy2
%load_ext rpy2.ipython
%R require(ggplot2)
array([1], dtype=int32)
import pandas as pd
df = pd.DataFrame({
'Letter': ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'],
'X': [4, 3, 5, 2, 1, 7, 7, 5, 9],
'Y': [0, 4, 3, 6, 7, 10, 11, 9, 13],
'Z': [1, 2, 3, 1, 2, 3, 1, 2, 3]
})
%%R -i df
ggplot(data = df) + geom_point(aes(x = X, y= Y, color = Letter, size = Z))
有时numpy的速度不够快,我需要写一些快速的代码。
!pip install cython fortran-magic
%load_ext Cython
%%cython
def myltiply_by_2(float x):
return 2.0 * x
myltiply_by_2(23.)
就个人而言我建议使用fortran:
%load_ext fortranmagic
%%fortran
subroutine compute_fortran(x, y, z)
real, intent(in) :: x(:), y(:)
real, intent(out) :: z(size(x, 1))
z = sin(x + y)
end subroutine compute_fortran
compute_fortran([1, 2, 3], [4, 5, 6])
你可以在Jupyter中使用多行编辑模式,只需要按住Alt
键。
Jupyter-contrib extensions
是一个插件库,包含了很多实用的插件,包括jupyter spell-checker
和code-formatter
。Jupyter-contrib extensions
!pip install https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tarball/master
!pip install jupyter_nbextensions_configurator
!jupyter contrib nbextension install --user
!jupyter nbextensions_configurator enable --user
安装成功后
Jupyter-contrib extensions
会以菜单栏的方式显示在界面上。
安装RISE
工具就可以从已有的notebook中创建powerpoint风格的演示了。conda install -c damianavila82 rise
或pip install RISE
安装RISE
。
# 激活RISE
jupyter-nbextension install rise --py --sys-prefix
jupyter-nbextension enable rise --py --sys-prefix
使用IPython.display
这个库可以将多媒体文件排列输出。
推荐使用ipyparallel
,pyspark
工具以及%%sql
魔法命令进行大数据查询,处理。
通常分享*.ipynb
文件是最简单的方式。但是如果你要给不用Jupyter的人分享有以下几种选择:
使用File
- Download as
- HTMLl
菜单选项将笔记本转换为html文件
在github
或者gist.github.com
上分享notebooks
使用jupyterhub
搭建你自己的分享系统
在dropbox
上存储你的notebook并且将链接挂到https://nbviewer.jupyter.org上
使用File
- Download as
- PDF
保存notebook为PDF
最后希望大家看完这篇“安利”后可以愉快地使用Jupyter Notebook~
[28 Jupyter Notebook Tips, Tricks, and Shortcuts]
(https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts)
github: https://github.com/keloli
blog: https://www.jianshu.com/u/d055ee434e59
往期回顾之作者李中梁
机器学习算法工程师
一个用心的公众号
长按,识别,加关注
进群,学习,得帮助
你的关注,我们的热度,
我们一定给你学习最大的帮助