title: Jupyter notebook 教程(高级) date: 2020-03-05 11:22:17


总结在 Jupyter notebook 使用中涉及到的比较有意思的特性(advanced tutorial,不涉及安装软件)。

  1. Shell Commands
1
2
! echo Hello world!
pip freeze | grep pandas

Note that the shell in which ! commands are executed is discarded after execution completes, so commands like cd will have no effect. However, IPython magics offer a solution. ch 是不起作用的,因为是没有状态保留~

  1. Basic Magics

There are two categories of magic: line magics and cell magics.

使用以下命令显示所有的 magics

1
%lsmagic
  1. Displaying Matplotlib Plots
1
%matplotlib inline

Providing the inline argument instructs IPython to show Matplotlib plot images inline, within your cell outputs, enabling you to include charts inside your notebooks. Be sure to include this magic before you import Matplotlib, as it may not work if you do not; many import it at the start of their notebook, in the first code cell. 如果你想要画图(plot),那么一定要使用这个 magic 去输出图像

  1. 统计时间
1
2
n =100
%timeit -n 3 sum(range(n))
  1. 对 latex 的支持
1
2
3
4
5
6
7
%%latex
Some important equations:$E = mc^2$
$e^{i pi} = -1$


files = !ls | head -3
print(files)
  1. Extensions (扩展包是非常有用的)
1
2
3
4
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
jupyter nbextension enable spellchecker/main
jupyter nbextension enable codefolding/main

This will install the jupyter_contrib_nbextensions package in Python, install it in Jupyter, and then enable the spell check and code folding extensions.

具体的扩展包,是可以到网上找的。

  1. Enhancing Charts with Seaborn

主要的功能:实现更加好看。seaborn 实现效果更加好看

1
2
3
import matplotlib.pyplot as plt
import seaborn as sns
data = sns.load_dataset("tips")
1
plt.scatter(data.total_bill, data.tip);
1
sns.set(style="darkgrid")plt.scatter(data.total_bill, data.tip);
  1. Executing External Code

能够导入外部的项目或者包是非常重要的。 Jupyter also lets you %load and %run external scripts to support better organised, larger-scale projects and reusability.

We can load this simply by writing a one-line code cell, like so:

1
%load imports.py

load 相当于 python 文件中的import, 导入文件中的函数,但并没有执行。

The %run magic is similar, except it will execute the code and display any output, including Matplotlib plots. You can even execute entire notebooks this way, but remember that not all code truly belongs in a notebook. Let’s check out an example of this magic; consider a file containing the following short script.

run 命令是真正的运行。然后会将结果显示到 jupyter 的输出中(包括图像)。并且在jupyter 中是可以传参的,将一些参数可以加入到其中。类似命令行。

If you wish to pass arguments to a script, simply list them explicitly after the filename %run my_file.py 0 “Hello, World!” or using variables %run $filename {arg0} {arg1}. Additionally, use the -p option to run the code through the Python profiler.

  1. nbconvert

nbconvert: Convert Notebooks to other formats

1
2
3
pip install nbconvert
sudo apt-get install pandoc # install pandoc
sudo  apt-get install texlive-xetex texlive-fonts-recommended texlive-generic-recommended # install latex
  1. Running Jupyter Notebooks in Docker Containers

可以在docker 中使用 jupyter notebook

  1. jupyter notebook 的撤销功能

方法一 先按esc进入命令模式,即左侧线为蓝色(为绿色时是编辑模式),按z键即可恢复

方法二 如果是运行过的代码 直接运行

history 1 方法三 功能栏 edit -> undo delete cell