在线notebook
notebook 参考大全
IPython notebook界面类似于一个在线的文档编辑器
该文档可以包含很多单元,单元内可以是文本也可以使代码,而且代码可以通过点击和按下shift+enter执行
并在下面单元中显示执行结果
全局变量可以在不同的cell中使用
代码的执行过程按照从上到下的顺序执行
单元分为以下几种
Markdown cells
使用Markdown 语法进行编辑组织的文本,一旦执行后,缩写的内容被格式化为Markdown 格式
Header cells
标题单元可以组织文本的大纲和递进关系
Raw cells
原生的单元内容,写什么就是什么
Code cells
可以获取IPython 的输入(i.e. Python code, %magics, !system calls, etc.)
选中后按下 Shift-Enter开始执行,结果显示在下一个cell中
如果想让结果显示在代码所在的cell则按下Ctrl-Enter
如果按下 Enter 则进入代码编辑模式
自动补全和提示功能
Tab
Shift+Tab
magic?
直接执行shell指令
ls
!echo 'hello'
加载模块和外部文件
%pylab inline
import numpy
%load
%load http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/integral_demo.py
绘图
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
plt.plot(np.random.rand(100));
科学计算
# what is this line all about?!? Answer in lecture 4
%pylab inline
from numpy import *
from numpy import random
# uniform random numbers ini [0,1]
random.rand(5,5)
矩阵
In [24]:
# a diagonal matrix
diag([1,2,3])
Out[24]:
array([[1, 0, 0],
[0, 2, 0],
[0, 0, 3]])
In [25]:
# diagonal with offset from the main diagonal
diag([1,2,3], k=1)
Out[25]:
array([[0, 1, 0, 0],
[0, 0, 2, 0],
[0, 0, 0, 3],
[0, 0, 0, 0]])
zeros and ones
In [26]:
zeros((3,3))
Out[26]:
array([[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]])
In [27]:
ones((3,3))
Out[27]:
array([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]])