numpy入门学习笔记
2016-06-14 15:23
441 查看
本文介绍了简单的numpy用法,作为入门可以参考
from numpy import * a = array([2,2,2]) print(a.dtype) print(a)
int64 [2 2 2]
b = array( [ (1.5,2,3), (4,5,6) ] ) c = array( [ [1,2], [3,4] ], dtype=complex ) print(b) print(c)
[[ 1.5 2. 3. ] [ 4. 5. 6. ]] [[ 1.+0.j 2.+0.j] [ 3.+0.j 4.+0.j]]
print(zeros((3,2))) print(ones((2,3,4), dtype=int16)) print(empty((2,3)))
[[ 0. 0.] [ 0. 0.] [ 0. 0.]] [[[1 1 1 1] [1 1 1 1] [1 1 1 1]] [[1 1 1 1] [1 1 1 1] [1 1 1 1]]] [[ 1.39069238e-309 1.39069238e-309 1.39069238e-309] [ 1.39069238e-309 1.39069238e-309 1.39069238e-309]]
print(arange( 10, 30, 5 )) b = linspace(0,pi,3) print(b) #other func: array, zeros, zeros_like, ones, ones_like, empty, empty_like, arange, linspace, rand, randn, fromfunction, fromfile
[10 15 20 25] [ 0. 1.57079633 3.14159265]
a = arange(6) print a b = arange(12).reshape(4,3) print b c = arange(24).reshape(2,3,4) print c print arange(10000) print arange(10000).reshape(100,100)
[0 1 2 3 4 5] [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]] [ 0 1 2 ..., 9997 9998 9999] [[ 0 1 2 ..., 97 98 99] [ 100 101 102 ..., 197 198 199] [ 200 201 202 ..., 297 298 299] ..., [9700 9701 9702 ..., 9797 9798 9799] [9800 9801 9802 ..., 9897 9898 9899] [9900 9901 9902 ..., 9997 9998 9999]]
a = array([20,30,40,50]) b = arange(4) print(a-b) print(b**2) print(10*sin(a)) print(a<35) a+=b print a print a.sum() print a.min() print a.max()
[20 29 38 47] [0 1 4 9] [ 9.12945251 -9.88031624 7.4511316 -2.62374854] [ True True False False] [20 31 42 53] 146 20 53
A = array( [[1,1],[0,1]] ) B = array( [[2,0],[3,4]] ) print A*B print dot(A,B)
[[2 0] [0 4]] [[5 4] [3 4]]
b = arange(12).reshape(3,4) print b print b.sum(axis=0) print b.min(axis=1) print b.cumsum(axis=1) a = arange(3) print exp(a) print sqrt(a) #更多函数all, alltrue, any, apply along axis, argmax, #argmin, argsort, average, bincount, ceil, clip, conj, #conjugate, corrcoef, cov, cross, cumprod, cumsum, diff, #dot, floor, inner, inv, lexsort, max, maximum, mean, median, #min, minimum, nonzero, outer, prod, re, round, sometrue, sort, #std, sum, trace, transpose, var, vdot, vectorize, where 参见:http://scipy.org/Numpy_Example_List
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [12 15 18 21] [0 4 8] [[ 0 1 3 6] [ 4 9 15 22] [ 8 17 27 38]] [ 1. 2.71828183 7.3890561 ] [ 0. 1. 1.41421356]
a = arange(10)**3 print a print a[2] print a[2:5] a[:6:2] = -1000 print a print a[::-1]
[ 0 1 8 27 64 125 216 343 512 729] 8 [ 8 27 64] [-1000 1 -1000 27 -1000 125 216 343 512 729] [ 729 512 343 216 125 -1000 27 -1000 1 -1000]
b = fromfunction(lambda x,y:10*x+y,(5,4),dtype=int) print b print b[2,3] print b[0:5,1] print b[:,1] print b[1:3,1]
[[ 0 1 2 3] [10 11 12 13] [20 21 22 23] [30 31 32 33] [40 41 42 43]] 23 [ 1 11 21 31 41] [ 1 11 21 31 41] [11 21]
a = floor(10*random.random((3,4))) print a print a.shape print a.ravel() a.shape = (6,2) print a.transpose()
[[ 7. 1. 2. 3.] [ 8. 4. 4. 5.] [ 6. 5. 7. 7.]] (3, 4) [ 7. 1. 2. 3. 8. 4. 4. 5. 6. 5. 7. 7.] [[ 7. 2. 8. 4. 6. 7.] [ 1. 3. 4. 5. 5. 7.]]
a = floor(10*random.random((2,2))) b = floor(10*random.random((2,2))) print vstack((a,b)) print hstack((a,b))
[[ 1. 9.] [ 0. 0.] [ 3. 3.] [ 3. 8.]] [[ 1. 9. 3. 3.] [ 0. 0. 3. 8.]]
a=array([4.,2.]) b=array([2.,8.]) print a[:,newaxis] print column_stack((a[:,newaxis],b[:,newaxis])) print vstack((a[:,newaxis],b[:,newaxis]))
[[ 4.] [ 2.]] [[ 4. 2.] [ 2. 8.]] [[ 4.] [ 2.] [ 2.] [ 8.]]
print r_[1:4,0,4]
[1 2 3 0 4]
a = floor(10*random.random((2,12))) print a print hsplit(a,3) # Split a into 3 print hsplit(a,(3,4)) # Split a after the third and the fourth column
[[ 7. 7. 9. 2. 9. 2. 2. 9. 1. 2. 4. 3.] [ 7. 9. 9. 1. 9. 5. 2. 8. 8. 6. 9. 6.]] [array([[ 7., 7., 9., 2.], [ 7., 9., 9., 1.]]), array([[ 9., 2., 2., 9.], [ 9., 5., 2., 8.]]), array([[ 1., 2., 4., 3.], [ 8., 6., 9., 6.]])] [array([[ 7., 7., 9.], [ 7., 9., 9.]]), array([[ 2.], [ 1.]]), array([[ 9., 2., 2., 9., 1., 2., 4., 3.], [ 9., 5., 2., 8., 8., 6., 9., 6.]])]
c = a print c is a print c.base is a c = a.view() print c is a print c.base is a c = a.copy() print c is a print c.base is a
True False False True False False
#创建数组 #arange, array, copy, empty, empty_like, eye, fromfile, fromfunction, identity, linspace, logspace, mgrid, ogrid, ones, ones_like, r , zeros, zeros_like #转化 #astype, atleast 1d, atleast 2d, atleast 3d, mat #操作 #array split, column stack, concatenate, diagonal, dsplit, dstack, hsplit, hstack, item, newaxis, ravel, repeat, reshape, resize, squeeze, swapaxes, take, transpose, vsplit, vstack #询问 #all, any, nonzero, where #排序 #argmax, argmin, argsort, max, min, ptp, searchsorted, sort #运算 #choose, compress, cumprod, cumsum, inner, fill, imag, prod, put, putmask, real, sum #基本统计 #cov, mean, std, var #基本线性代数 #cross, dot, outer, svd, vdot
a = arange(12)**2 # the first 12 square numbers i = array( [ 1,1,3,8,5 ] ) # an array of indices print a[i] # the elements of a at the positions i j = array( [ [ 3, 4], [ 9, 7 ] ] ) # a bidimensional array of indices print a[j] # the same shape as j
[ 1 1 9 64 25] [[ 9 16] [81 49]]
palette = array( [ [0,0,0], # black ... [255,0,0], # red ... [0,255,0], # green ... [0,0,255], # blue ... [255,255,255] ] ) # white image = array( [ [ 0, 1, 2, 0 ], # each value corresponds to a color in the palette ... [ 0, 3, 4, 0 ] ] ) print palette[image] # the (2,4,3) color image
[[[ 0 0 0] [255 0 0] [ 0 255 0] [ 0 0 0]] [[ 0 0 0] [ 0 0 255] [255 255 255] [ 0 0 0]]]
a = arange(12).reshape(3,4) print a i = array( [ [0,1], # indices for the first dim of a [1,2] ] ) j = array( [ [2,1], # indices for the second dim [3,3] ] ) print a[i,j] # i and j must have equal shape print a[i,2] print a[:,j] # i.e., a[ : , j]
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[ 2 5] [ 7 11]] [[ 2 6] [ 6 10]] [[[ 2 1] [ 3 3]] [[ 6 5] [ 7 7]] [[10 9] [11 11]]]
a = arange(12).reshape(3,4) b = a > 4 print b # b is a boolean with a's shape print a[b] # 1d array with the selected elements
[[False False False False] [False True True True] [ True True True True]] [ 5 6 7 8 9 10 11]
a = arange(12).reshape(3,4) b1 = array([False,True,True]) # first dim selection b2 = array([True,False,True,False]) # second dim selection print a[b1,:] # selecting rows print a[b1] # same thing print a[:,b2] # selecting columns print a[b1,b2]
[[ 4 5 6 7] [ 8 9 10 11]] [[ 4 5 6 7] [ 8 9 10 11]] [[ 0 2] [ 4 6] [ 8 10]] [ 4 10]
a = array([2,3,4,5]) b = array([8,5,4]) c = array([5,4,6,8,3]) ax,bx,cx = ix_(a,b,c) print ax print bx print cx print ax.shape, bx.shape, cx.shape print ax+bx*cx
[[[2]] [[3]] [[4]] [[5]]] [[[8] [5] [4]]] [[[5 4 6 8 3]]] (4, 1, 1) (1, 3, 1) (1, 1, 5) [[[42 34 50 66 26] [27 22 32 42 17] [22 18 26 34 14]] [[43 35 51 67 27] [28 23 33 43 18] [23 19 27 35 15]] [[44 36 52 68 28] [29 24 34 44 19] [24 20 28 36 16]] [[45 37 53 69 29] [30 25 35 45 20] [25 21 29 37 17]]]
from numpy import * from numpy.linalg import * a = array([[1.0, 2.0], [3.0, 4.0]]) print a print a.transpose() print inv(a) u = eye(2) # unit 2x2 matrix; "eye" represents "I" print u j = array([[0.0, -1.0], [1.0, 0.0]]) print dot (j, j) # matrix product print trace(u) # trace y = array([[5.], [7.]]) print solve(a, y) print eig(j)
[[ 1. 2.] [ 3. 4.]] [[ 1. 3.] [ 2. 4.]] [[-2. 1. ] [ 1.5 -0.5]] [[ 1. 0.] [ 0. 1.]] [[-1. 0.] [ 0. -1.]] 2.0 [[-3.] [ 4.]] (array([ 0.+1.j, 0.-1.j]), array([[ 0.70710678+0.j , 0.70710678-0.j ], [ 0.00000000-0.70710678j, 0.00000000+0.70710678j]]))
A = matrix('1.0 2.0; 3.0 4.0') print A print type(A) # file where class is defined print A.T # transpose X = matrix('5.0 7.0') Y = X.T print Y print A*Y # matrix multiplication print A.I # inverse print solve(A, Y) # solving linear equation
[[ 1. 2.] [ 3. 4.]] <class 'numpy.matrixlib.defmatrix.matrix'> [[ 1. 3.] [ 2. 4.]] [[ 5.] [ 7.]] [[ 19.] [ 43.]] [[-2. 1. ] [ 1.5 -0.5]] [[-3.] [ 4.]]
A = arange(12) print A A.shape = (3,4) M = mat(A.copy()) print type(A)," ",type(M) print A print M
[ 0 1 2 3 4 5 6 7 8 9 10 11] <type 'numpy.ndarray'> <class 'numpy.matrixlib.defmatrix.matrix'> [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]
a = arange(30) a.shape = 2,-1,3 # -1 means "whatever is needed" print a.shape print a
(2, 5, 3) [[[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11] [12 13 14]] [[15 16 17] [18 19 20] [21 22 23] [24 25 26] [27 28 29]]]
#以上是numpy简要的demo #本人是matlab转numpy的,同样的朋友参考https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html将有所帮助
相关文章推荐
- python循环嵌套遍历前后关联的select下拉框
- python安装NumPy
- 深入解析Python中的线程同步方法
- python os模块
- 用PDB库调试Python程序
- python 字符串 大小写转换
- [教程]Python 求解任意闭区间的所有素数
- [教程]Python 求解任意闭区间的所有素数
- [教程]Python 求解任意闭区间的所有素数
- [教程]Python 求解任意闭区间的所有素数
- [教程]Python 求解任意闭区间的所有素数
- [教程]Python 求解任意闭区间的所有素数
- [教程]Python 求解任意闭区间的所有素数
- [教程]Python 求解任意闭区间的所有素数
- [教程]Python 求解任意闭区间的所有素数
- [教程]Python 求解任意闭区间的所有素数
- [教程]Python 求解任意闭区间的所有素数
- [教程]Python 求解任意闭区间的所有素数
- Python的模块与函数以及与自动化的结合
- Python的模块与函数以及与自动化的结合