如何计算列表中省略特殊值(-999)的值的平均值?
import numpy as np
A = [4,5,7,8,-999]
M = np.mean(A)
任何想法 ???
>>> import numpy as np
>>> a = np.array([1,2,3,4,5])
>>> np.mean(a)
3.0
>>> np.mean(a[a<=3])
2.0
>>> np.mean(a[a!=4])
2.75
对于 OP 案例:
np.mean(A[A!=-999])
表现
让我们用 Python 生成器测试三个片段:plainnp.mean
和masked_array
“naive”解决方案。数组有 1000000 个值。
from timeit import timeit
setup = 'import numpy as np; a=np.arange(0, 1000000)'
snippets = [
'assert np.mean(a[a!=999999]) == 499999.0',
'm=np.ma.masked_array(a,a==999999); assert np.ma.mean(m) == 499999.0',
'assert sum(x for x in a if x != 999999)/999999 == 499999'
]
timings = [timeit(x, setup=setup, number=10) for x in snippets]
print('\n'.join(str(x) for x in timings))
结果:
0.0840559005737
0.0890350341797
10.4104599953
简单np.mean
且masked_array
时间很接近,而“幼稚”的解决方案要慢 100 倍以上。
在 numpy 中,您可以使用掩码数组的意思:
import numpy as np
A = np.array([4,5,7,8,-999])
mask_A = A == -999
ma_A = np.ma.masked_array(A, mask_A)
print np.ma.mean(ma_A)
结果是:
6.0
我不知道麻木。但这会起作用
A = [4,5,7,8,-999]
A = [item for item in A if item != -999]
print sum(A)/float(len(A))
输出
6.0
编辑:
要找到所有子列表的均值,
A = [[4,5,7,8,-999],[3,8,5,7,-999]]
M = [sum(z)/float(len(z)) for z in [[x for x in y if x != -999] for y in A]]
print M
输出
[6.0, 5.75]
from numpy import*
A = [4,5,7,8,-999]
result = mean(A[A!=-999])
print (result)