numpy.mean

http://docs.scipy.org/doc/numpy/reference/generated/numpy.mean.html

numpy.mean(aaxis=Nonedtype=Noneout=Nonekeepdims=False)[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. float64 intermediate 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.

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 isNone; 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 original arr.

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.

See also

average
Weighted average

stdvarnanmeannanstdnanvar

Notes

The arithmetic mean is the sum of the elements along the axis divided by the number of elements.

Note that for floating-point input, the mean is computed using the same precision the input has. Depending on the input data, this can cause the results to be inaccurate, especially for float32 (see example below). Specifying a higher-precision accumulator using the dtype keyword can alleviate this issue.

Examples

>>>

>>> a = np.array([[1, 2], [3, 4]])
>>> np.mean(a)
2.5
>>> np.mean(a, axis=0)
array([ 2.,  3.])
>>> np.mean(a, axis=1)
array([ 1.5,  3.5])

In single precision, mean can be inaccurate:

>>>

>>> a = np.zeros((2, 512*512), dtype=np.float32)
>>> a[0, :] = 1.0
>>> a[1, :] = 0.1
>>> np.mean(a)
0.546875

Computing the mean in float64 is more accurate:

>>>

>>> np.mean(a, dtype=np.float64)
0.55000000074505806
时间: 2024-09-04 15:15:16

numpy.mean的相关文章

numpy数据类型dtype转换

这篇文章我们玩玩numpy的数值数据类型转换 导入numpy >>> import numpy as np 一.随便玩玩 生成一个浮点数组 >>> a = np.random.random(4) 看看信息 >>> a array([ 0.0945377 , 0.52199916, 0.62490646, 0.21260126]) >>> a.dtype dtype('float64') >>> a.shape (4,

numpy的random模块

[说明] 翻译自官网的文档. 随机抽样 (numpy.random) 简单的随机数据 rand(d0, d1, ..., dn) 随机值 >>> np.random.rand(3,2) array([[ 0.14022471, 0.96360618], #random [ 0.37601032, 0.25528411], #random [ 0.49313049, 0.94909878]]) #random randn(d0, d1, ..., dn) 返回一个样本,具有标准正态分布.

NumPy基础:数组和失量计算

NumPy : Numerical Python,是高性能科学计算和数据分析的基础包. 部分功能: ndarray:一个具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组: 用于对整组数据进行快速运算的标准数学函数(无需编写循环): 用于读写磁盘数据的工具以及用于操作内存映射文件的工具: 线性代数.随机数生成以及傅里叶变换功能: 用于集成C.C++.Fortran等语言编写的代码工具: 大部分数据分析应用关注的功能: 用于

【学习】基础知识:数组和矢量计量【Numpy】

Numpy是高性能科学计算和数据分析的基础包.功能如下: ndarray 一个具有矢量算法运算和复杂广播能力的快速且节省空间的多维数组 用于对整组数据进行快速运算的标准数学函数(无需编写循环) 用于读写磁盘数据的工具以及用于操作内存映射文件的工具. 线性代数.随机数生成以及傅里叶变换功能 用于集成由C\C++\Fortran等语言编写的代码的工具 numpy本身并没有提供多么高级的数据分析功能,理解numpy数组以及面向数组的计算将有助于更加高效地使用诸如pandas之类的工具 关注的功能集中在

数据分析之Numpy库入门

1.列表与数组 在python的基础语言部分,我们并没有介绍数组类型,但是像C.Java等语言都是有数组类型的,那python中的列表和数组有何区别呢? 一维数据:都表示一组数据的有序结构 区别: 列表:数据类型可以不同,如:[3.1413,'pi',3.1404,[3.1402,2.34],'3.2376'] 数组:数据类型相同 .如[3.14,34.34,3433.3,343.23] 二维数据:二维数据由多个一维数据构成,是一维数据的集合形式!表格是典型的二维数据! 注意:表格的表头,可以是

python科学计算之numpy

1.np.logspace(start,stop,num): 函数表示的意思是;在(start,stop)间生成等比数列num个 eg: import numpy as np print np.logspace(1,4,4) 结果为: [    10.    100.   1000.  10000.] 2. np.fromstring('admin',dtype=np.int8):函数的作用是将字符串装换成对应的ascii值 import numpy as np print np.fromstr

NumPy基础(一)

安装自行解决 ##为什么使用NumPy 文件 vectorSumCompare.py #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'teng' import sys from datetime import datetime import numpy as np def numpysum(n):     a = np.arange(n)**2     b = np.arange(n)**3     c = a+b     r

python 3.4 装matplotlib numpy

为了装个matplotlib包,搞了好久: python3.4,官方没有对应版本的包,只能去下面这个地方下对应的版本: http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载试了下,后发现出错,好像还缺个numpy的包 python 3.4 numpy官方也没有对应版本, 貌似得到下面的网站下载 http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyparsing 最后终于可以了 python 3.4 装matplotlib

《Python数据分析常用手册》一、NumPy和Pandas篇

一.常用链接: 1.Python官网:https://www.python.org/ 2.各种库的whl离线安装包:http://www.lfd.uci.edu/~gohlke/pythonlibs/#scikit-learn 3.数据分析常用库的离线安装包(pip+wheels)(百度云):http://pan.baidu.com/s/1dEMXbfN 密码:bbs2 二.常用库 1.NumPy NumPy是高性能科学计算和数据分析的基础包.部分功能如下: ndarray, 具有矢量算术运算和

numpy中的matrix和array

Preface 在相关聚类算法的实现过程中,用python语言实现,会经常出现array和matrix的混淆,这里做个总结. array数组 numpy中最基本(默认)的类型是array,他的相关操作都是按元素操作的即用作数值计算当中(按元素操作有+,-,,/,*等).相乘举例: from numpy import * >>> a=array([1,2]) >>> a array([1, 2]) >>> b=array([2,3]) >>&