numpy.ma.masked_where#
- ma.masked_where(condition, a, copy=True)[源代码]#
当满足条件时掩码数组。
当 condition 为 True 时,返回 a 的一个被掩码的数组。 a 或 condition 中任何被掩码的值在输出中也会被掩码。
- 参数:
- condition类数组
掩码条件。当 condition 测试浮点数值的相等性时,请考虑使用
masked_values代替。- a类数组对象
要掩码的数组。
- copy布尔值
如果为 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])
当 condition 或 a 包含被掩码的值时。
>>> 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)