Jupyter Notebook 27绝技

来源:互联网 发布:淘宝五折换购 编辑:程序博客网 时间:2024/06/05 03:52

转载自:https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/

 

Jupyter notebook, formerly known as the IPython notebook, is a flexible tool that helps you create readable analyses, as you can keep code, images, comments, formulae and plots together.

Jupyter Notebook, 集代码、图像、注释、公式、图表为一体的神器。

Jupyter名字的起源 :the name Jupyter is an indirect acronyum of the three core languages it was designed for: JUlia, PYThon, and R and is inspired by the planet Jupiter.

 

1. Keyboard Shortcuts  快捷键

 怎么查看说明,打开一个notebook,Help > Keyboard Shortcuts

有命令模式的快捷键:

也有编辑模式的快捷键:

 

几个常用的:

1. Esc + F  :Find and replace on your code but not the outputs.

查找并替换代码(而不是输出)

 

2. 对于cell的一些操作

Esc + O  折叠cell输出;

Shift + J or Shift + Down:向下选中多个cell,反之亦然

选中多个cell有什么用呢?删除修改复制等不用说,可以合并呀,哈哈

Shift + M: 合并多个cell

 

2. Pretty Display of Variables   美化输出

主要把数据以更好的形式输出,方便观察,先来个普通的:

虽然能看,但是……也还行吧,能更好就尽量更好,那么

是不是觉得很赞,但是每次还要import interactiveShell,有点麻烦哈

没关系,在~/.ipython/profile_default/ipython_config.py  里添加:

c = get_config()
# Run all nodes interactivelyc.InteractiveShell.ast_node_interactivity = all

我测试的时候好像有点问题,第一句话其实是不用的,但是第二句话删掉后貌似还可以,有点奇怪。

 

1. Help菜单

2. ?操作符,?后面跟着想查询的对象就可以啦,其实就是调用了docstring。

  

4. Plotting in notebooks 绘图

一般都是用matplotlib绘图,就一条命令:

%matplotlib inline

另外,据说%matplotlib notebook 可已创建可交互图,但是非常慢,可是我都没成功。作为替代,bokeh包可以尝试下。土豪也可以用plot.ly,能做出更漂亮的图表,但是要付费。mpld3 也是一个不错的替代。

 

5. Jupyter Magic Commands   神奇的命令符 %

说实话我真不知道这个用处到底多大,用%lsmagic 查看  

有缘人请查看文档:http://ipython.readthedocs.io/en/stable/interactive/magics.html

后面介绍几个所谓的%绝技

 

 

6. Jupyter Magic - %env: Set Environment Variables   定义环境变量

 

定义环境变量,而且不用重新启动notebook,一些库,比如theano,会用到环境变量

可以用%env  查看环境变量

也可以这样设置:%env OMP_NUM_THREADS=4

话说这个还是挺给力的。

 

 

7. Jupyter Magic - %run: Execute python code   直接运行文件

 

命令:%run

用途:可以直接运行.py 文件,或者 notebook的.ipynb文件

没什么说的,提高了集成性,有用

 

 

8. Jupyter Magic - %load: Insert the code from an external script

 

将python文件插入到cell中,%load E:/test.py

但是要注意,test.py中的内容会把cell里面的东西覆盖掉

所以,这个类似于一个快捷的复制粘贴吧,当然,也可以load一个URL,哈哈

 

 

9. Jupyter Magic - %store: Pass variables between notebooks. 在notebook之间共享变量

 

如题

两个命令: %store 变量

              %store -r 变量

 

 10. Jupyter Magic - %who: List all variables of global scope.  列出所有的全局变量

 如题,也可以根据类型筛选,如:%who str

 

 

 11. Jupyter Magic - Timing   计时

 这个功能很有用,尤其是分析一段代码的性能时,有两种方式:

1. %%time  记录一个cell中代码的运行时间  

 

2. %%timeit  调用python的timeit模块,运行10万次,给出最好的三次运行时间的分析结果。

 

12. Jupyter Magic - %%writefile and %pycat: Export the contents of a cell/Show the contents of an external script

代码的保存和查看

notebook一般是用来试验,一段代码写好,测试没问题了,不用复制粘贴出去,使用命令:%%writefile filename.py 保存。

反过来,有之前的%load, %run 命令,把已经有的代码载入,或者运行,但是有些代码只想看看,就可以用命令:%pycat filename.py 查看。

 

13. Jupyter Magic - %prun: Show how much time your program spent in each function.  分析一条命令所涉及的函数,及总的运行时间

命令: %prun fun()

 

