import numpy as np
data = np.array([[0.95, -0.24, -0.88],
[0.56, 0.23, 0.91]])
print(data)
[[ 0.95 -0.24 -0.88]
[ 0.56 0.23 0.91]]
print(data.ndim)
print(data.shape)
print(data.dtype)
1
(100,)
float64
MathJax数学公式的表达
#$e^{i\pi}+1=0$
e i π + 1 = 0 e^{i\pi}+1=0 eiπ+1=0
%magic查看关于各个命令的说明
%magic
嵌入图表
%matplotlib
%matplotlib inline
#inline表示将图表嵌入到Notebook中
import pylab as pl
pl.seed(1)
data = pl.randn(100)
pl.plot(data)
[]
用%config配置IPython中各种可配置对象
将PNG修改为SVG
%config InlineBackend.figure_format="svg"
pl.plot(data)
[]
(CSDN无法显示SVG格式图片)
输出模式转为GUI界面
选择界面库:gtk,osx,qt,qt4,tk,wx
%matplotlib qt4
pl.plot(data)
Warning: Cannot change to a different GUI toolkit: qt4. Using tk instead.
[]
内容大纲
%timeit 调用timeit模块对单行语句重复执行,计算执行时间
%%timeit 单元中
time 相比 timeit 只执行一次代码
%%capture 将单元格的输出保存为一个对象
%%prun 调用 profile 模块,对代码进行性能分析,如运行次数显示
%debug 调试代码
执行前设置断点
执行错误后,调用堆栈
%%func_debug 指定中断运行的函数
a = [1, 2, 3]
%timeit a[1] = 10
58.4 ns ± 0.173 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
%%timeit
a=[]
for i in range(10):
a.append(i)
1.36 µs ± 10.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%%time
a = []
for i in range(100000):
a.append(i)
Wall time: 20 ms
%%capture time_results
# random.shuffle() 打乱不同长度列表顺序
# %time记下shuffle()时间
import random
for n in [1000,5000,10000, 50000, 100000, 500000]:
print "n={0}".format(n)
alist = range(n)
%time random.shuffle(alist)
```python
%%prun
def fib(n):
if n < 2:
return 1
else:
return fib(n-1) + fib(n-2)
def fib_fast(n, a=1, b=1):
if n == 1:
return b
else:
return fib_fast(n-1, b, a+b)
fib(20)
fib_fast(20)
返回prun的分析结果——两个函数分别调用的次数
import math
def sinc(x):
return math.sin(x)/x
[sinc(x) for x in range(5)]
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
in ()
4 return math.sin(x)/x
5
----> 6 [sinc(x) for x in range(5)]
in (.0)
4 return math.sin(x)/x
5
----> 6 [sinc(x) for x in range(5)]
in sinc(x)
2
3 def sinc(x):
----> 4 return math.sin(x)/x
5
6 [sinc(x) for x in range(5)]
ZeroDivisionError: float division by zero
%debug
以下为机器调试过程,此处不显示