1.ndarray.ndim
数组的维度
2.ndarray.shape
m*n
3.ndarray.size
总元素和
4.ndarray.dtype
数据类型
5.ndarray.itemsize
元素字节大小
1 >>> import numpy as np 2 >>> a = np.arange(15).reshape(3, 5) 3 >>> a 4 array([[ 0, 1, 2, 3, 4], 5 [ 5, 6, 7, 8, 9], 6 [10, 11, 12, 13, 14]]) 7 >>> a.shape 8 (3, 5) 9 >>> a.ndim 10 2 11 >>> a.dtype.name 12 'int64' 13 >>> a.itemsize 14 8 15 >>> a.size 16 15 17 >>> type(a) 18 <type 'numpy.ndarray'> 19 >>> b = np.array([6, 7, 8]) 20 >>> b 21 array([6, 7, 8]) 22 >>> type(b) 23 <type 'numpy.ndarray'>
构造数组
1 >>> import numpy as np 2 >>> a = np.array([2,3,4]) 3 >>> a 4 array([2, 3, 4]) 5 >>> a.dtype 6 dtype('int64') 7 >>> b = np.array([1.2, 3.5, 5.1]) 8 >>> b.dtype 9 dtype('float64')
常见错误:
a = np.array(1,2,3,4) # WRONG
二维数组
1 >>> b = np.array([(1.5,2,3), (4,5,6)]) 2 >>> b 3 array([[ 1.5, 2. , 3. ], 4 [ 4. , 5. , 6. ]])
1 >>> c = np.array( [ [1,2], [3,4] ], dtype=complex ) 2 >>> c 3 array([[ 1.+0.j, 2.+0.j], 4 [ 3.+0.j, 4.+0.j]])
几种常用的构造数组
1 >>> np.zeros( (3,4) ) 2 array([[ 0., 0., 0., 0.], 3 [ 0., 0., 0., 0.], 4 [ 0., 0., 0., 0.]]) 5 >>> np.ones( (2,3,4), dtype=np.int16 ) # dtype can also be specified 6 array([[[ 1, 1, 1, 1], 7 [ 1, 1, 1, 1], 8 [ 1, 1, 1, 1]], 9 [[ 1, 1, 1, 1], 10 [ 1, 1, 1, 1], 11 [ 1, 1, 1, 1]]], dtype=int16) 12 >>> np.empty( (2,3) ) # uninitialized, output may vary 13 array([[ 3.73603959e-262, 6.02658058e-154, 6.55490914e-260], 14 [ 5.30498948e-313, 3.14673309e-307, 1.00000000e+000]])
一维数组
1 >>> np.arange( 10, 30, 5 ) 2 array([10, 15, 20, 25]) 3 >>> np.arange( 0, 2, 0.3 ) # it accepts float arguments. [0,2) stride:0.3 4 array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])
linspace
1 > from numpy import pi 2 >>> np.linspace( 0, 2, 9 ) # 9 numbers from 0 to 2 3 array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ]) 4 >>> x = np.linspace( 0, 2*pi, 100 ) # useful to evaluate function at lots of points 5 >>> f = np.sin(x)
打印数组
1 >>> a = np.arange(6) # 1d array 2 >>> print(a) 3 [0 1 2 3 4 5] 4 >>> 5 >>> b = np.arange(12).reshape(4,3) # 2d array 6 >>> print(b) 7 [[ 0 1 2] 8 [ 3 4 5] 9 [ 6 7 8] 10 [ 9 10 11]] 11 >>> 12 >>> c = np.arange(24).reshape(2,3,4) # 3d array 13 >>> print(c) 14 [[[ 0 1 2 3] 15 [ 4 5 6 7] 16 [ 8 9 10 11]] 17 [[12 13 14 15] 18 [16 17 18 19] 19 [20 21 22 23]]]
基本操作
1 >>> a = np.array( [20,30,40,50] ) 2 >>> b = np.arange( 4 ) 3 >>> b 4 array([0, 1, 2, 3]) 5 >>>b[::-1] 6 array([3, 2, 1, 0]) 7 >>> c = a-b 8 >>> c 9 array([20, 29, 38, 47]) 10 >>> b**2 11 array([0, 1, 4, 9]) 12 >>> 10*np.sin(a) 13 array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854]) 14 >>> a<35 15 array([ True, True, False, False])
算术运算
1 >>> A = np.array( [[1,1], 2 ... [0,1]] ) 3 >>> B = np.array( [[2,0], 4 ... [3,4]] ) 5 >>> A * B # elementwise product 6 array([[2, 0], 7 [0, 4]]) 8 >>> A @ B # matrix product 9 array([[5, 4], 10 [3, 4]]) 11 >>> A.dot(B) # another matrix product 12 array([[5, 4], 13 [3, 4]])
1 >>> a = np.ones((2,3), dtype=int) 2 >>> b = np.random.random((2,3)) 3 >>> a *= 3 4 >>> a 5 array([[3, 3, 3], 6 [3, 3, 3]]) 7 >>> b += a 8 >>> b 9 array([[ 3.417022 , 3.72032449, 3.00011437], 10 [ 3.30233257, 3.14675589, 3.09233859]]) 11 >>> a += b # b is not automatically converted to integer type 12 Traceback (most recent call last): 13 ... 14 TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('int64') with casting rule 'same_kind'
元素操作
1 >>> a = np.random.random((2,3)) 2 >>> a 3 array([[ 0.18626021, 0.34556073, 0.39676747], 4 [ 0.53881673, 0.41919451, 0.6852195 ]]) 5 >>> a.sum() 6 2.5718191614547998 7 >>> a.min() 8 0.1862602113776709 9 >>> a.max() 10 0.6852195003967595
1 >>> b = np.arange(12).reshape(3,4) 2 >>> b 3 array([[ 0, 1, 2, 3], 4 [ 4, 5, 6, 7], 5 [ 8, 9, 10, 11]]) 6 >>> 7 >>> b.sum(axis=0) # sum of each column 8 array([12, 15, 18, 21]) 9 >>> 10 >>> b.min(axis=1) # min of each row 11 array([0, 4, 8]) 12 >>> 13 >>> b.cumsum(axis=1) # cumulative sum along each row 14 array([[ 0, 1, 3, 6], 15 [ 4, 9, 15, 22], 16 [ 8, 17, 27, 38]])
1 >>> B = np.arange(3) 2 >>> B 3 array([0, 1, 2]) 4 >>> np.exp(B) 5 array([ 1. , 2.71828183, 7.3890561 ]) 6 >>> np.sqrt(B) 7 array([ 0. , 1. , 1.41421356]) 8 >>> C = np.array([2., -1., 4.]) 9 >>> np.add(B, C) 10 array([ 2., 0., 6.])
索引,切片,迭代
1 >>> a = np.arange(10)**3 2 >>> a 3 array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]) 4 >>> a[2] 5 8 6 >>> a[2:5] 7 array([ 8, 27, 64]) 8 >>> a[:6:2] = -1000 # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000 9 >>> a 10 array([-1000, 1, -1000, 27, -1000, 125, 216, 343, 512, 729]) 11 >>> a[ : :-1] # reversed a 12 array([ 729, 512, 343, 216, 125, -1000, 27, -1000, 1, -1000]) 13 >>> for i in a: 14 ... print(i**(1/3.)) 15 ... 16 nan 17 1.0 18 nan 19 3.0 20 nan 21 5.0 22 6.0 23 7.0 24 8.0 25 9.0
1 >>> def f(x,y): 2 ... return 10*x+y 3 ... 4 >>> b = np.fromfunction(f,(5,4),dtype=int) 5 >>> b 6 array([[ 0, 1, 2, 3], 7 [10, 11, 12, 13], 8 [20, 21, 22, 23], 9 [30, 31, 32, 33], 10 [40, 41, 42, 43]]) 11 >>> b[2,3] 12 23 13 >>> b[0:5, 1] # each row in the second column of b 14 array([ 1, 11, 21, 31, 41]) 15 >>> b[ : ,1] # equivalent to the previous example 16 array([ 1, 11, 21, 31, 41]) 17 >>> b[1:3, : ] # each column in the second and third row of b 18 array([[10, 11, 12, 13], 19 [20, 21, 22, 23]]) 20 >>> b[-1] # the last row. Equivalent to b[-1,:] 21 array([40, 41, 42, 43])
1 >>> c = np.array( [[[ 0, 1, 2], # a 3D array (two stacked 2D arrays) 2 ... [ 10, 12, 13]], 3 ... [[100,101,102], 4 ... [110,112,113]]]) 5 >>> c.shape 6 (2, 2, 3) 7 >>> c[1,...] # same as c[1,:,:] or c[1] 8 array([[100, 101, 102], 9 [110, 112, 113]]) 10 >>> c[...,2] # same as c[:,:,2] 11 array([[ 2, 13], 12 [102, 113]])
迭代
1 >>> for row in b: 2 ... print(row) 3 ... 4 [0 1 2 3] 5 [10 11 12 13] 6 [20 21 22 23] 7 [30 31 32 33] 8 [40 41 42 43]
1 >>> for element in b.flat: 2 ... print(element) 3 ... 4 0 5 1 6 2 7 3 8 10 9 11 10 12 11 13 12 20 13 21 14 22 15 23 16 30 17 31 18 32 19 33 20 40 21 41 22 42 23 43
np.around 返回四舍五入后的值,可指定精度。
1 >>> a = np.array([[0.1, 0.5, 0.6],[1.2,0.9,3.3]]) 2 >>> b = np.around(a) 3 >>> b 4 array([[0., 0., 1.], 5 [1., 1., 3.]]) 6 >>> a 7 array([[0.1, 0.5, 0.6], 8 [1.2, 0.9, 3.3]])
np.floor 返回不大于输入参数的最大整数。 即对于输入值 x ,将返回最大的整数 i ,使得 i <= x。 注意在Python中,向下取整总是从 0 舍入。
1 >>> c = np.floor(a) 2 >>> c 3 array([[0., 0., 0.], 4 [1., 0., 3.]])
np.ceil 函数返回输入值的上限,即对于输入 x ,返回最小的整数 i ,使得 i> = x。
1 >>> d = np.ceil(a) 2 >>> d 3 array([[1., 1., 1.], 4 [2., 1., 4.]])
将原数组分割
1 >>> a = np.floor(10*np.random.random((2,12))) 2 >>> a 3 array([[ 9., 5., 6., 3., 6., 8., 0., 7., 9., 7., 2., 7.], 4 [ 1., 4., 9., 2., 2., 1., 0., 6., 2., 2., 4., 0.]]) 5 >>> np.hsplit(a,3) # Split a into 3 6 [array([[ 9., 5., 6., 3.], 7 [ 1., 4., 9., 2.]]), array([[ 6., 8., 0., 7.], 8 [ 2., 1., 0., 6.]]), array([[ 9., 7., 2., 7.], 9 [ 2., 2., 4., 0.]])] 10 >>> np.hsplit(a,(3,4)) # Split a after the third and the fourth column 11 [array([[ 9., 5., 6.], 12 [ 1., 4., 9.]]), array([[ 3.], 13 [ 2.]]), array([[ 6., 8., 0., 7., 9., 7., 2., 7.], 14 [ 2., 1., 0., 6., 2., 2., 4., 0.]])]
1 >>> x = np.arange(16.0).reshape(4, 4) 2 >>> x 3 array([[ 0., 1., 2., 3.], 4 [ 4., 5., 6., 7.], 5 [ 8., 9., 10., 11.], 6 [12., 13., 14., 15.]]) 7 >>> np.vsplit(x, 2) 8 [array([[0., 1., 2., 3.], 9 [4., 5., 6., 7.]]), array([[ 8., 9., 10., 11.], 10 [12., 13., 14., 15.]])] 11 >>> np.vsplit(x, np.array([3, 6])) 12 [array([[ 0., 1., 2., 3.], 13 [ 4., 5., 6., 7.], 14 [ 8., 9., 10., 11.]]), array([[12., 13., 14., 15.]]), array([], shape=(0, 4), dtype=float64)]
1 >>> x = np.arange(8.0).reshape(2, 2, 2) 2 >>> x 3 array([[[0., 1.], 4 [2., 3.]], 5 [[4., 5.], 6 [6., 7.]]]) 7 >>> np.vsplit(x, 2) 8 [array([[[0., 1.], 9 [2., 3.]]]), array([[[4., 5.], 10 [6., 7.]]])]
浅拷贝
1 >>> c = a.view() 2 >>> c is a 3 False 4 >>> c.base is a # c is a view of the data owned by a 5 True 6 >>> c.flags.owndata 7 False 8 >>> 9 >>> c.shape = 2,6 # a's shape doesn't change 10 >>> a.shape 11 (3, 4) 12 >>> c[0,4] = 1234 # a's data changes 13 >>> a 14 array([[ 0, 1, 2, 3], 15 [1234, 5, 6, 7], 16 [ 8, 9, 10, 11]]) 17 Slicing an array returns a view of it: 18 19 >>> 20 >>> s = a[ : , 1:3] # spaces added for clarity; could also be written "s = a[:,1:3]" 21 >>> s[:] = 10 # s[:] is a view of s. Note the difference between s=10 and s[:]=10 22 >>> a 23 array([[ 0, 10, 10, 3], 24 [1234, 10, 10, 7], 25 [ 8, 10, 10, 11]])
深拷贝
1 >>> d = a.copy() # a new array object with new data is created 2 >>> d is a 3 False 4 >>> d.base is a # d doesn't share anything with a 5 False 6 >>> d[0,0] = 9999 7 >>> a 8 array([[ 0, 10, 10, 3], 9 [1234, 10, 10, 7], 10 [ 8, 10, 10, 11]])
numpy随机数
1 >>> import numpy as np 2 >>> a = np.random.rand(2,2) 3 >>> a 4 array([[0.75382796, 0.81290411], 5 [0.90656694, 0.0197535 ]]) 6 >>> b = np.random.uniform(1,10,(2,3)) 7 >>> b 8 array([[8.44836122, 2.95325268, 6.34505736], 9 [9.63767774, 7.5162236 , 8.02966445]]) 10 >>> c = np.random.randn(3,4) 11 >>> c 12 array([[ 0.10787345, -1.4670443 , 0.41602867, 0.51831032], 13 [-0.73956548, -1.22996858, -0.60736657, -2.54139988], 14 [-0.44398806, 1.37963897, -1.0837557 , -0.69188339]]) 15 >>> d = np.random.normal(0,1,(3,4)) 16 >>> d 17 array([[ 0.0481841 , 0.020283 , 0.05428642, -1.07924635], 18 [-1.30240176, 1.86423441, 0.51290432, 0.53366215], 19 [ 0.61771499, -1.30219363, 0.04899631, -0.1209856 ]]) 20 >>> x = np.random.randint(2,4,(4,5)) 21 >>> x 22 array([[2, 2, 2, 3, 3], 23 [2, 2, 2, 2, 2], 24 [2, 2, 2, 3, 3], 25 [2, 3, 3, 2, 3]]) 26 >>> y = np.random.random((2,3)) 27 >>> y 28 array([[0.47555261, 0.45704184, 0.51881348], 29 [0.11602502, 0.58367782, 0.27376024]])
1、numpy.random.rand(d0, d1, ..., dn)
作用:产生一个给定形状的数组(其实应该是ndarray对象或者是一个单值),数组中的值服从[0, 1)之间的均匀分布。
参数:d0, d, ..., dn : int,可选。如果没有参数则返回一个float型的随机数,该随机数服从[0, 1)之间的均匀分布。
返回值:ndarray对象或者一个float型的值
2、numpy.random.uniform(low=0.0, high=1.0, size=None)
作用:返回一个在区间[low, high)中均匀分布的数组,size指定形状。
参数:
low, high:float型或者float型的类数组对象。指定抽样区间为[low, high),low的默认值为0.0,hign的默认值为1.0
size:int型或int型元组。指定形状,如果不提供size,则返回一个服从该分布的随机数。
3、numpy.random.randn(d0, d1, ..., dn)
作用:返回一个指定形状的数组,数组中的值服从标准正态分布(均值为0,方差为1)。
参数:d0, d, ..., dn : int,可选。如果没有参数,则返回一个服从标准正态分布的float型随机数。
返回值:ndarray对象或者float
4、numpy.random.normal(loc=0.0, scale=1.0, size=None)
作用:返回一个由size指定形状的数组,数组中的值服从 \(\mu=loc, \sigma=scale\) 的正态分布。
参数:
loc : float型或者float型的类数组对象,指定均值 \(\mu\)
scale : float型或者float型的类数组对象,指定标准差 \(\sigma\)
size : int型或者int型的元组,指定了数组的形状。如果不提供size,且loc和scale为标量(不是类数组对象),则返回一个服从该分布的随机数。
输出:ndarray对象或者一个标量
5、numpy.random.randint(low, high=None, size=None, dtype='l')
作用:返回一个在区间[low, high)中离散均匀抽样的数组,size指定形状,dtype指定数据类型。
参数:
low, high:int型,指定抽样区间[low, high)
size:int型或int型的元组,指定形状
dypte:可选参数,指定数据类型,比如int,int64等,默认是np.int
返回值:如果指定了size,则返回一个int型的ndarray对象,否则返回一个服从该分布的int型随机数。
6、numpy.random.random(size=None)
作用:返回从[0, 1)之间均匀抽样的数组,size指定形状。
参数:
size:int型或int型的元组,如果不提供则返回一个服从该分布的随机数
返回值:float型或者float型的ndarray对象