Python numpy 屏蔽一系列值
Python numpy mask a range of values
我有一个名为 img 的二维数组,大小为 100x100。我正在尝试屏蔽所有大于 -100 且小于 100 的值,如下所示。
img = np.ma.masked_where(-100 < img < 100, img)
但是,上面给出了一个错误说
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
谢谢。
您不能对 NumPy 数组使用链式比较,因为它们在后台使用 Python and
。
您必须使用 &
或等效函数 numpy.logical_and
or numpy.bitwise_and
。
例如:
np.ma.masked_where((-100 < img) & (img < 100), img)
你也可以使用masked inside,比如我们可以屏蔽2到5范围内的值:
import numpy as np
from numpy import ma
img = np.arange(9).reshape(3,3)
imgm = ma.masked_inside(img,2,5)
我有一个名为 img 的二维数组,大小为 100x100。我正在尝试屏蔽所有大于 -100 且小于 100 的值,如下所示。
img = np.ma.masked_where(-100 < img < 100, img)
但是,上面给出了一个错误说
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
谢谢。
您不能对 NumPy 数组使用链式比较,因为它们在后台使用 Python and
。
您必须使用 &
或等效函数 numpy.logical_and
or numpy.bitwise_and
。
例如:
np.ma.masked_where((-100 < img) & (img < 100), img)
你也可以使用masked inside,比如我们可以屏蔽2到5范围内的值:
import numpy as np
from numpy import ma
img = np.arange(9).reshape(3,3)
imgm = ma.masked_inside(img,2,5)