[英]Averaging numpy masked array over multiple dimensions
It is possible to compute the average of a numpy array over multiple dimensions, as in eg. 例如,可以在多个维度上计算numpy数组的平均值。 my_ndarray.mean(axis=(1,2))
. my_ndarray.mean(axis=(1,2))
。
However, it does not seem to work with a masked array : 但是,它似乎不适用于蒙版数组 :
>>> import numpy as np
>>> a = np.random.randint(0, 10, (2, 2, 2))
>>> a
array([[[0, 9],
[2, 5]],
[[8, 6],
[0, 7]]])
>>> a.mean(axis=(1, 2))
array([ 4. , 5.25])
>>> ma = np.ma.array(a, mask=(a < 5))
>>> ma.mean(axis=(1, 2))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/numpy/ma/core.py", line 5066, in mean
cnt = self.count(axis=axis)
File "/usr/lib/python2.7/site-packages/numpy/ma/core.py", line 4280, in count
n1 = np.size(m, axis)
File "/usr/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2700, in size
return a.shape[axis]
TypeError: tuple indices must be integers, not tuple
How can I compute the average of a masked array over multiple axis, preferably as simply as it would be for a normal array? 如何计算多轴上的蒙版阵列的平均值,最好像普通阵列一样简单?
(I would rather use a solution that does not implies defining a new function, as proposed in this answer .) (我宁愿使用不暗示定义新功能的解决方案,如本答案中所建议的。)
I found out that though np.ma.mean
does not works, np.ma.average
gives the expected result: 我发现尽管np.ma.mean
不起作用,但np.ma.average
给出了预期的结果:
>>> np.ma.average(ma, axis=(1,2))
masked_array(data = [7.0 7.0],
mask = [False False],
fill_value = 1e+20)
This is confusing since for regular array, np.average
is a mere wrapper around np.mean
. 这是感到困惑,因为规则排列, np.average
大约是一个单纯的包装np.mean
。 But as long as it works, I won't complain! 但是只要有效,我就不会抱怨!
You can reshape it before the mean : 您可以在均值之前重塑它:
>>>ma.reshape(mc.shape[0],-1).mean(1)
masked_array(data = [1.6666666666666667 4.0],
mask = [False False],
fill_value = 1e+20)
Note that partial application of averaging lead to ambiguous results : 请注意,部分应用平均会导致模棱两可的结果:
>>> ma.mean(1).mean(1)
masked_array(data = [1.5 4.0],
mask = [False False],
fill_value = 1e+20)
>>> ma.mean(2).mean(1)
masked_array(data = [2.25 4.0],
mask = [False False],
fill_value = 1e+20)
Explained by : 解释者:
>>>ma
masked_array(data =
[[[0 1]
[4 --]]
[[-- --]
[-- 4]]],
mask =
[[[False False]
[False True]]
[[ True True]
[ True False]]],
fill_value = 999999)
The weights are not the same in each case. 每种情况下的权重都不相同。
To average on other dimensions, you can use np.rollaxis before. 要平均其他尺寸,可以在之前使用np.rollaxis。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.