Jupyther notebook 是一个可以把代码、图像、注释、公式和作图集于一处,从而实现可读性分析的一种灵活的工具。 它的核心在于展示与快速迭代。
看看各路大神,都用Jupyter notebook干了什么,自然就回答了这个问题。
使用快捷键可以大幅提高效率,让你欲罢不能。使用Cmd + Shift + P 调出命令面板。
Jupyter Notebook 有两种键盘输入模式。编辑模式,允许你往单元中键入代码或文本;这时的单元框线是绿色的。命令模式,键盘输入运行程序命令;这时的单元框线是灰色
在库、方法或变量的前面打上?,即可打开相关语法的帮助文档。
?str.replace
Docstring: L.append(object) -> None -- append object to end
Type: method_descriptor
?list.pop
Docstring:
L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
Type: method_descriptor
?dict.get
Docstring: D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
Type: method_descriptor
推荐使用matplotlib,可通过%matplotlib inline 激活
from pylab import *
from mpl_toolkits.mplot3d import Axes3D
fig = figure()
ax = Axes3D(fig)
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot')
show()
%run 可以运行.py格式的python代码——这是众所周知的。不那么为人知晓的事实是它也可以运行其它的jupyter notebook文件,这一点很有用。
%run ./ex1.py
Hello World! Hello Again I like typing this. This is fun. Yay! Printing. I`d much rather you 'not'. I "said" do not touch this.
one = "for the money"
two = "for the show"
three = "to get ready now go cat go"
%who str
one three two
有两种用于计时的jupyter magic命令: %%time 和 %timeit.当你有一些很耗时的代码,想要查清楚问题出在哪时,这两个命令非常给力。
%%time 会告诉你cell内代码的单次运行时间信息。
%%time
a = []
for i in range(10000000):
a.append(i)
CPU times: user 1.34 s, sys: 161 ms, total: 1.5 s Wall time: 1.52 s
%%timeit 使用了Python的 timeit 模块,该模块运行某语句100,000次(默认值),然后提供最快的3次的平均值作为结果。
%%timeit
a = []
for i in range(10):
a.append(i)
1000000 loops, best of 3: 1.26 µs per loop
如果你想要,其实可以把不同内核的代码结合到一个notebook里运行。
只需在每个单元格的起始,用Jupyter magics调用kernal的名称:
%%bash
for i in {1..5}
do
echo "i is $i"
done
i is 1 i is 2 i is 3 i is 4 i is 5