numpy.ma.masked_where#

ma.masked_where(condition, a, copy=True)[源代码]#

在满足条件的地方屏蔽数组.

返回 a 作为一个数组,其中 condition 为 True 的部分被屏蔽. acondition 的任何被屏蔽的值也会在输出中被屏蔽.

参数:
conditionarray_like

屏蔽条件.当 condition 测试浮点值的相等性时,请考虑使用 masked_values .

aarray_like

要屏蔽的数组.

copybool

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

返回:
resultMaskedArray

屏蔽 acondition 为 True 的部分的结果.

参见

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)