如何获得列表内的每个列表的均值,从而避免某些值? [英] How to get mean of each list inside the list avoiding certain value?
问题描述
如何在列表内分别计算每个列表的平均值,避免使用特殊值(-999)?
How to calculate mean of values of each list separately inside the list avoiding a special value (-999)?
A = [[4,5,7,8,-999],[3,8,5,7,-999]]
M = [sum(x)/len(x) for x in [y for y in A if y!= -999]
print (M)
有什么主意吗?
为了获得最佳速度:有人可以更正以下代码吗? @ alexanderlukanin13
For best speed: can someone correct the following code? @alexanderlukanin13
M = [np.mean(L [L!=-999])for L in A]
推荐答案
您提到的numpy
:
>>> import numpy as np
>>>
>>> A = [[4,5,7,8,-999],[3,8,5,7,-999]]
>>> M = [np.mean([x for x in L if x > -999]) for L in A]
>>> print M
[6.0, 5.75]
编辑
正如您提到的速度是一项重要要求,您可以执行以下操作:
As you mentioned speed as an important requirement, you can do this:
>>> B = np.array(A)
>>> np.average(B, axis=1, weights=B!=-999)
array([ 6. , 5.75])
所有事情都在numpy(即C)空间中发生,这应该会使它变得非常快.
Everything happens in numpy (i.e. C) space, which should get it pretty fast.
解释一下正在发生的事情:
To explain a bit what is happening:
np.mean(A, axis=1)
和等效的np.average(A, axis=1)
计算数组各列的平均值,即您想要的每一行的均值向量. average
允许使用权重:我们使用B!=-999
是一个布尔数组,当用作权重时将其评估为1
s和0
s,即忽略评估为False
的值.
np.mean(A, axis=1)
and the equivalent np.average(A, axis=1)
compute the average over the columns of your array, i.e. the vector of means of each row, which is what you want. average
allows the use of weights: we use B!=-999
, which is a boolean array, evaluated as 1
s and 0
s when used as weights, i.e. ignoring the values evaluated as False
.
这篇关于如何获得列表内的每个列表的均值,从而避免某些值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!