# 导入numpy 模块 1 import numpy as np 10 a = np.random.random((2,4)) 11 a 12 Out[5]: 13 array([[0.20974732, 0.73822026, 0.82760722, 0.050551 ], 14 [0.77337155, 0.06521922, 0.55524187, 0.59209907]]) # 求矩阵所有数据的和,最小值,最大值 22 np.sum(a) 23 Out[7]: 3.812057513268513 24 np.min(a) 25 Out[8]: 0.05055099733013646 26 np.max(a) 27 Out[9]: 0.8276072194278252 28 print("a=",a) 29 a= [[0.20974732 0.73822026 0.82760722 0.050551 ] 30 [0.77337155 0.06521922 0.55524187 0.59209907]] # axis=0 代表列, axis=1代表行
31 print("min",np.min(a)) 32 min 0.05055099733013646#求每列当中的最小值 33 print("lmin:",np.min(a,axis=0)) 34 lmin: [0.20974732 0.06521922 0.55524187 0.050551 ] 35 print("lmin:",np.min(a,axis=1)) 36 lmin: [0.050551 0.06521922] 37 print("sum:",np.sum(a,axis=1)) 38 sum: [1.8261258 1.98593171] # reshape 数据, 3行4列 39 A = np.arange(2,14).reshape(3,4) 40 A 41 Out[16]: 42 array([[ 2, 3, 4, 5], 43 [ 6, 7, 8, 9], 44 [10, 11, 12, 13]]) # ndarray中最小值,最大值的序号 45 print(np.argmin(A)) 46 0 47 print(np.argmax(A)) 48 11 49 print(np.mean(A)) 50 7.5 51 print(np.average(A)) 52 7.5 53 print(A.mean()) 54 7.5 # cumsum 迭代相加 69 A 70 Out[24]: 71 array([[ 2, 3, 4, 5], 72 [ 6, 7, 8, 9], 73 [10, 11, 12, 13]]) 81 print(A.cumsum()) 82 [ 2 5 9 14 20 27 35 44 54 65 77 90] 83 A 84 Out[27]: 85 array([[ 2, 3, 4, 5], 86 [ 6, 7, 8, 9], 87 [10, 11, 12, 13]])# clip(a, a_min, a_max) 将ndarray中的数据进行判断,小于a_min的值都赋值为a_min, 大于a_max的都赋值a_max,在这之间的值不变。 88 print(np.clip(A,5,8)) 89 [[5 5 5 5] 90 [6 7 8 8] 91 [8 8 8 8]] # 判断ndarray阶数,几维向量 99 A.ndim 100 Out[30]: 2 101 A 102 Out[31]: 103 array([[ 2, 3, 4, 5], 104 [ 6, 7, 8, 9], 105 [10, 11, 12, 13]]) 106 A.ndim 107 Out[32]: 2 108 a 109 Out[33]: 110 array([[0.20974732, 0.73822026, 0.82760722, 0.050551 ], 111 [0.77337155, 0.06521922, 0.55524187, 0.59209907]]) 112 a.ndim 113 Out[34]: 2 114 A 115 Out[35]: 116 array([[ 2, 3, 4, 5], 117 [ 6, 7, 8, 9], 118 [10, 11, 12, 13]])
原文地址:https://www.cnblogs.com/brownz/p/9520767.html
时间: 2024-10-10 01:55:01