'1.20.1'
创建数组 1 2 3 array1 = np.array([1 , 2 , 3 , 4 , 5 ]) print (array1)
[1 2 3 4 5]
array([1, 2, 3, 4, 5])
1 2 3 4 5 array2 = np.array([[11 , 22 , 33 , 44 ], [22 , 33 , 44 , 55 ], [1 , 2 , 3 , 4 ], [3 , 4 , 5 , 6 ]])
array([[11, 22, 33, 44],
[22, 33, 44, 55],
[ 1, 2, 3, 4],
[ 3, 4, 5, 6]])
查看数组的基本属性 查看数组的大小 数组中元素的个数
16
查看数组的尺寸
(4, 4)
查看数组的维度
2
查看数组的数据类型
dtype('int32')
array([[11, 22, 33, 44],
[22, 33, 44, 55],
[ 1, 2, 3, 4],
[ 3, 4, 5, 6]])
array([[11, 22, 33, 44],
[22, 33, 44, 55],
[ 1, 2, 3, 4],
[ 3, 4, 5, 6]])
构建特殊数组 等差数组 1 2 3 4 5 np.arange(1 , 11 , 0.5 )
array([ 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. ,
6.5, 7. , 7.5, 8. , 8.5, 9. , 9.5, 10. , 10.5])
1 2 3 4 5 6 7 8 9 np.linspace(start=1 , stop=10 , num=10 , endpoint=True )
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
等比数组 1 2 3 4 5 6 np.logspace(start=1 , stop=10 , num=10 , endpoint=True , base=10 )
array([1.e+01, 1.e+02, 1.e+03, 1.e+04, 1.e+05, 1.e+06, 1.e+07, 1.e+08,
1.e+09, 1.e+10])
案例重现 1 2 3 4 5 6 7 8 9 10 11 n = 10000 x = np.linspace(0 , 2 *np.pi, num=n) y = np.abs (np.sin(x)) width = (2 *np.pi)/n S = np.sum (y*width) S
3.9995999670980282
生成全0数组
array([0., 0., 0., 0., 0.])
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
生成全1数组
array([1., 1., 1., 1., 1.])
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
生成单位矩阵
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
生成对角矩阵
array([[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]])
array([[11, 22, 33, 44],
[22, 33, 44, 55],
[ 1, 2, 3, 4],
[ 3, 4, 5, 6]])
array([11, 33, 3, 6])
数组间的运算 数组与标量间的运算
array([[ 1, 12, 23, 34],
[12, 23, 34, 45],
[-9, -8, -7, -6],
[-7, -6, -5, -4]])
array([[110, 220, 330, 440],
[220, 330, 440, 550],
[ 10, 20, 30, 40],
[ 30, 40, 50, 60]])
数组和数组间的运算 两个数组尺寸完全一致 1 2 array2 - np.ones_like(array2)
array([[10, 21, 32, 43],
[21, 32, 43, 54],
[ 0, 1, 2, 3],
[ 2, 3, 4, 5]])
1 2 (array2 - np.ones_like(array2) - np.eye(4 )) * np.eye(4 )
array([[ 9., 0., 0., 0.],
[ 0., 31., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 4.]])
1 2 (array2 - np.ones_like(array2) - np.eye(4 )) @ np.eye(4 )
array([[ 9., 21., 32., 43.],
[21., 31., 43., 54.],
[ 0., 1., 1., 3.],
[ 2., 3., 4., 4.]])
当数组尺寸不一致时怎么办? 应用广播运算规则
规则1:如果两个数组的维度数不相同,那么小维度数组的形状将会在最左边补1
规则2:如果两个数组的形状在任何一个维度上都不匹配,那么数组的形状会沿着维度为1的维度扩展以匹配另一个数组的形状
规则3:如果两个数组的形状在仍和一个维度上都不匹配并且没有仍和一个维度等于1,那么会引发异常
(4, 4)
1 2 array3 = np.array([1 , 2 , 3 , 4 ]) array3
array([1, 2, 3, 4])
(4,)
array([[10, 20, 30, 40],
[21, 31, 41, 51],
[ 0, 0, 0, 0],
[ 2, 2, 2, 2]])
1 2 array4 = np.ones((4 , 3 , 2 , 6 )) array5 = np.ones((2 , 2 , 1 ))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-35-879136073d6e> in <module>
----> 1 array4 - array5
ValueError: operands could not be broadcast together with shapes (4,3,2,6) (2,2,1)
练习题 1 2 3 4 array1 = np.array([[1 , 2 , 3 , 4 ], [5 , 6 , 7 , 8 ], [11 , 12 , 13 , 14 ], [22 , 25 , 28 , 29 ]])
1 2 array1 = array1 - np.eye(4 ) array1
array([[ 0., 2., 3., 4.],
[ 5., 5., 7., 8.],
[11., 12., 12., 14.],
[22., 25., 28., 28.]])
1 2 array1 = array1 + np.diag([1 , 2 , 3 , 4 ]) array1
array([[ 1., 2., 3., 4.],
[ 5., 7., 7., 8.],
[11., 12., 15., 14.],
[22., 25., 28., 32.]])
1 2 3 4 array1 - np.array([[0 , 2 , 0 , 0 ], [0 , 0 , 2 , 0 ], [0 , 0 , 0 , 2 ], [0 , 0 , 0 , 0 ]])
array([[ 1., 0., 3., 4.],
[ 5., 7., 5., 8.],
[11., 12., 15., 12.],
[22., 25., 28., 32.]])
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
1 2 3 array1 - np.eye(4 , k=1 ) * 2
array([[ 1., 0., 3., 4.],
[ 5., 7., 5., 8.],
[11., 12., 15., 12.],
[22., 25., 28., 32.]])
生成随机数构成的数组 生成无约束随机数 1 2 np.random.random((4 , 4 ))
array([[0.05959022, 0.20497504, 0.14549915, 0.28539865],
[0.03659693, 0.56834715, 0.29203641, 0.89289598],
[0.55004802, 0.67954465, 0.23020844, 0.51558984],
[0.16344608, 0.91540574, 0.24069967, 0.96999008]])
生成均匀分布随机数
array([[0.84102007, 0.11226738, 0.86117375, 0.18992005],
[0.43942631, 0.47379869, 0.59556254, 0.14174232],
[0.73160647, 0.23418576, 0.25115777, 0.6499373 ],
[0.62314336, 0.51291401, 0.48727339, 0.3538733 ]])
生成正态分布随机数
array([[ 1.4701293 , -0.65461595, -0.88510978, 0.53183953],
[ 1.47047998, 0.83511023, 0.07514323, -0.83907472],
[ 0.527289 , -0.10834703, 3.65280732, -2.2189723 ],
[-0.3130769 , -0.59899358, -0.14821391, -1.22187388]])
生成随机整数 1 2 np.random.randint(low=5 , size=(4 , 4 ))
array([[3, 4, 4, 0],
[4, 2, 0, 4],
[2, 4, 0, 1],
[1, 2, 4, 4]])
1 2 3 arr = np.random.randint(low=5 , high=10 , size=(4 , 4 )) arr
array([[7, 7, 9, 6],
[6, 5, 8, 9],
[9, 8, 6, 8],
[8, 6, 9, 9]])
将数组保存与读取 将数组保存为二进制文件并读取 保存单个数组
保存多个数组 1 2 np.savez('all_array' , arr1=array1, arr2=array2)
读取二进制Numpy数据 1 2 print (np.load('array.npy' ))
[[7 7 9 6]
[6 5 8 9]
[9 8 6 8]
[8 6 9 9]]
1 array_file = np.load('all_array.npz' )
['arr1', 'arr2']
array([[ 1., 2., 3., 4.],
[ 5., 7., 7., 8.],
[11., 12., 15., 14.],
[22., 25., 28., 32.]])
array([[11, 22, 33, 44],
[22, 33, 44, 55],
[ 1, 2, 3, 4],
[ 3, 4, 5, 6]])
将数组保存为txt文件并读取 保存 1 np.savetxt('array2.txt' , array2, fmt='%.2e' )
读取 1 np.loadtxt('array2.txt' )
array([[11., 22., 33., 44.],
[22., 33., 44., 55.],
[ 1., 2., 3., 4.],
[ 3., 4., 5., 6.]])
练习题 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 30 np.random.seed(2021 ) array1 = np.random.rand(3 , 3 ) * 10 print (array1)array2 = np.reshape(range (9 ), newshape=(3 , 3 )) array2 np.random.seed(2021 ) array3 = np.random.rand(3 , 3 ) * 10 print (array3)print (np.all (array1 == array3))print (np.any (np.array([True , False , True , False , False ])))np.savez('all_array.npz' , array1=array1, array2=array2) all_array = np.load('all_array.npz' ) new_array1, new_array2 = all_array['array1' ], all_array['array2' ] print (new_array1)print (new_array2)array4 = new_array1 + new_array2 np.save('array4' , array4)
[[6.05978279 7.33369361 1.38947157]
[3.12673084 9.97243281 1.28162375]
[1.78993106 7.52925429 6.62160514]]
[[6.05978279 7.33369361 1.38947157]
[3.12673084 9.97243281 1.28162375]
[1.78993106 7.52925429 6.62160514]]
True
True
[[6.05978279 7.33369361 1.38947157]
[3.12673084 9.97243281 1.28162375]
[1.78993106 7.52925429 6.62160514]]
[[0 1 2]
[3 4 5]
[6 7 8]]
数组元素的访问 1 2 3 np.random.seed(8888 ) array6 = np.random.randint(low=5 , high=30 , size=(6 ,)) array6
array([ 8, 27, 8, 13, 14, 5])
array([ 8, 27, 8])
1 2 3 np.random.seed(8888 ) array5 = np.random.randint(low=5 , high=30 , size=(6 , 6 )) print (array5)
[[ 8 27 8 13 14 5]
[17 27 21 29 20 24]
[14 11 16 29 18 26]
[20 7 24 16 27 5]
[11 9 10 14 27 13]
[10 11 9 22 8 19]]
索引
29
切片 取出某行
array([17, 27, 21, 29, 20, 24])
取出某列
array([ 8, 21, 16, 24, 10, 9])
取出指定的连续区域
array([[16, 29, 18],
[24, 16, 27],
[10, 14, 27]])
array([[14, 5],
[20, 24],
[18, 26]])
改变数组的形状 修改数组的尺寸 1 2 3 array1 = np.random.random(size=(3 , 4 )) array1
array([[0.13683338, 0.69298187, 0.29863099, 0.00703621],
[0.81543781, 0.41387772, 0.70436646, 0.81758299],
[0.08116234, 0.03800953, 0.60329194, 0.38941598]])
1 2 3 np.reshape(a=array1, newshape=(4 , 3 ))
array([[0.13683338, 0.69298187, 0.29863099],
[0.00703621, 0.81543781, 0.41387772],
[0.70436646, 0.81758299, 0.08116234],
[0.03800953, 0.60329194, 0.38941598]])
1 2 np.reshape(array1, newshape=(3 , 3 ))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-39-53c72e2b4c61> in <module>
1 # 修改尺寸前后,数组的大小必须保持一致
----> 2 np.reshape(array1, newshape=(3, 3))
<__array_function__ internals> in reshape(*args, **kwargs)
D:\Users\Python\Anaconda3.8\lib\site-packages\numpy\core\fromnumeric.py in reshape(a, newshape, order)
297 [5, 6]])
298 """
--> 299 return _wrapfunc(a, 'reshape', newshape, order=order)
300
301
D:\Users\Python\Anaconda3.8\lib\site-packages\numpy\core\fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
56
57 try:
---> 58 return bound(*args, **kwds)
59 except TypeError:
60 # A TypeError occurs if the object does have such a method in its
ValueError: cannot reshape array of size 12 into shape (3,3)
在指定的维度上增加一个维度1
(3, 4)
1 np.expand_dims(array1, 0 ).shape
(1, 3, 4)
1 np.expand_dims(array1, 1 ).shape
(3, 1, 4)
1 np.expand_dims(array1, -1 ).shape
(3, 4, 1)
删除维度数为1的维度 1 2 new_array1 = np.expand_dims(array1, (0 , 2 , 4 )) new_array1.shape
(1, 3, 1, 4, 1)
1 2 np.squeeze(new_array1).shape
(3, 4)
展平数组
array([0.13683338, 0.69298187, 0.29863099, 0.00703621, 0.81543781,
0.41387772, 0.70436646, 0.81758299, 0.08116234, 0.03800953,
0.60329194, 0.38941598])
array([0.13683338, 0.69298187, 0.29863099, 0.00703621, 0.81543781,
0.41387772, 0.70436646, 0.81758299, 0.08116234, 0.03800953,
0.60329194, 0.38941598])
1 2 array1.flatten(order='F' )
array([0.13683338, 0.81543781, 0.08116234, 0.69298187, 0.41387772,
0.03800953, 0.29863099, 0.70436646, 0.60329194, 0.00703621,
0.81758299, 0.38941598])
数组的组合 1 2 3 4 array2 = np.random.randint(low=5 , high=30 , size=(3 , 3 )) array3 = np.random.randint(low=5 , high=30 , size=(3 , 3 )) print (array2)print (array3)
[[ 6 10 7]
[23 25 9]
[25 15 26]]
[[17 20 8]
[28 28 13]
[ 6 29 29]]
横向组合 1 np.hstack([array2, array3])
array([[ 6, 10, 7, 17, 20, 8],
[23, 25, 9, 28, 28, 13],
[25, 15, 26, 6, 29, 29]])
1 np.concatenate([array2, array3], axis=1 )
array([[ 6, 10, 7, 17, 20, 8],
[23, 25, 9, 28, 28, 13],
[25, 15, 26, 6, 29, 29]])
纵向组合 1 np.vstack([array2, array3])
array([[ 6, 10, 7],
[23, 25, 9],
[25, 15, 26],
[17, 20, 8],
[28, 28, 13],
[ 6, 29, 29]])
1 np.concatenate([array2, array3], axis=0 )
array([[ 6, 10, 7],
[23, 25, 9],
[25, 15, 26],
[17, 20, 8],
[28, 28, 13],
[ 6, 29, 29]])
切割 1 2 array4 = np.concatenate([array2, array3], axis=0 ) array4
array([[ 6, 10, 7],
[23, 25, 9],
[25, 15, 26],
[17, 20, 8],
[28, 28, 13],
[ 6, 29, 29]])
横向切割
[array([[ 6, 10, 7],
[23, 25, 9]]),
array([[25, 15, 26],
[17, 20, 8]]),
array([[28, 28, 13],
[ 6, 29, 29]])]
1 np.split(array4, 3 , axis=0 )
[array([[ 6, 10, 7],
[23, 25, 9]]),
array([[25, 15, 26],
[17, 20, 8]]),
array([[28, 28, 13],
[ 6, 29, 29]])]
纵向切割
[array([[ 6],
[23],
[25],
[17],
[28],
[ 6]]),
array([[10],
[25],
[15],
[20],
[28],
[29]]),
array([[ 7],
[ 9],
[26],
[ 8],
[13],
[29]])]
1 np.split(array4, 3 , axis=1 )
[array([[ 6],
[23],
[25],
[17],
[28],
[ 6]]),
array([[10],
[25],
[15],
[20],
[28],
[29]]),
array([[ 7],
[ 9],
[26],
[ 8],
[13],
[29]])]
排序 直接排序
array([[ 6, 10, 7],
[23, 25, 9],
[25, 15, 26],
[17, 20, 8],
[28, 28, 13],
[ 6, 29, 29]])
array([[ 6, 10, 7],
[ 6, 15, 8],
[17, 20, 9],
[23, 25, 13],
[25, 28, 26],
[28, 29, 29]])
array([[ 6, 7, 10],
[ 9, 23, 25],
[15, 25, 26],
[ 8, 17, 20],
[13, 28, 28],
[ 6, 29, 29]])
间接排序 对数组元素的下标进行排序
array([[ 6, 10, 7],
[23, 25, 9],
[25, 15, 26],
[17, 20, 8],
[28, 28, 13],
[ 6, 29, 29]])
1 2 index = np.argsort(array4, axis=0 ) index
array([[0, 0, 0],
[5, 2, 3],
[3, 3, 1],
[1, 1, 4],
[2, 4, 2],
[4, 5, 5]], dtype=int64)
1 array4[:, 0 ][index[:, 0 ]]
array([ 6, 6, 17, 23, 25, 28])
去重和重复 去重 将数组中重复元素删除,并将剩下的元素按照从小到大的顺序进行排列
array([ 6, 7, 8, 9, 10, 13, 15, 17, 20, 23, 25, 26, 28, 29])
重复 1 2 array1 = np.array([1 , 2 , 3 , 4 ]) array1
array([1, 2, 3, 4])
array([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])
array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4])
简单的二元函数
324
array([[ 6, 10, 7],
[23, 25, 9],
[25, 15, 26],
[17, 20, 8],
[28, 28, 13],
[ 6, 29, 29]])
array([105, 127, 92])
array([23, 57, 66, 45, 69, 64])
1 2 np.argmin(array4, axis=0 )
array([0, 0, 0], dtype=int64)
1 np.argmin(array4, axis=1 )
array([0, 2, 1, 2, 2, 0], dtype=int64)
矩阵相关操作 创建矩阵 1 2 matrix4 = np.mat(array4) matrix4
matrix([[ 6, 10, 7],
[23, 25, 9],
[25, 15, 26],
[17, 20, 8],
[28, 28, 13],
[ 6, 29, 29]])
矩阵的相关计算
matrix([[12, 20, 14],
[46, 50, 18],
[50, 30, 52],
[34, 40, 16],
[56, 56, 26],
[12, 58, 58]])
1 2 matrix4 * np.mat(np.eye(3 ))
matrix([[ 6., 10., 7.],
[23., 25., 9.],
[25., 15., 26.],
[17., 20., 8.],
[28., 28., 13.],
[ 6., 29., 29.]])
案例实战 1 2 data_file = np.load('考试成绩.npz' )
['三班成绩', '个人成绩']
1 2 3 three_class_grade = data_file['三班成绩' ] three_class_grade.shape
(50, 3)
1 2 np.mean(three_class_grade, axis=0 )
array([77.56, 81.24, 85.96])
1 2 np.var(three_class_grade, axis=0 )
array([32.0864, 69.9024, 30.9984])
1 2 3 bool_array = three_class_grade > 90 bool_array
array([[False, False, False],
[False, False, False],
[False, False, True],
[False, False, True],
[False, True, False],
[False, False, False],
[False, True, False],
[False, False, False],
[False, False, False],
[False, False, False],
[False, False, False],
[False, False, False],
[False, False, False],
[False, False, False],
[False, False, False],
[False, False, True],
[False, False, True],
[False, False, False],
[False, False, False],
[False, False, False],
[False, True, False],
[False, False, False],
[False, False, False],
[False, False, False],
[False, True, True],
[False, False, False],
[False, False, False],
[False, False, False],
[False, False, True],
[False, False, False],
[False, False, False],
[False, False, False],
[False, False, False],
[False, True, True],
[False, False, True],
[False, True, False],
[False, False, False],
[False, False, False],
[False, False, False],
[False, False, False],
[False, False, False],
[False, False, True],
[False, False, False],
[False, False, True],
[False, True, True],
[False, True, False],
[False, False, False],
[False, True, False],
[False, False, False],
[False, False, True]])
1 np.sum (bool_array, axis=0 )
array([ 0, 9, 12])