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实现的防DDoS脚本
Feb 08 Python
python中urllib模块用法实例详解
Nov 19 Python
Python打包可执行文件的方法详解
Sep 19 Python
Python简单的制作图片验证码实例
May 31 Python
Python图片裁剪实例代码(如头像裁剪)
Jun 21 Python
高质量Python代码编写的5个优化技巧
Nov 16 Python
python 快速把超大txt文件转存为csv的实例
Oct 26 Python
Python 闭包,函数分隔作用域,nonlocal声明非局部变量操作示例
Oct 14 Python
kafka-python 获取topic lag值方式
Dec 23 Python
python实现简单区块链结构
Apr 25 Python
Python echarts实现数据可视化实例详解
Mar 03 Python
python实现学生信息管理系统(面向对象)
Jun 05 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
人尽可用的Windows技巧小贴士之下篇
2007/03/22 PHP
Avengerls vs Newbee BO3 第一场2.18
2021/03/10 DOTA
jQuery 性能优化指南 (1)
2009/05/21 Javascript
IE iframe的onload方法分析小结
2010/01/07 Javascript
javascript判断两个IP地址是否在同一个网段的实现思路
2013/12/13 Javascript
利用js制作html table分页示例(js实现分页)
2014/04/25 Javascript
jQuery修改CSS伪元素属性的方法
2014/07/30 Javascript
jQuery源码解读之hasClass()方法分析
2015/02/20 Javascript
jQuery插件支持同一页面被多次调用
2016/02/14 Javascript
jQuery Ajax使用FormData对象上传文件的方法
2016/09/07 Javascript
React 全自动数据表格组件——BodeGrid的实现思路
2019/06/12 Javascript
webpack4.0+vue2.0利用批处理生成前端单页或多页应用的方法
2019/06/28 Javascript
layui的layedit富文本赋值方法
2019/09/18 Javascript
Jquery ajax书写方法代码实例解析
2020/06/12 jQuery
利用Python如何生成随机密码
2016/04/20 Python
Python实现的中国剩余定理算法示例
2017/08/05 Python
python实现栅栏加解密 支持密钥加密
2019/03/20 Python
python解析xml简单示例
2019/06/21 Python
python实现beta分布概率密度函数的方法
2019/07/08 Python
Python 利用高德地图api实现经纬度与地址的批量转换
2019/08/14 Python
Python 分发包中添加额外文件的方法
2019/08/16 Python
浅析python中while循环和for循环
2019/11/19 Python
pandas数据选取:df[] df.loc[] df.iloc[] df.ix[] df.at[] df.iat[]
2020/04/24 Python
python多进程使用函数封装实例
2020/05/02 Python
python,Java,JavaScript实现indexOf
2020/09/09 Python
Python 按比例获取样本数据或执行任务的实现代码
2020/12/03 Python
Django实现简单的分页功能
2021/02/22 Python
CSS3 网页下拉菜单代码解释 中文翻译
2010/02/27 HTML / CSS
西班牙高科技产品购物网站:MejorDeseo
2019/09/08 全球购物
2019年Java面试必问之经典试题
2012/09/12 面试题
大学学习生活感言
2014/01/18 职场文书
师德承诺书2015
2015/04/28 职场文书
《检阅》教学反思
2016/02/22 职场文书
《风筝》教学反思
2016/02/23 职场文书
每日六道java新手入门面试题,通往自由的道路
2021/06/30 Java/Android
Redis分布式锁的7种实现
2022/04/01 Redis