标签归档:mean

Python NumPy中的np.mean()vs np.average()吗?

问题:Python NumPy中的np.mean()vs np.average()吗?

我注意到

In [30]: np.mean([1, 2, 3])
Out[30]: 2.0

In [31]: np.average([1, 2, 3])
Out[31]: 2.0

但是,应该存在一些差异,因为它们毕竟是两个不同的功能。

它们之间有什么区别?

I notice that

In [30]: np.mean([1, 2, 3])
Out[30]: 2.0

In [31]: np.average([1, 2, 3])
Out[31]: 2.0

However, there should be some differences, since after all they are two different functions.

What are the differences between them?


回答 0

np.average采用可选的权重参数。如果未提供,则等效。看一下源代码:MeanAverage

np.mean:

try:
    mean = a.mean
except AttributeError:
    return _wrapit(a, 'mean', axis, dtype, out)
return mean(axis, dtype, out)

np.average:

...
if weights is None :
    avg = a.mean(axis)
    scl = avg.dtype.type(a.size/avg.size)
else:
    #code that does weighted mean here

if returned: #returned is another optional argument
    scl = np.multiply(avg, 0) + scl
    return avg, scl
else:
    return avg
...

np.average takes an optional weight parameter. If it is not supplied they are equivalent. Take a look at the source code: Mean, Average

np.mean:

try:
    mean = a.mean
except AttributeError:
    return _wrapit(a, 'mean', axis, dtype, out)
return mean(axis, dtype, out)

np.average:

...
if weights is None :
    avg = a.mean(axis)
    scl = avg.dtype.type(a.size/avg.size)
else:
    #code that does weighted mean here

if returned: #returned is another optional argument
    scl = np.multiply(avg, 0) + scl
    return avg, scl
else:
    return avg
...

回答 1

np.mean 总是计算算术平均值,并具有一些用于输入和输出的其他选项(例如,使用什么数据类型,将结果放置在何处)。

np.average如果weights提供了参数,则可以计算加权平均值。

np.mean always computes an arithmetic mean, and has some additional options for input and output (e.g. what datatypes to use, where to place the result).

np.average can compute a weighted average if the weights parameter is supplied.


回答 2

在某些版本的numpy中,您必须意识到另一个重要的区别:

average 不考虑掩码,因此请计算整个数据集的平均值。

mean 考虑到掩码,因此仅对未掩码的值计算平均值。

g = [1,2,3,55,66,77]
f = np.ma.masked_greater(g,5)

np.average(f)
Out: 34.0

np.mean(f)
Out: 2.0

In some version of numpy there is another imporant difference that you must be aware:

average do not take in account masks, so compute the average over the whole set of data.

mean takes in account masks, so compute the mean only over unmasked values.

g = [1,2,3,55,66,77]
f = np.ma.masked_greater(g,5)

np.average(f)
Out: 34.0

np.mean(f)
Out: 2.0

回答 3

在您的调用中,两个函数是相同的。

average 可以计算加权平均值。

Doc链接:meanaverage

In your invocation, the two functions are the same.

average can compute a weighted average though.

Doc links: mean and average


回答 4

除了已经指出的差异之外,还有另一个非常重要的差异,我刚刚发现了很难的方法:与不同np.meannp.average不允许使用dtype关键字,这在某些情况下对于获得正确的结果至关重要。我有一个非常大的单精度数组,可以从h5文件访问它。如果我沿轴0和1取平均值,除非指定dtype='float64'

>T.shape
(4096, 4096, 720)
>T.dtype
dtype('<f4')

m1 = np.average(T, axis=(0,1))                #  garbage
m2 = np.mean(T, axis=(0,1))                   #  the same garbage
m3 = np.mean(T, axis=(0,1), dtype='float64')  # correct results

不幸的是,除非您知道要查找的内容,否则不一定能说出结果是错误的。np.average由于这个原因,我将不再使用,但将始终np.mean(.., dtype='float64')在任何大型阵列上使用。如果我想要一个加权平均数,我将使用权重向量和目标数组的乘积,然后再加上np.sumnp.mean,适当地(也具有适当的精度),对它进行显式计算。

