Nov 21, 2011

numpy 笔记



NumPy for Matlab Users
有matlab函数相应的numpy函数
推荐使用 array
array: element wise operation
matrix: algebra operation
array shape N, 1xN, Nx1 是不同的

a = array([3,4])
b = a[:,newaxis]
c = a[newaxis,:]

a,b,c 分别为 2, 2×1, 1×2
[0 4]
[[0]
[4]]
[0 4]
a.T 还是 a
python reference and view
n = a
python assignment 是共享引用,并没有拷贝生成一个新的array
view 指对原有数据的另一个视图,没有拷贝数据
slicing (submatrix) 是 view
下面几个函数也是生成 view
asarray: always returns an object of type array
asmatrix or mat: always return an object of type matrix
asanyarray: always returns an array object or a subclass derived from it, depending on the input. For instance if you pass in a matrix it returns a matrix.
类似的 matrix.A 也是 view
而 matrix(array) 是 copy
numpy array 是 0-based indexing, C ordered.
如 2x3x4 array
最右边的 dimensionality 是最先排入的
indexing 取元素
slicing[inclusive, exclusive] 返回 array,
a[0:1] 为 array(a[0])
-1 对应最后元素
numpy.random 下有几个函数,生成随机数
rand 返回 [0,1)
randn
randint(low, size=()) 返回 [0,low)
randint(low, high, size=()) 返回 [low,high)
和 matlab 类似的函数
zeros
ones
eye
diag
linspace
mgrid, meshgrid
tile (matlab repmat)
EricsBroadcastingDoc
broad-casting:
In order to broadcast, the size of the trailing axes for both arrays in an operation must either be the same size or one of them must be one.
由于 numpy array 为 c-ordered,所以从右边的维度开始对应,两个array的每个维度必须相等,或其中一个为1(不存在视为1)
然后进行拷贝填充,最后element wise operation
上述为理解方便,实际上并没有拷贝填充这一步,broadcasting效率是很高的
Image (3d array): 256 x 256 x 3
Scale (1d array): 3
Result (3d array): 256 x 256 x 3
A (4d array): 8 x 1 x 6 x 1
B (3d array): 7 x 1 x 5
Result (4d array): 8 x 7 x 6 x 5
Tentative NumPy Tutorial
ndarray是numpy的基本数据类型,它的一些attributes
ndarray.ndim
ndarray.shape
ndarray.size (元素个数)
ndarray.dtype
ndarray.itemsize (每一个元素的字节大小)
ndarray.data (buffer)
创建 array 时可以指定 dtype
arange(10)
返回 0-9
arange(a,b,c)
返回 a, a+c, a+2c, ... 不等于或超过 b(b exclusive)
linspace(a,b,n)
[a,b] 取n个,b inclusive
a.min
a.max
a.sum
a.cumsum
a.reshape
a.ravel
一般来说 reshape, ravel 都返回 view,除非 a 是 submatrix
b[i] can be read an b[i, ]. In NumPy, this can also be written using dots as b[i,...].
… 意为满足维度的任意个:
numpy 会自动计算 …
vstack: first dimension
hstack: second dimension
dstack
column_stack
二维矩阵,vstack 就是垂直方向
hstack 水平方向
view (shallow copy)
.copy() (deep copy)
c = a.view()
c = a.copy()
indexing:
array 支持类似 python list 的indexing,还支持用 integer array 和 boolean array
使用 integer array 进行 indexing
令 indexed array 为 a
index array 为 i
1. 若 a shape 为 N,如 a = arange(N)
则 i 可以为任意 shape,返回和 i shape 相同的 array
2. 若 a 是多维的,如a = arange(12).reshape(3,4)
则 i 中元素代表第一维
返回 i shape array,内含 a去掉一维 的元素 array
3. 可以使用多个 index arrays,如 i,j
i 代表第一维, j 代表第二维
i,j shape 必须一致,否则使用 broadcasting
返回 i shape array,内含a去掉两维的元素
array index 可以用 python list 代替
如 a[[0,3]]
boolean array indexing:
1. single boolean array, shape 和 a 相同
则取 true 元素组成 shape N array
可用于赋值
2. 多个 boolean array,每个维度和a某维相同。类似 python list indexing (用整数取某维,boolean用true/false取)
ix_()
笛卡尔乘积
如 ax, bx, cx = ix_(a,b,c)
则对ax, bx, cx进行操作,等价于对
a x b x c 进行操作
ix_ 类似于 matlab 的 repmat

0 comments: