python中 mean()函数功能:求取均值

用法:mean(matrix,axis=0)  其中 matrix为一个矩阵,axis为参数

以m * n矩阵举例:

axis 不设置值,对 m*n 个数求均值,返回一个实数

axis = 0:压缩行,对各列求均值,返回 1* n 矩阵

axis =1 :压缩列,对各行求均值,返回 m *1 矩阵

举例:

>>>  import numpy as np

>>> num1 = np.array([[1,2,3],[2,3,4],[3,4,5],[4,5,6]])
>>> now2 = np.mat(num1)
>>> now2
matrix([[1, 2, 3],
        [2, 3, 4],
        [3, 4, 5],
        [4, 5, 6]])


>>> np.mean(now2) # 对所有元素求均值
3.5


>>> np.mean(now2,0) # 压缩行,对各列求均值
matrix([[ 2.5,  3.5,  4.5]])


>>> np.mean(now2,1) # 压缩列,对各行求均值
matrix([[ 2.],
        [ 3.],
        [ 4.],
        [ 5.]])

猜你喜欢

转载自blog.csdn.net/jiangsujiangjiang/article/details/83660368