4

感谢我在stackoverflow上获得的一些帮助,我正在使用掩码数组,但是我在对掩码数组进行np.where评估时遇到了问题。

我的蒙面数组是:

m_pt0 = np.ma.masked_array([1, 2, 3, 0, 4, 7, 6, 5],
                           mask=[False, True, False, False,
                                 False, False, False, False])

并像这样打印:

In [24]: print(m_pt0)
[1 -- 3 0 4 7 6 5]

我正在寻找 m_pt0 中 m_pt0 = 0 的索引,我希望

np.where(0 == m_pt0)

会返回:

(array([3]))

然而,尽管面具(或因为?),我反而得到

(array([1, 3]),)

使用掩码的全部目的是避免访问“空白”的索引,所以我怎样才能使用 where (或其他函数)来仅检索未掩码并匹配我的布尔标准的索引。

4

2 回答 2

7

您需要使用where()函数的掩码变体,否则它将为掩码数组返回错误或不需要的结果。其他功能也是如此,例如polyfit().

IE。:

In [2]: np.ma.where(0 == m_pt0)
Out[2]: (array([3]),)
于 2017-05-03T13:50:06.093 回答
2

平等测试可能会造成混乱。结果是另一个掩码数组:

In [19]: 0 == m_pt0
Out[19]: 
masked_array(data = [False -- False True False False False False],
             mask = [False  True False False False False False False],
       fill_value = True)

掩码数组具有.data.mask属性。 numpy不知道 MA 的功能只需查看.data

In [20]: _.data
Out[20]: array([False,  True, False,  True, False, False, False, False], dtype=bool)

np.where看到 2 True,然后返回

In [23]: np.where(0 == m_pt0)
Out[23]: (array([1, 3], dtype=int32),)
In [24]: np.where((0 == m_pt0).data)
Out[24]: (array([1, 3], dtype=int32),)

在可能的情况下,最好使用np.ma函数的版本:

In [25]: np.ma.where(0 == m_pt0)
Out[25]: (array([3], dtype=int32),)

查看代码,np.source(np.ma.where)我看到了

if missing == 2:
    return filled(condition, 0).nonzero()
(plus lots of code for the 3 argument use)

这样filled做:

In [27]: np.ma.filled((0 == m_pt0),0)
Out[27]: array([False, False, False,  True, False, False, False, False], dtype=bool)

MA函数通常用无害的东西(在这种情况下为 0)替换掩码值,或用于compressed将它们从考虑中删除。

In [36]: m_pt0.compressed()
Out[36]: array([1, 3, 0, 4, 7, 6, 5])
In [37]: m_pt0.filled(100)
Out[37]: array([  1, 100,   3,   0,   4,   7,   6,   5])

如果将工作委托给数组自己的方法,则 numpy 函数将在 MA 上正常工作。

In [41]: np.nonzero(m_pt0)
Out[41]: (array([0, 2, 4, 5, 6, 7], dtype=int32),)
In [42]: m_pt0.nonzero()
Out[42]: (array([0, 2, 4, 5, 6, 7], dtype=int32),)
In [43]: np.where(m_pt0)
Out[43]: (array([0, 1, 2, 4, 5, 6, 7], dtype=int32),)

np.nonzero代表。 np.where才不是。


掩码数组的repr显示掩码。它只str显示屏蔽数据:

In [31]: m_pt0
Out[31]: 
masked_array(data = [1 -- 3 0 4 7 6 5],
             mask = [False  True False False False False False False],
       fill_value = 999999)
In [32]: str(m_pt0)
Out[32]: '[1 -- 3 0 4 7 6 5]'
于 2017-05-03T16:10:56.803 回答