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编程中的命名空间与作用域
Oct 16 Python
pandas ix &amp;iloc &amp;loc的区别
Jan 10 Python
python ipset管理 增删白名单的方法
Jan 14 Python
使用python绘制二元函数图像的实例
Feb 12 Python
零基础使用Python读写处理Excel表格的方法
May 02 Python
使用Python OpenCV为CNN增加图像样本的实现
Jun 10 Python
python用match()函数爬数据方法详解
Jul 23 Python
如何运行带参数的python脚本
Nov 15 Python
用Python绘制漫步图实例讲解
Feb 26 Python
Python实现子类调用父类的初始化实例
Mar 12 Python
opencv 图像滤波(均值,方框,高斯,中值)
Jul 08 Python
python怎么删除缓存文件
Jul 19 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
PHP5与MySQL数据库操作常用代码 收集
2010/03/21 PHP
fleaphp crud操作之find函数的使用方法
2011/04/23 PHP
php实现encode64编码类实例
2015/03/24 PHP
json-lib出现There is a cycle in the hierarchy解决办法
2010/02/24 Javascript
Javascript的getYear、getFullYear、getUTCFullYear异同分享
2011/11/30 Javascript
DOM2非标准但却支持很好的几个属性小结
2012/01/21 Javascript
Jquery 实现grid绑定模板
2015/01/28 Javascript
解决vue router使用 history 模式刷新后404问题
2017/07/19 Javascript
javascript cookie的基本操作(添加和删除)
2017/07/24 Javascript
js实现1,2,3,5数字按照概率生成
2017/09/12 Javascript
vue element 关闭当前tab 跳转到上一路由操作
2020/07/22 Javascript
jQuery编写QQ简易聊天框
2020/08/27 jQuery
有关wxpython pyqt内存占用问题分析
2014/06/09 Python
python传递参数方式小结
2015/04/17 Python
python 多个参数不为空校验方法
2019/02/14 Python
Python 常用模块 re 使用方法详解
2019/06/06 Python
Python使用百度api做人脸对比的方法
2019/08/28 Python
Python基于gevent实现文件字符串查找器
2020/08/11 Python
python如何将图片转换素描画
2020/09/08 Python
Python中使用Selenium环境安装的方法步骤
2021/02/22 Python
.NET常见笔试题集
2012/12/01 面试题
中西医结合临床医学专业大学生自荐信
2013/09/28 职场文书
出纳的岗位职责
2013/11/09 职场文书
英文自荐信格式
2013/11/28 职场文书
2014年十一国庆向国旗敬礼寄语
2014/04/11 职场文书
论群众路线学习笔记
2014/11/06 职场文书
六一领导慰问欢迎词
2015/01/26 职场文书
给老婆的检讨书
2015/01/27 职场文书
2015年营业员工作总结
2015/04/23 职场文书
大学生暑假实习总结
2015/07/13 职场文书
小学体育组工作总结
2015/08/13 职场文书
2019暑期安全倡议书!
2019/06/27 职场文书
教您怎么制定西餐厅运营方案 ?
2019/07/05 职场文书
Mysql效率优化定位较低sql的两种方式
2021/05/26 MySQL
Win11 PC上的Outlook搜索错误怎么办?
2022/07/15 数码科技
vue实现简易音乐播放器
2022/08/14 Vue.js