In addition to the differences already noted, there’s another extremely important difference that I just now discovered the hard way: unlike np.mean, np.average doesn’t allow the dtype keyword, which is essential for getting correct results in some cases. I have a very large single-precision array that is accessed from an h5 file. If I take the mean along axes 0 and 1, I get wildly incorrect results unless I specify dtype='float64':

>T.shape
(4096, 4096, 720)
>T.dtype
dtype('<f4')

m1 = np.average(T, axis=(0,1))                #  garbage
m2 = np.mean(T, axis=(0,1))                   #  the same garbage
m3 = np.mean(T, axis=(0,1), dtype='float64')  # correct results

Unfortunately, unless you know what to look for, you can’t necessarily tell your results are wrong. I will never use np.average again for this reason but will always use np.mean(.., dtype='float64') on any large array. If I want a weighted average, I’ll compute it explicitly using the product of the weight vector and the target array and then either np.sum or np.mean, as appropriate (with appropriate precision as well).


在Python中计算算术平均值(一种平均值)

问题:在Python中计算算术平均值(一种平均值)

Python中是否有内置或标准库方法来计算数字列表的算术平均值(一种平均值)?

Is there a built-in or standard library method in Python to calculate the arithmetic mean (one type of average) of a list of numbers?


回答 0

我不知道标准库中的任何内容。但是,您可以使用类似以下内容的方法:

def mean(numbers):
    return float(sum(numbers)) / max(len(numbers), 1)

>>> mean([1,2,3,4])
2.5
>>> mean([])
0.0

在numpy中,有numpy.mean()

I am not aware of anything in the standard library. However, you could use something like:

def mean(numbers):
    return float(sum(numbers)) / max(len(numbers), 1)

>>> mean([1,2,3,4])
2.5
>>> mean([])
0.0

In numpy, there’s numpy.mean().


回答 1

NumPy的a numpy.mean是算术平均值。用法很简单:

>>> import numpy
>>> a = [1, 2, 4]
>>> numpy.mean(a)
2.3333333333333335

NumPy has a numpy.mean which is an arithmetic mean. Usage is as simple as this:

>>> import numpy
>>> a = [1, 2, 4]
>>> numpy.mean(a)
2.3333333333333335

回答 2

用途statistics.mean

import statistics
print(statistics.mean([1,2,4])) # 2.3333333333333335

从Python 3.4开始可用。对于3.1-3.3用户,该模块的旧版本可在PyPI上以的名称获得stats。只需更改statistics为即可stats

Use statistics.mean:

import statistics
print(statistics.mean([1,2,4])) # 2.3333333333333335

It’s available since Python 3.4. For 3.1-3.3 users, an old version of the module is available on PyPI under the name stats. Just change statistics to stats.


回答 3

您甚至不需要麻木或肮脏的…

>>> a = [1, 2, 3, 4, 5, 6]
>>> print(sum(a) / len(a))
3

You don’t even need numpy or scipy…

>>> a = [1, 2, 3, 4, 5, 6]
>>> print(sum(a) / len(a))
3

回答 4

使用scipy:

import scipy;
a=[1,2,4];
print(scipy.mean(a));

Use scipy:

import scipy;
a=[1,2,4];
print(scipy.mean(a));

回答 5

除了强制浮动之外,您还可以执行以下操作

def mean(nums):
    return sum(nums, 0.0) / len(nums)

或使用lambda

mean = lambda nums: sum(nums, 0.0) / len(nums)

更新日期:2019-12-15

Python 3.8 在统计模块中添加了功能fmean。哪个更快,并且总是返回float。

将数据转换为浮点并计算算术平均值。

它的运行速度比mean()函数快,并且始终返回浮点数。数据可以是序列或可迭代的。如果输入数据集为空,则引发StatisticsError。

fmean([3.5,4.0,5.25])

4.25

3.8版的新功能。

Instead of casting to float you can do following

def mean(nums):
    return sum(nums, 0.0) / len(nums)

or using lambda

mean = lambda nums: sum(nums, 0.0) / len(nums)

UPDATES: 2019-12-15

Python 3.8 added function fmean to statistics module. Which is faster and always returns float.

