1. mean() 函数定义:

numpy.mean(a, axis=None, dtype=None, out=None, keepdims=)[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()的区别(坑)

原理

在计算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中mean的用法_python 的numpy库中的mean()函数用法介绍相关推荐

  1. python average函数怎么用_python 的numpy库中的mean()函数用法介绍

    1. mean() 函数定义: numpy.mean(a, axis=None, dtype=None, out=None, keepdims=)[source] Compute the arithm ...

  2. python定义一个空数组_python – 在numpy数组中设置空值

    如何基于条件使numpy数组中的某些值为空? 我不明白为什么我最终得到0而不是null或空值不满足条件- b是一个用0和1值填充的numpy数组,c是另一个完全填充的numpy数组.所有阵列都是71x ...

  3. python的自带数据集_Python的Sklearn库中的数据集

    一.Sklearn介绍 scikit-learn是Python语言开发的机器学习库,一般简称为sklearn,目前算是通用机器学习算法库中实现得比较完善的库了.其完善之处不仅在于实现的算法多,还包括大 ...

  4. python numpy库中省略号...的一些用法

    在学习<Designing Machine Learning Systems with Python>(<机器学习系统设计Python语言实现>)一书的第五章梯度下降一节代码中 ...

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

    这篇文章主要介绍了python 的numpy库中的mean()函数用法介绍,具有很好对参考价值,希望对大家有所帮助.一起跟随小编过来看看吧 mean() 函数定义: 2 mean()函数功能: 求取均 ...

  6. Python:Numpy库中的invert()函数的用法

    Numpy库中的invert()函数的用法 官方解释: Compute bit-wise inversion, or bit-wise NOT, element-wise. Computes the ...

  7. Python:numpy库中的一些函数简介、使用方法之详细攻略

    Python:numpy库中的一些函数简介.使用方法之详细攻略 目录 numpy库中的一些函数简介.使用方法 1.np.concatenate() 1.1.函数案例 1.2.函数用法 numpy库中的 ...

  8. [转载] Python里面numpy库中zeros()的一些问题

    参考链接: Python中的numpy.zeros Python里面numpy库中zeros函数的一些问题 定义 本文记录了在使用numpy库中的zeros函数时遇到的一些问题 定义 用法:zeros ...

  9. Python的numpy库中rand(),randn(),randint(),random_integers()等random系函数的使用

    在使用Python进行数据处理时,往往需要用到大量的随机数据,那如何构造这么多数据呢?Python的第三方库numpy库中提供了random函数来实现这个功能. 本文将根据官方文档以及其他博友的博客一 ...

最新文章

  1. python怎么打包压缩文件_Python打包文件夹的方法小结(zip,tar,tar.gz等)
  2. smarty学习——编程知识
  3. 数学——Euler方法求解微分方程详解(python3)
  4. CISP-PTE注册信息安全专业人员渗透测试工程师知识体系大纲
  5. Android开发5——文件读写
  6. 无法使用tomcat6.exe启动服务
  7. linkedin爬虫_机器学习的学生和从业者的常见问题在LinkedIn上提问
  8. docker 镜像选择_为什么选择Docker?
  9. 脚本在流程中的性能影响
  10. 什么是光纤放大器?光放大器的原理是什么?
  11. leetcode题解50-Pow(x,n)
  12. 基于JAVA+SpringBoot+Mybatis+MYSQL的贷款审批系统
  13. [Matlab科学绘图] Matlab画图常用函数和命令
  14. win10应用程序无法启动因为应用程序的并行配置不正确解决思路
  15. win7(win10)更改“文件类型显示图标“的终极修改方法
  16. Tensorflow入门之 Win10 运行 linux 子系统
  17. Eclipse官网下载
  18. 关于线宽与PCB过孔铺铜的一点经验
  19. 实际采用 FleaPHP 的网站
  20. Axon Framework架构概述

热门文章

  1. C/C++ volatile
  2. 反编译C#的dll文件并修改,再重新生成dll
  3. String 创建对象问题
  4. webpack教程——css的加载
  5. laravel 知识点总结
  6. 【Python】【有趣的模块】【systimeos】
  7. BZOJ 1801: [Ahoi2009]chess 中国象棋( dp )
  8. 多线程与多进程(转载)
  9. linux比较两个目录的差异
  10. 在线文字图标logo文章封面图生成工具