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


Posted in Python onMarch 03, 2020

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 相关文章推荐
基于Python的身份证号码自动生成程序
Aug 15 Python
python求列表交集的方法汇总
Nov 10 Python
Python通过select实现异步IO的方法
Jun 04 Python
python daemon守护进程实现
Aug 27 Python
python使用logging模块发送邮件代码示例
Jan 18 Python
Python创建普通菜单示例【基于win32ui模块】
May 09 Python
python 3调用百度OCR API实现剪贴板文字识别
Sep 04 Python
Python3实现的简单三级菜单功能示例
Mar 12 Python
python实现维吉尼亚加密法
Mar 20 Python
python使用flask与js进行前后台交互的例子
Jul 19 Python
Python读取ini配置文件传参的简单示例
Jan 05 Python
Python数据可视化之绘制柱状图和条形图
May 25 Python
Python统计学一数据的概括性度量详解
Mar 03 #Python
python多维数组分位数的求取方式
Mar 03 #Python
浅谈pandas.cut与pandas.qcut的使用方法及区别
Mar 03 #Python
python Plotly绘图工具的简单使用
Mar 03 #Python
python 函数嵌套及多函数共同运行知识点讲解
Mar 03 #Python
python实现扫雷游戏
Mar 03 #Python
python实现从ftp服务器下载文件
Mar 03 #Python
You might like
PHP实现上传文件并存进数据库的方法
2015/07/16 PHP
PHP代码维护,重构变困难的4种原因分析
2016/01/25 PHP
浅析Yii2 GridView实现下拉搜索教程
2016/04/22 PHP
一个简单安全的PHP验证码类、PHP验证码
2016/09/24 PHP
浅谈Laravel核心解读之Console内核
2018/12/02 PHP
JavaScript CSS 修改学习第四章 透明度设置
2010/02/19 Javascript
火狐下input焦点无法重复获取问题的解决方法
2014/06/16 Javascript
js实现鼠标经过表格行变色的方法
2015/05/12 Javascript
浅谈jQuery animate easing的具体使用方法(推荐)
2016/06/17 Javascript
浅谈bootstrap使用中的一些问题以及解决过程
2016/10/18 Javascript
JS中用try catch对代码运行的性能影响分析
2016/12/26 Javascript
Node.JS中事件轮询(Event Loop)的解析
2017/02/25 Javascript
整理关于Bootstrap警示框的慕课笔记
2017/03/29 Javascript
vue使用video.js进行视频播放功能
2019/07/18 Javascript
微信小程序激励式视频广告组件使用详解
2019/12/06 Javascript
微信小程序实现注册登录功能(表单校验、错误提示)
2019/12/10 Javascript
tensorflow入门之训练简单的神经网络方法
2018/02/26 Python
python保存网页图片到本地的方法
2018/07/24 Python
python实现图片转字符小工具
2019/04/30 Python
python中的decimal类型转换实例详解
2019/06/26 Python
如何用python处理excel表格
2020/06/09 Python
CSS+jQuery实现的在线答题功能
2015/04/25 HTML / CSS
巴西服装和鞋子购物网站:Marisa
2018/10/25 全球购物
预订旅游活动、景点和旅游:GetYourGuide
2019/09/29 全球购物
俄罗斯EPL钻石珠宝店:ЭПЛ
2019/10/22 全球购物
应用数学自荐书范文
2013/11/24 职场文书
应届优秀本科大学毕业生自我鉴定
2014/01/21 职场文书
厕所文明标语
2014/06/11 职场文书
室内趣味活动方案
2014/08/24 职场文书
2015年妇女工作总结
2015/05/14 职场文书
2015国庆66周年宣传语
2015/07/14 职场文书
2016年基层党组织创先争优承诺书
2016/03/25 职场文书
MySQL系列之七 MySQL存储引擎
2021/07/02 MySQL
nginx作grpc的反向代理踩坑总结
2021/07/07 Servers
CSS 一行代码实现头像与国旗的融合
2021/10/24 HTML / CSS
Win11运行育碧游戏总是崩溃怎么办 win11玩育碧游戏出现性能崩溃的解决办法
2022/04/06 数码科技