NumPy 笔记目录
Array Creation
np.arange
1 | >>> import numpy as np |
np.array
1 | >>> import numpy as np |
Basic Operations
dot
1 | >>> A.dot(B) or |
axis
1 | >>> b = np.arange(12).reshape(3,4) |
1 | >>> b.sum(axis=0) # sum of each column |
1 | >>> b.min(axis=1) # min of each row |
1 | >>> b.cumsum(axis=1) # cumulative sum along each row |
Indexing, Slicing and Iterating
One-dimensional
1 | >>> a = np.arange(10)**3 |
1 | >>> a[: 6 : 2] = -1000 # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element (二次元) to -1000 |
1 | >>> a[ : :-1] # reversed a |
Multidimensional
1 | >>> def f(x,y): |
1 | >>> b = np.fromfunction(f,(5,4),dtype=int) |
1 | >>> b[1:3, : ] # each column in the second and third row of b |
1 | >>> c = np.array( [[[ 0, 1, 2], |
1 | >>> c.shape |
1 | >>> c[1,...] # same as c[1,:,:] or c[1] |
1 | >>> c[...,2] # same as c[:,:,2] |
Iterating
1 | >>> for row in b: |
key words: flat
Indexing with Arrays
1 | >>> a = np.arange(12).reshape(3,4) |
1 | >>> i = np.array([ [0,1], |
1 | >>> a[i,j] # i and j must have equal shape |
1 | >>> a[i] # equal to a[i, :] |
1 | >>> a[i,2] |
1 | >>> a[:,j] # i.e., a[ : , j] |
1 | >>> l = [i,j] |
1 | >>> s = np.array( [i,j] ) # s.shape = (2, 2, 2) |
Search max & min
1 | >>> time = np.linspace(20, 145, 5) # time scale |
1 | >>> ind = data.argmax(axis=0) # index of the maxima for each series |
1 | >>> time_max = time[ind] # times corresponding to the maxima |
1 | >>> time_max |
1 | >>> np.all(data_max == data.max(axis=0)) |
key words: data.argmax(axis=0), data.max(axis=0)
np.ix_()
1 | >>> a = np.array([2,3,4,5]) |
1 | >>> a = np.arange(10).reshape(2,5) |
key words: np.ix_()
,可以发现从 ax 到 cx,分别对应 4,3,5。
Shape Manipulation
Changing the shape
1 | >>> a = np.floor(10*np.random.random((3,4))) |
1 | >>> a.ravel() # flatten the array |
1 | >>> a.resize((2,6)) |
1 | >>> a.reshape(3,-1) # equal to a.reshape(3, 4) |
1 | >>> a = np.arange(30) |
key words: ravel, reshape, resize
Stacking together
1 | >>> a = np.floor(10*np.random.random((2,2))) |
1 | >>> b = np.floor(10*np.random.random((2,2))) |
1 | >>> np.vstack((a,b)) # vertical stack |
1 | >>> np.hstack((a,b)) # horizontal stack |
1 | >>> from numpy import newaxis |
1 | >>> a = np.array([4.,2.]) # a.shape = (2,) |
1 | >>> a[:, newaxis] # This allows to have a 2D columns vector a.shape = (2, 1) |
1 | >>> np.column_stack((a[:,newaxis],b[:,newaxis])) # equal to np.vstack(a, b) |
1 | >>> np.vstack((a[:,newaxis],b[:,newaxis])) # The behavior of vstack is different |
1 | >>> np.vstack((a, b)) |
1 | >>> x = np.arange(0,10,2) # x=([0,2,4,6,8]) |
1 | In complex cases, _r and _c are useful for creating arrays by stacking numbers along one axis. They allow the use of range literals (”:”) : |
key words: hstack, vstack, newaxis
,注意 Vector 的连接方法。
Splitting
1 | >>> a = np.floor(10*np.random.random((2,12))) |
1 | >>> np.hsplit(a,3) # Split a into 3 |
1 | >>> np.hsplit(a,(3,4)) # Split a after the third and the fourth column |
key words: hsplit
Copy and Views
1 | >>> c = a.view() |
1 | >>> d = a.copy() # a new array object with new data is created |
key words: view, base, copy
view
复制了 data 但不复制 shape,就是原始数据的 view; copy
完全复制一份。
引自官方手册