numpy.ma.masked_where#

ma.masked_where(condition, a, copy=True)[source]#

在满足条件的位置对数组进行掩码。

返回一个数组,其中 acondition 为 True 的位置被掩码。 acondition 中任何已掩码的值在输出中也将被掩码。

参数
conditionarray_like

掩码条件。当 condition 针对浮点值进行相等性测试时,请考虑改用 masked_values

aarray_like

要掩码的数组。

copybool

如果为 True(默认),则在结果中创建 a 的副本。如果为 False,则就地修改 a 并返回一个视图。

返回
resultMaskedArray

condition 为 True 的位置对 a 进行掩码的结果。

另请参见

masked_values

使用浮点相等性进行掩码。

masked_equal

当等于给定值时掩码。

masked_not_equal

当*不*等于给定值时掩码。

masked_less_equal

当小于或等于给定值时掩码。

masked_greater_equal

当大于或等于给定值时掩码。

masked_less

当小于给定值时掩码。

masked_greater

当大于给定值时掩码。

masked_inside

在给定区间内掩码。

masked_outside

在给定区间外掩码。

masked_invalid

掩码无效值(NaN 或 inf)。

示例

>>> import numpy as np
>>> import numpy.ma as ma
>>> a = np.arange(4)
>>> a
array([0, 1, 2, 3])
>>> ma.masked_where(a <= 2, a)
masked_array(data=[--, --, --, 3],
             mask=[ True,  True,  True, False],
       fill_value=999999)

根据 a 的条件掩码数组 b

>>> b = ['a', 'b', 'c', 'd']
>>> ma.masked_where(a == 2, b)
masked_array(data=['a', 'b', --, 'd'],
             mask=[False, False,  True, False],
       fill_value='N/A',
            dtype='<U1')

参数 copy 的效果。

>>> c = ma.masked_where(a <= 2, a)
>>> c
masked_array(data=[--, --, --, 3],
             mask=[ True,  True,  True, False],
       fill_value=999999)
>>> c[0] = 99
>>> c
masked_array(data=[99, --, --, 3],
             mask=[False,  True,  True, False],
       fill_value=999999)
>>> a
array([0, 1, 2, 3])
>>> c = ma.masked_where(a <= 2, a, copy=False)
>>> c[0] = 99
>>> c
masked_array(data=[99, --, --, 3],
             mask=[False,  True,  True, False],
       fill_value=999999)
>>> a
array([99,  1,  2,  3])

conditiona 包含掩码值时。

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