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采集腾讯新闻实例
Jul 10 Python
Python使用cx_Oracle模块将oracle中数据导出到csv文件的方法
May 16 Python
Go语言基于Socket编写服务器端与客户端通信的实例
Feb 19 Python
Python实现统计文本文件字数的方法
May 05 Python
python对配置文件.ini进行增删改查操作的方法示例
Jul 28 Python
Python实现加载及解析properties配置文件的方法
Mar 29 Python
windows下python安装pip图文教程
May 25 Python
python 在指定范围内随机生成不重复的n个数实例
Jan 28 Python
python 多进程共享全局变量之Manager()详解
Aug 15 Python
Python基本语法之运算符功能与用法详解
Oct 22 Python
numpy ndarray 按条件筛选数组,关联筛选的例子
Nov 26 Python
基于python检查SSL证书到期情况代码实例
Apr 04 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为表单获取的URL 地址预设 http 字符串函数代码
2010/05/26 PHP
ThinkPHP自动转义存储富文本编辑器内容导致读取出错的解决方法
2014/08/08 PHP
php隐藏实际地址的文件下载方法
2015/04/18 PHP
PHP中两个float(浮点数)比较实例分析
2015/09/27 PHP
php分页查询的简单实现代码
2017/03/14 PHP
php写app接口并返回json数据的实例(分享)
2017/05/20 PHP
PHP Class SoapClient not found解决方法
2018/01/20 PHP
PHP call_user_func和call_user_func_array函数的简单理解与应用分析
2019/11/25 PHP
JS和jquery获取各种屏幕的宽度和高度的代码
2013/08/02 Javascript
高性能JavaScript 重排与重绘(2)
2015/08/11 Javascript
基于jQuery实现鼠标点击导航菜单水波动画效果附源码下载
2016/01/06 Javascript
js前端日历控件(悬浮、拖拽、自由变形)
2017/03/02 Javascript
详解React Native网络请求fetch简单封装
2017/08/10 Javascript
浅谈Express异步进化史
2017/09/09 Javascript
轻量级JS Cookie插件js-cookie的使用方法
2018/03/22 Javascript
vue-cli项目根据线上环境分别打出测试包和生产包
2018/05/23 Javascript
Vue调用后端java接口的实例代码
2019/10/28 Javascript
详解利用eventemitter2实现Vue组件通信
2019/11/04 Javascript
Js参数RSA加密传输之jsencrypt.js的使用
2020/02/07 Javascript
JS造成内存泄漏的几种情况实例分析
2020/03/02 Javascript
javascript实现点击按钮切换轮播图功能
2020/09/23 Javascript
[04:09]2018年度DOTA2社区贡献奖-完美盛典
2018/12/16 DOTA
[47:31]完美世界DOTA2联赛PWL S3 INK ICE vs DLG 第一场 12.12
2020/12/16 DOTA
python每次处理固定个数的字符的方法总结
2013/01/29 Python
Python实现随机生成手机号及正则验证手机号的方法
2018/04/25 Python
详解Django-auth-ldap 配置方法
2018/12/10 Python
python实现两个一维列表合并成一个二维列表
2019/12/02 Python
将 Ubuntu 16 和 18 上的 python 升级到最新 python3.8 的方法教程
2020/03/11 Python
django admin后管定制-显示字段的实例
2020/03/11 Python
使用Filters滤镜弥补CSS3的跨浏览器问题以及兼容低版本IE
2013/01/23 HTML / CSS
英文版网络工程师求职信
2013/10/28 职场文书
区域销售经理岗位职责
2013/12/10 职场文书
环保建议书300字
2014/05/14 职场文书
党支部评议意见
2015/06/02 职场文书
解除合同协议书范本
2016/03/21 职场文书
PostgreSQL之连接失败的问题及解决
2023/05/08 PostgreSQL