對於矩陣:
from numpy import * a=mat([[2,2,2],[4,4,4]]) print(a.mean()) #所有元素的平均值 print(mean(a)) #所有元素的平均值 print(mean(a,0)) #壓縮行,對各列求平均值 print(mean(a,1)) #壓縮列,對各行求平均值
輸出:<class 'numpy.matrixlib.defmatrix.matrix'>
3.0 3.0 [[3. 3. 3.]] [[2.] [4.]]
對於數組:
from numpy import * a=array([[2,2,2],[4,4,4]]) print(a.mean()) #所有元素的平均值 print(mean(a)) #所有元素的平均值 print(mean(a,0)) #壓縮行,對各列求平均值 print(mean(a,1)) #壓縮列,對各行求平均值
輸出:<class 'numpy.ndarray'>
3.0 3.0 [3. 3. 3.] [2. 4.]