numpy库补充 mean函数应用

mean()函数功能:求取均值
经常操作的参数为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.]])

猜你喜欢

转载自www.cnblogs.com/cxhzy/p/10773413.html