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

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函数

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com

评论“python 的numpy库中的mean()函数用法介绍”

暂无“python 的numpy库中的mean()函数用法介绍”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。