【问题标题】:np.where and masked arraynp.where 和掩码数组
【发布时间】:2017-05-04 00:33:01
【问题描述】:

感谢我在 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(或其他函数)仅检索未掩码并匹配我的布尔标准的索引。

【问题讨论】:

  • 您是否尝试过 ma.where() 来自 numpy.ma 子模块?它会产生不同的结果吗?
  • 请出示您的完整代码。在我整理的示例中,一切都按预期工作。
  • [1 -- 3 0 4 7 6 5] 不是有效的 numpy 对象。你指的是什么实物?
  • @Kasramvd 那里,我为他们修好了。
  • @blubberdiblub 与我使用的语法相同,很抱歉造成混淆。

标签: python arrays numpy boolean-operations masked-array


【解决方案1】:

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

我。即:

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

【讨论】:

  • 这是解决方案,但由于这种行为,请阅读my comment
  • @Kasramvd 我认为它没有那么明确。 np.polyfit() 也不适用于掩码数组,并且它会进行计算,而不是比较。
  • 是的,它做了一些计算。虽然它仍然有一些比较(使用拟合多项式的度数),但这并没有什么区别,我认为这是文档中的一个错误。
【解决方案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]'

【讨论】:

    猜你喜欢
    • 2014-01-04
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多