python中mean的用法_python 的numpy库中的mean()函数用法介绍

1. mean() 函数定义:

numpy.mean(a, axis=None, dtype=None, out=None, keepdims=)[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

Array containing numbers whose mean is desired. If a is not an array, a conversion is attempted.

axis : None or int or tuple of ints, optional

Axis or axes along which the means are computed. The default is to compute the mean of the flattened array.

New in version 1.7.0.

If this is a tuple of ints, a mean is performed over multiple axes, instead of a single axis or all the axes as before.

dtype : data-type, optional

Type to use in computing the mean. For integer inputs, the default is float64; for floating point inputs, it is the same as the input dtype.

out : ndarray, optional

Alternate output array in which to place the result. The default is None; if provided, it must have the same shape as the expected output, but the type will be cast if necessary. See doc.ufuncs for details.

keepdims : bool, optional

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

If the default value is passed, then keepdims will not be passed through to the mean method of sub-classes of ndarray, however any non-default value will be. If the sub-classes sum method does not implement keepdims any exceptions will be raised.

Returns:

m : ndarray, see dtype parameter above

If out=None, returns a new array containing the mean values, otherwise a reference to the output array is returned.

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

补充拓展:numpy的np.nanmax和np.max区别(坑)

numpy的np.nanmax和np.array([1,2,3,np.nan]).max()的区别(坑)

numpy中numpy.nanmax的官方文档

原理

在计算dataframe最大值时,最先用到的一定是Series对象的max()方法(),最终结果是4。

s1 = pd.Series([1,2,3,4,np.nan])

s1_max = s1.max()

但是笔者由于数据量巨大,列数较多,于是为了加快计算速度,采用numpy进行最大值的计算,但正如以下代码,最终结果得到的是nan,而非4。发现,采用这种方式计算最大值,nan也会包含进去,并最终结果为nan。

s1 = pd.Series([1,2,3,4,np.nan])

s1_max = s1.values.max()

>>>nan

通过阅读numpy的文档发现,存在np.nanmax的函数,可以将np.nan排除进行最大值的计算,并得到想要的正确结果。

当然不止是max,min 、std、mean 均会存在列中含有np.nan时,s1.values.min /std/mean ()返回nan的情况。

速度区别

速度由快到慢依次:

s1 = pd.Series([1,2,3,4,5,np.nan])

#速度由快至慢

np.nanmax(s1.values) > np.nanmax(s1) > s1.max()

以上这篇python 的numpy库中的mean()函数用法介绍就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

本文标题: python 的numpy库中的mean()函数用法介绍

本文地址: http://www.cppcns.com/jiaoben/python/302056.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python3的NumPy库mean()和average()都是用于计算数组元素的平均值的函数。它们的主要区别在于对于多维数组的处理方式。 1. mean()函数mean()函数用于计算数组元素的平均值。它可以接受一个轴参数,以便在指定轴上进行计算。 例如,对于以下二维数组: ``` import numpy as np arr = np.array([[1, 2], [3, 4]]) ``` 我们可以使用mean()函数来计算所有元素的平均值: ``` print(np.mean(arr)) # 输出:2.5 ``` 我们也可以指定轴参数来计算每行或每列的平均值: ``` print(np.mean(arr, axis=0)) # 输出:[2. 3.] print(np.mean(arr, axis=1)) # 输出:[1.5 3.5] ``` 2. average()函数: average()函数也用于计算数组元素的平均值,但它可以指定每个元素的权重。它也可以接受一个轴参数。 例如,对于以下一维数组: ``` arr = np.array([1, 2, 3, 4]) ``` 我们可以使用average()函数来计算所有元素的平均值: ``` print(np.average(arr)) # 输出:2.5 ``` 我们也可以指定每个元素的权重: ``` weights = np.array([1, 2, 3, 4]) print(np.average(arr, weights=weights)) # 输出:3.0 ``` 如果我们的数组是二维的,我们可以指定轴参数来计算每行或每列的加权平均值: ``` arr = np.array([[1, 2], [3, 4]]) weights = np.array([1, 2]) print(np.average(arr, axis=0, weights=weights)) # 输出:[2.33333333 3.33333333] print(np.average(arr, axis=1, weights=weights)) # 输出:[1.66666667 3.66666667] ``` 因此,总的来说,mean()函数用于计算简单的平均值,而average()函数则可以指定每个元素的权重来计算加权平均值。在处理多维数组时,mean()函数计算每行或每列的平均值,而average()函数则可以计算加权平均值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值