Convert data to floats and compute the arithmetic mean.

This runs faster than the mean() function and it always returns a float. The data may be a sequence or iterable. If the input dataset is empty, raises a StatisticsError.

fmean([3.5, 4.0, 5.25])

4.25

New in version 3.8.


回答 6

from statistics import mean
avarage=mean(your_list)

例如

from statistics import mean

my_list=[5,2,3,2]
avarage=mean(my_list)
print(avarage)

结果是

3.0
from statistics import mean
avarage=mean(your_list)

for example

from statistics import mean

my_list=[5,2,3,2]
avarage=mean(my_list)
print(avarage)

and result is

3.0

回答 7

def avg(l):
    """uses floating-point division."""
    return sum(l) / float(len(l))

例子:

l1 = [3,5,14,2,5,36,4,3]
l2 = [0,0,0]

print(avg(l1)) # 9.0
print(avg(l2)) # 0.0
def avg(l):
    """uses floating-point division."""
    return sum(l) / float(len(l))

Examples:

l1 = [3,5,14,2,5,36,4,3]
l2 = [0,0,0]

print(avg(l1)) # 9.0
print(avg(l2)) # 0.0

回答 8

def list_mean(nums):
    sumof = 0
    num_of = len(nums)
    mean = 0
    for i in nums:
        sumof += i
    mean = sumof / num_of
    return float(mean)
def list_mean(nums):
    sumof = 0
    num_of = len(nums)
    mean = 0
    for i in nums:
        sumof += i
    mean = sumof / num_of
    return float(mean)

回答 9

我一直认为应该avg从Builtins / stdlib中省略它,因为它很简单

sum(L)/len(L) # L is some list

并且任何告诫将在调用者代码中解决以供本地使用

注意事项:

  1. 非浮点结果:在python2中,9/4为2。解析,使用float(sum(L))/len(L)from __future__ import division

  2. 除以零:列表可能为空。解决:

    if not L:
        raise WhateverYouWantError("foo")
    avg = float(sum(L))/len(L)

I always supposed avg is omitted from the builtins/stdlib because it is as simple as

sum(L)/len(L) # L is some list

and any caveats would be addressed in caller code for local usage already.

Notable caveats:

  1. non-float result: in python2, 9/4 is 2. to resolve, use float(sum(L))/len(L) or from __future__ import division

  2. division by zero: the list may be empty. to resolve:

    if not L:
        raise WhateverYouWantError("foo")
    avg = float(sum(L))/len(L)
    

回答 10

对您问题的正确答案是使用statistics.mean。但是为了好玩,这是一个不使用该len()功能的Mean的版本,因此statistics.mean可以在不支持该功能的生成器上使用它(如)len()

from functools import reduce
from operator import truediv
def ave(seq):
    return truediv(*reduce(lambda a, b: (a[0] + b[1], b[0]), 
                           enumerate(seq, start=1), 
                           (0, 0)))

The proper answer to your question is to use statistics.mean. But for fun, here is a version of mean that does not use the len() function, so it (like statistics.mean) can be used on generators, which do not support len():

from functools import reduce
from operator import truediv
def ave(seq):
    return truediv(*reduce(lambda a, b: (a[0] + b[1], b[0]), 
                           enumerate(seq, start=1), 
                           (0, 0)))

回答 11

其他人已经发布了很好的答案,但有些人可能仍在寻找找到Mean(avg)的经典方法,因此,我在这里发布了此信息(在Python 3.6中测试过的代码):

def meanmanual(listt):

mean = 0
lsum = 0
lenoflist = len(listt)

for i in listt:
    lsum += i

mean = lsum / lenoflist
return float(mean)

a = [1, 2, 3, 4, 5, 6]
meanmanual(a)

Answer: 3.5

Others already posted very good answers, but some people might still be looking for a classic way to find Mean(avg), so here I post this (code tested in Python 3.6):

def meanmanual(listt):

mean = 0
lsum = 0
lenoflist = len(listt)

for i in listt:
    lsum += i

mean = lsum / lenoflist
return float(mean)

a = [1, 2, 3, 4, 5, 6]
meanmanual(a)

Answer: 3.5