简体   繁体   English

numpy masked_array总和

[英]Numpy masked_array sum

I would expect the result of a summation for a fully masked array to be zero, but instead "masked" is returned. 我希望完全掩码数组的求和结果为零,但返回“ masked”。 How can I get the function to return zero? 如何获得返回零的函数?

>>> a = np.asarray([1, 2, 3, 4])
>>> b = np.ma.masked_array(a, mask=~(a > 2))
>>> b
masked_array(data = [-- -- 3 4],
             mask = [ True  True False False],
       fill_value = 999999)

>>> b.sum()
7
>>> b = np.ma.masked_array(a, mask=~(a > 5))
>>> b
masked_array(data = [-- -- -- --],
         mask = [ True  True  True  True],
   fill_value = 999999)


>>> b.sum()
masked
>>> np.ma.sum(b)
masked
>>> 

Here's another unexpected thing: 这是另一件事:

>>> b.sum() + 3
masked

In your last case: 最后一种情况:

In [197]: bs=b1.sum()
In [198]: bs.data
Out[198]: array(0.0)
In [199]: bs.mask
Out[199]: array(True, dtype=bool)
In [200]: repr(bs)
Out[200]: 'masked'
In [201]: str(bs)
Out[201]: '--'

If I specify keepdims , I get a different array: 如果指定keepdimskeepdims得到另一个数组:

In [208]: bs=b1.sum(keepdims=True)
In [209]: bs
Out[209]: 
masked_array(data = [--],
             mask = [ True],
       fill_value = 999999)
In [210]: bs.data
Out[210]: array([0])
In [211]: bs.mask
Out[211]: array([ True], dtype=bool)

here's the relevant part of the sum code: 这是sum码的相关部分:

def sum(self, axis=None, dtype=None, out=None, keepdims=np._NoValue):
    kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}

    _mask = self._mask
    newmask = _check_mask_axis(_mask, axis, **kwargs)
    # No explicit output
    if out is None:
        result = self.filled(0).sum(axis, dtype=dtype, **kwargs)
        rndim = getattr(result, 'ndim', 0)
        if rndim:
            result = result.view(type(self))
            result.__setmask__(newmask)
        elif newmask:
            result = masked
        return result
    ....

It's the 这是

 newmask = np.ma.core._check_mask_axis(b1.mask, axis=None)
 ...
 elif newmask: result = masked

lines that produce the masked value in your case. 在您的情况下产生masked值的行。 newmask is True in the case where all values are masked, and False is some are not. newmask在所有值都被屏蔽的情况下为True,而在某些情况下newmask False。 The choice to return np.ma.masked is deliberate. 返回np.ma.masked的选择是故意的。

The core of the calculation is: 计算的核心是:

In [218]: b1.filled(0).sum()
Out[218]: 0

the rest of the code decides whether to return a scalar or masked array. 其余代码决定返回标量数组还是掩码数组。

============ ============

And for your addition: 并为您添加:

In [232]: np.ma.masked+3
Out[232]: masked

It looks like the np.ma.masked is a special array that propagates itself across calculations. 看起来np.ma.masked是一个特殊的数组,可在计算之间传播。 Sort of like np.nan . np.nannp.nan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM