python 的numpy库中的mean()函数用法
1. mean() 函数定义:
numpy. mean ( a, axis=None, dtype=None, out=None, keepdims=<class numpy._globals._NoValue at 0x40b6a26c> ) [source]Compute the arithmetic mean along the specified axis.
Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis. float64intermediate and return values are used for integer inputs.
Parameters: | a : array_like
axis : None or int or tuple of ints, optional
dtype : data-type, optional
out : ndarray, optional
keepdims : bool, optional
|
---|---|
Returns: | m : ndarray, see dtype parameter above
|
经常操作的参数为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.]])
更多推荐
所有评论(0)