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结合opencv实现人脸检测与跟踪
Jun 08 Python
浅谈python中的占位符
Nov 09 Python
Python使用matplotlib绘制正弦和余弦曲线的方法示例
Jan 06 Python
Python机器学习库scikit-learn安装与基本使用教程
Jun 25 Python
python 通过 socket 发送文件的实例代码
Aug 14 Python
Python Pandas批量读取csv文件到dataframe的方法
Oct 08 Python
Python 给屏幕打印信息加上颜色的实现方法
Apr 24 Python
Python实现的插入排序,冒泡排序,快速排序,选择排序算法示例
May 04 Python
简单的Python人脸识别系统
Jul 14 Python
如何使用PyCharm引入需要使用的包的方法
Sep 22 Python
Python colormap库的安装和使用详情
Oct 06 Python
python获取天气接口给指定微信好友发天气预报
Dec 28 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的$_FILES的临时储存文件与回收机制实测过程
2013/07/12 PHP
php获取网站根目录物理路径的几种方法(推荐)
2017/03/04 PHP
Laravel 5使用Laravel Excel实现Excel/CSV文件导入导出的功能详解
2017/10/11 PHP
laravel框架使用阿里云短信发送消息操作示例
2020/02/15 PHP
javascript 全角转换实现代码
2009/07/17 Javascript
extjs 的权限问题 要求控制的对象是 菜单,按钮,URL
2010/03/09 Javascript
jQuery(1.6.3) 中css方法对浮动的实现缺陷分析
2011/09/09 Javascript
Javascript 多浏览器兼容总结(实战经验)
2013/10/30 Javascript
JavaScript继承基础讲解(原型链、借用构造函数、混合模式、原型式继承、寄生式继承、寄生组合式继承)
2014/08/16 Javascript
jQuery类选择器用法实例
2014/12/23 Javascript
jquery实现弹出层登录和全屏层注册特效
2015/08/28 Javascript
js中遍历对象的属性和值的方法
2016/07/27 Javascript
微信小程序 template模板详解及实例
2017/02/21 Javascript
flag和jq on 的绑定多个对象和方法(必看)
2017/02/27 Javascript
js实现点击切换checkbox背景图片的简单实例
2017/05/08 Javascript
JavaScript箭头函数_动力节点Java学院整理
2017/06/28 Javascript
JavaScript操作文件_动力节点Java学院整理
2017/06/30 Javascript
AngularJs+Bootstrap实现漂亮的计算器
2017/08/10 Javascript
详解如何实现Element树形控件Tree在懒加载模式下的动态更新
2019/04/25 Javascript
Vue中多元素过渡特效的解决方案
2020/02/05 Javascript
Python中删除文件的程序代码
2011/03/13 Python
Python读取mat文件,并转为csv文件的实例
2018/07/04 Python
Python Selenium 之关闭窗口close与quit的方法
2019/02/13 Python
Python API 自动化实战详解(纯代码)
2019/06/11 Python
Django ORM 聚合查询和分组查询实现详解
2019/08/09 Python
python对象转字典的两种实现方式示例
2019/11/07 Python
Jupyter Notebook 文件默认目录的查看以及更改步骤
2020/04/14 Python
CSS3 translate导致字体模糊的实例代码
2019/08/30 HTML / CSS
HTML5实现应用程序缓存(Application Cache)
2020/06/16 HTML / CSS
Viking Direct荷兰:购买办公用品
2019/06/20 全球购物
党员志愿者服务倡议书
2015/04/29 职场文书
行政上诉状范文
2015/05/23 职场文书
七一表彰大会简报
2015/07/20 职场文书
一波干货,会议主持词开场白范文
2019/05/06 职场文书
python函数指定默认值的实例讲解
2021/03/29 Python
关于springboot配置druid数据源不生效问题(踩坑记)
2021/09/25 Java/Android