New findings in Numpy



1, Ellipsis

>>> a = np.arange(6).reshape(2,3)
>>> a
array([[0, 1, 2], [3, 4, 5]])
>>> for x in np.nditer(a, op_flags=['readwrite']):
                                                      x[...] = 2 * x
>>> a
array([[ 0, 2, 4], [ 6, 8, 10]])

2, array or matrix

  • Operator *dot(), and multiply():
    • For array‘``*``’ means element-wise multiplication, and the dot() function is used for matrix multiplication.
    • For matrix‘``*``’ means matrix multiplication, and the multiply() function is used for element-wise multiplication.
  • Handling of vectors (one-dimensional arrays)
    • For array, the vector shapes 1xN, Nx1, and N are all different things. Operations like A[:,1] return a one-dimensional array of shape N, not a two-dimensional array of shape Nx1. Transpose on a one-dimensional array does nothing.
    • For matrixone-dimensional arrays are always upconverted to 1xN or Nx1 matrices(row or column vectors). A[:,1] returns a two-dimensional matrix of shape Nx1.
  • Handling of higher-dimensional arrays (ndim > 2)
    • array objects can have number of dimensions > 2;
    • matrix objects always have exactly two dimensions.
  • Convenience attributes
    • array has a .T attribute, which returns the transpose of the data.
    • matrix also has .H, .I, and .A attributes, which return the conjugate transpose, inverse, and asarray() of the matrix, respectively.
  • Convenience constructor
    • The array constructor takes (nested) Python sequences as initializers. As in, array([[1,2,3],[4,5,6]]).
    • The matrix constructor additionally takes a convenient string initializer. As in matrix("[1 2 3; 4 5 6]").


3, np.ogrid[0:9.,0:6.]

Out[165]: 
[array([[ 0.],
        [ 1.],
        [ 2.],
        [ 3.],
        [ 4.],
        [ 5.],
        [ 6.],
        [ 7.],
        [ 8.]]), array([[ 0.,  1.,  2.,  3.,  4.,  5.]])]

np.mgrid[0:9.,0:6.]
Out[166]: 
array([[[ 0.,  0.,  0.,  0.,  0.,  0.],
        [ 1.,  1.,  1.,  1.,  1.,  1.],
        [ 2.,  2.,  2.,  2.,  2.,  2.],
        [ 3.,  3.,  3.,  3.,  3.,  3.],
        [ 4.,  4.,  4.,  4.,  4.,  4.],
        [ 5.,  5.,  5.,  5.,  5.,  5.],
        [ 6.,  6.,  6.,  6.,  6.,  6.],
        [ 7.,  7.,  7.,  7.,  7.,  7.],
        [ 8.,  8.,  8.,  8.,  8.,  8.]],
       [[ 0.,  1.,  2.,  3.,  4.,  5.],
        [ 0.,  1.,  2.,  3.,  4.,  5.],
        [ 0.,  1.,  2.,  3.,  4.,  5.],
        [ 0.,  1.,  2.,  3.,  4.,  5.],
        [ 0.,  1.,  2.,  3.,  4.,  5.],
        [ 0.,  1.,  2.,  3.,  4.,  5.],
        [ 0.,  1.,  2.,  3.,  4.,  5.],
        [ 0.,  1.,  2.,  3.,  4.,  5.],
        [ 0.,  1.,  2.,  3.,  4.,  5.]]])

4, Matlab & numpy comparison Table  (Here):

