python mean()

对于矩阵:

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.]

猜你喜欢

转载自www.cnblogs.com/zhhy236400/p/9932551.html