np.where 和掩码数组
np.where and masked array
感谢我在 Whosebug 上获得的一些帮助,我正在使用掩码数组,但我 运行 遇到掩码数组的 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)
会 return:
(array([3]))
然而,尽管有面具(或因为?),我反而得到
(array([1, 3]),)
使用掩码的全部意义在于避免访问 "blank" 的索引,因此我如何使用 where(或其他函数)仅检索未掩码且符合我的布尔标准的索引。
您需要使用 where()
函数的掩码变体,否则它将 return 掩码数组的错误或不需要的结果。其他功能也是如此,例如 polyfit()
.
我。即:
In [2]: np.ma.where(0 == m_pt0)
Out[2]: (array([3]),)
相等性测试可能会造成混淆。结果是另一个掩码数组:
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
,returns
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]'
感谢我在 Whosebug 上获得的一些帮助,我正在使用掩码数组,但我 运行 遇到掩码数组的 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)
会 return:
(array([3]))
然而,尽管有面具(或因为?),我反而得到
(array([1, 3]),)
使用掩码的全部意义在于避免访问 "blank" 的索引,因此我如何使用 where(或其他函数)仅检索未掩码且符合我的布尔标准的索引。
您需要使用 where()
函数的掩码变体,否则它将 return 掩码数组的错误或不需要的结果。其他功能也是如此,例如 polyfit()
.
我。即:
In [2]: np.ma.where(0 == m_pt0)
Out[2]: (array([3]),)
相等性测试可能会造成混淆。结果是另一个掩码数组:
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
,returns
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]'