help func info(func) or help(func) or func? (in Ipython)
type func source(func) or func?? (in Ipython)
a && b a and b
a || b a or b
1*i1*j1i1j 1j
eps np.spacing(1)
ode45 scipy.integrate.ode(f).set_integrator('dopri5')
ode15s scipy.integrate.ode(f).set_integrator('vode',method='bdf', order=5)
a(2,:) a[1] or a[1,:]
a(1:5,:) a[0:5] or a[:5] or a[0:5,:]
a(end-4:end,:) a[-5:]
a(1:3,5:9) a[0:3][:,4:9]
a([2,4,5],[1,3]) a[ix_([1,3,4],[0,2])]
a(3:2:21,:) a[ 2:21:2,:]
a(1:2:end,:) a[ ::2,:]
a(end:-1:1,:) or flipud(a) a[ ::-1,:]
a([1:end 1],:) a[r_[:len(a),0]]
a.' a.transpose() or a.T
a' a.conj().transpose() or a.conj().T
a * b a.dot(b)
a .* b a * b
a./b a/b
a.^3 a**3
(a>0.5) (a>0.5)
find(a>0.5) nonzero(a>0.5)
a(:,find(v>0.5)) a[:,nonzero(v>0.5)[0]]
a(:,find(v>0.5)) a[:,v.T>0.5]
a(a<0.5)=0 a[a<0.5]=0
a .* (a>0.5) a * (a>0.5)
a(:) = 3 a[:] = 3
y=x y = x.copy()
y=x(2,:) y = x[1,:].copy()
y=x(:) y = x.flatten()
1:10 arange(1.,11.) or r_[1.:11.] orr_[1:10:10j]
0:09 arange(10.) or r_[:10.] orr_[:9:10j]
[1:10]' arange(1.,11.)[:, newaxis]
zeros(3,4) zeros((3,4))
zeros(3,4,5) zeros((3,4,5))
ones(3,4) ones((3,4))
eye(3) eye(3)
diag(a) diag(a)
diag(a,0) diag(a,0)
rand(3,4) random.rand(3,4)
linspace(1,3,4) linspace(1,3,4)
[x,y]=meshgrid(0:8,0:5) mgrid[0:9.,0:6.] or meshgrid(r_[0:9.],r_[0:6.]
ogrid[0:9.,0:6.] or ix_(r_[0:9.],r_[0:6.]
[x,y]=meshgrid([1,2,4],[2,4,5]) meshgrid([1,2,4],[2,4,5])
boardcasting ix_([1,2,4],[2,4,5])
repmat(a, m, n) tile(a, (m, n))
[a b] concatenate((a,b),1) or hstack((a,b)) orcolumn_stack((a,b)) or c_[a,b]
[a; b] concatenate((a,b)) or vstack((a,b)) or r_[a,b]
max(max(a)) a.max()
max(a) a.max(0)
max(a,[],2) a.max(1)
max(a,b) maximum(a, b)
norm(v) sqrt(dot(v,v)) or np.linalg.norm(v)
a & b logical_and(a,b)
a | b logical_or(a,b)
bitand(a,b) a & b
bitor(a,b) a | b
inv(a), pinv(a) linalg.inv(a), linalg.pinv(a)
rank(a) linalg.matrix_rank(a)
a\b linalg.solve(a,b) if a is square; linalg.lstsq(a,b) otherwise
b/a Solve a.T x.T = b.T instead
[U,S,V]=svd(a) U, S, Vh = linalg.svd(a), V =Vh.T
chol(a) linalg.cholesky(a).T
[V,D]=eig(a) D,V = linalg.eig(a)
[V,D]=eig(a,b) V,D = np.linalg.eig(a,b)
[V,D]=eigs(a,k)  find the k largest eigenvectors!
[Q,R,P]=qr(a,0) Q,R = scipy.linalg.qr(a)
[L,U,P]=lu(a) L,U = scipy.linalg.lu(a) or LU,P=scipy.linalg.lu_factor(a)
conjgrad scipy.sparse.linalg.cg
fft(a), ifft(a) fft(a), ifft(a)
sort(a) sort(a) or a.sort()
[b,I] = sortrows(a,i) I = argsort(a[:,i]), b=a[I,:]
regress(y,X) linalg.lstsq(X,y)
decimate(x, q) scipy.signal.resample(x,len(x)/q)

留言