10.Jupyter Magic-%who:列出所有的全局變量
不加任何參數(shù), %who 命令可以列出所有的全局變量。加上參數(shù) str 將只列出字符串型的全局變量。
In [1]: one = "for the money"
two = "for the show"
three = "to get ready now go cat go"
%who str
one three two
11.Jupyter Magic-計(jì)時(shí)
有兩種用于計(jì)時(shí)的jupyter magic命令: %%time 和 %timeit.當(dāng)你有一些很耗時(shí)的代碼,想要查清楚問題出在哪時(shí),這兩個(gè)命令非常給力。
仔細(xì)體會(huì)下我的描述哦。
%%time 會(huì)告訴你cell內(nèi)代碼的單次運(yùn)行時(shí)間信息。
In [4]: %%time
import time
for _ in range(1000):
time.sleep(0.01)# sleep for 0.01 seconds
CPU times: user 21.5 ms, sys: 14.8 ms, total: 36.3 ms
Wall time: 11.6 s
%%timeit 使用了Python的 timeit 模塊,該模塊運(yùn)行某語句100,000次(默認(rèn)值),然后提供最快的3次的平均值作為結(jié)果。
In [3]: import numpy
%timeit numpy.random.normal(size=100)
The slowest run took 7.29 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 5.5 μs per loop
12.Jupyter Magic-writefile and %pycat:導(dǎo)出cell內(nèi)容/顯示外部腳本的內(nèi)容
使用%%writefile magic可以保存cell的內(nèi)容到外部文件。 而%pycat功能相反,把外部文件語法高亮顯示(以彈出窗方式)。
In [7]: %%writefile pythoncode.py
import numpy
def append_if_not_exists(arr, x):
if x not in arr:
arr.append(x)
def some_useless_slow_function():
arr = list()
for i in range(10000):
x = numpy.random.randint(0, 10000)
append_if_not_exists(arr, x)
Writing pythoncode.py
In [8]: %pycat pythoncode.py
import numpy
def append_if_not_exists(arr, x):
if x not in arr:
arr.append(x)
def some_useless_slow_function():
arr = list()
for i in range(10000):
x = numpy.random.randint(0, 10000)
append_if_not_exists(arr, x)
13.Jupyter Magic-%prun:告訴你程序中每個(gè)函數(shù)消耗的時(shí)間
使用%prun+函數(shù)聲明會(huì)給你一個(gè)按順序排列的表格,顯示每個(gè)內(nèi)部函數(shù)的耗時(shí)情況,每次調(diào)用函數(shù)的耗時(shí)情況,以及累計(jì)耗時(shí)。
In [47]: %prun some_useless_slow_function()
26324 function calls in 0.556 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
10000 0.527 0.000 0.528 0.000 :2(append_if_not_exists)
10000 0.022 0.000 0.022 0.000 {method 'randint' of 'mtrand.RandomState' objects}
1 0.006 0.006 0.556 0.556 :6(some_useless_slow_function)
6320 0.001 0.000 0.001 0.000 {method 'append' of 'list' objects}
1 0.000 0.000 0.556 0.556 :1()
1 0.000 0.000 0.556 0.556 {built-in method exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
14.Jupyter Magic-用%pdb調(diào)試程序
Jupyter 有自己的調(diào)試界面The Python Debugger (pdb),使得進(jìn)入函數(shù)內(nèi)部檢查錯(cuò)誤成為可能。
Pdb中可使用的命令見鏈接
In [ ]: %pdb
def pick_and_take():
picked = numpy.random.randint(0, 1000)
raise NotImplementedError()
pick_and_take()
Automatic pdb calling has been turned ON
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
in ()
5 raise NotImplementedError()
6
----> 7 pick_and_take()
in pick_and_take()
3 def pick_and_take():
4 picked = numpy.random.randint(0, 1000)
----> 5 raise NotImplementedError()
6
7 pick_and_take()
NotImplementedError:
> (5)pick_and_take()
3 def pick_and_take():
4 picked = numpy.random.randint(0, 1000)
----> 5 raise NotImplementedError()
6
7 pick_and_take()
ipdb>
15.末句函數(shù)不輸出
有時(shí)候不讓末句的函數(shù)輸出結(jié)果比較方便,比如在作圖的時(shí)候,此時(shí),只需在該函數(shù)末尾加上一個(gè)分號(hào)即可。
In [4]: %matplotlib inline
from matplotlib import pyplot as plt
import numpy
x = numpy.linspace(0, 1, 1000)**1.5
In [5]: # Here you get the output of the function
plt.hist(x)
Out[5]:
(array([ 216., 126., 106., 95., 87., 81., 77., 73., 71., 68.]),
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ]),
)
In [6]: # By adding a semicolon at the end, the output is suppressed.
plt.hist(x);
16.運(yùn)行Shell命令
在notebook內(nèi)部運(yùn)行shell命令很簡(jiǎn)單,這樣你就可以看到你的工作文件夾里有哪些數(shù)據(jù)集。
In [7]: !ls *.csv
nba_2016.csv titanic.csv
pixar_movies.csv whitehouse_employees.csv
17.用LaTex寫公式
當(dāng)你在一個(gè)Markdown單元格里寫LaTex時(shí),它將用MathJax呈現(xiàn)公式:如
$$ P(A \mid B) = \frac{P(B \mid A) , P(A)}{P(B)} $$
會(huì)變成
18.在notebook內(nèi)用不同的內(nèi)核運(yùn)行代碼
如果你想要,其實(shí)可以把不同內(nèi)核的代碼結(jié)合到一個(gè)notebook里運(yùn)行。
只需在每個(gè)單元格的起始,用Jupyter magics調(diào)用kernal的名稱:
%%bash
%%HTML
%%python2
%%python3
%%ruby
%%perl
In [6]: %%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
聯(lián)系客服