14. Jupyter Magic - Debugging with %pdb  调试

命令: %pdb

然而,这个pdb的调试我不会用……

文档:https://docs.python.org/3.5/library/pdb.html#debugger-commands  

友情提示,调试模式下,里面的3变成了*,这样你需要在调试窗口输入exit,退出调试模式。

 

15. Suppress the output of a final function.    简化输出  

就是一个分号;,作用就是不显示变量,这里变量是重点,先看官方的例子,是一个很好的使用场景

 然后继续深入研究下

 一目了然吧,就是不显示变量,一个分号;搞定。

 

16. Executing Shell Commands  运行Shell命令

一个叹号!加上命令,就可以把notebook变成cmd黑框框,也是很方便啦

 试了下,不能用powershell,算是一个小辅助吧。

 

17. Using LaTeX for forumlas  使用LaTex编写公式

 看上去很方便,然而我没用过,有缘人自己挖掘吧

 

18. Run code from a different kernel in a notebook 在notebook里运行其他语言

在一个notebook里运行多种语言,这个也是很给力了,至少python2和python3切换是很方便了,其他的还有bash, HTML, ruby, perl。只需要加两个百分号,比如:%%python2。

 

19. Install other kernels for Jupyter 为jupyter notebook添加其他语言的kernel

好啦,用%%可以运行其他语言,但是也要先安装啊,两种安装方法

1. easy模式,前提是安装了Anaconda

cmd窗口运行conda install -c r r-essentials 即可安装

2. 手动安装

还是以R为例,先要自己安装R

然后,在R的console中运行:

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

所以还是安装Anacoda吧……

 

20. Running R and Python in the same notebook. 在同一个Notebook中运行python 和R

之前提到,用%%可以在Notebook中运行不同的语言

针对Python和R,貌似有更好的解决方案,即安装rpy2,当前这之前还是要装好R的

!pip install rpy2

这样,据说可以在两种语言之间共享变量,看上去很厉害的样子,但是我还没试过

看图,貌似R绘制的图更加漂亮呢……

 

21. Writing functions in other languages  使用其他语言编写函数

用于numpy性能不够时,需要自己编写性能更好,更快的函数,那么也就不能指望python了,需要搬出祖宗级的cython或者fortran了,编写完直接用python调用。

当然也是要先安装相关的东西

!pip install cython fortran-magic 

 然后就可以开干了

cython

 fortran

扩展阅读:

using fortran from python

http://arogozhnikov.github.io/2015/11/29/using-fortran-from-python.html

 

python代码加速的一些东西

http://arogozhnikov.github.io/2015/09/08/SpeedBenchmarks.html

 

22. Multicursor support  编辑多行

按住Alt,选择多行,每行就都会有光标,同时编辑

 

23. Jupyter-contrib extensions 一个小宝库

比如 jupyter spell-checker ,code-formatter

可以用命令安装:

 

!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

 

 也可以在Notebook中安装

24. Create a presentation from a Jupyter notebook.  notebook 秒变PPT

安装RISE

conda install -c damianavila82 rise

或者  pip install RISE

然后启动这个东西:

jupyter-nbextension install rise --py --sys-prefixjupyter-nbextension enable rise --py --sys-prefix

怎么用呢,以后再说。

 

25. The Jupyter output system

Notebook是使i用HTML展示的,cell的输出也是HTML,这样基于浏览器的话,那就可以输出视频,音频,图片了

当然首先还是需要Notebook支持的:

from IPython.display import display, Image

还有一个悲惨的播放视频失败案例:

融会贯通一下,也可以用bash命令获得文件列表

 

26. ‘Big data’ analysis  大数据分析

python大数据处理的一些工具

ipyparallel (formerly ipython cluster)

pyspark

spark-sql magic %%sql

 

27. Sharing notebooks  共享notebook

最好的方式使用.ipynb,对于没有使用Jupyter的人来说,也有一些方案:

1. 导出HTML文件,File > Download as > HTML

2. 导出PDF文件,File > Download as > PDF,参考Making publication ready Python notebooks.

3. 将Notebook发布在gists 或者 github。

4. 建立自己的体系:jupyterhub,可以用来讲课或者多人协作。

5. nbviewer,将ipynb文件存到网盘,这个网站可以输入URL,呈现notebook。http://nbviewer.jupyter.org/

6. 建立blog:Create a blog using Pelican from your Jupyter notebooks.

 

 

大功告成!

未经允许,请不要转载!

未经允许,请不要转载!

未经允许,请不要转载!

 

原创粉丝点击