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
一个带掩码的数组,其中
masked元素是条件被掩码的位置,来自 x 的元素是 condition 为 True 的位置,以及来自 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)