numpy.ma.where#
- ma.where(condition, x=<no value>, y=<no value>)[源代码]#
根据条件,返回一个包含来自 x 或 y 的元素的已屏蔽数组。
注意
当仅提供 condition 时,此函数与
nonzero相同。本文档的其余部分仅涵盖提供所有三个参数的情况。- 参数:
- conditionarray_like, bool
当为 True 时,产生 x,否则产生 y。
- x, yarray_like, optional
从中选择值的元素。x、y 和 condition 需要能够广播到某个形状。
- 返回:
- outMaskedArray
一个已屏蔽数组,其中在 condition 被屏蔽的位置为
masked元素,在 condition 为 True 的位置为 x 中的元素,其他位置为 y 中的元素。
另请参阅
numpy.where顶级 NumPy 模块中的等效函数。
nonzero当省略 x 和 y 时调用的函数
示例
>>> import numpy as np >>> x = np.ma.array(np.arange(9.).reshape(3, 3), mask=[[0, 1, 0], ... [1, 0, 1], ... [0, 1, 0]]) >>> x masked_array( data=[[0.0, --, 2.0], [--, 4.0, --], [6.0, --, 8.0]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=1e+20) >>> np.ma.where(x > 5, x, -3.1416) masked_array( data=[[-3.1416, --, -3.1416], [--, -3.1416, --], [6.0, --, 8.0]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=1e+20)