numpy masked_array 掩码改变类型
numpy masked_array mask changes type
如果值不在数组中,而是标量,np.ma.masked_equal
或 masked_values
不会创建 False 掩码,这让我感到有些惊讶。
示例:
y = np.arange(10)
yy = np.ma.masked_equal(y,0)
产生一个掩码数组,掩码是一个包含 10 个假值的数组,而
y = np.arange(1,10)
yy = np.ma.masked_equal(y,0)
生成一个掩码数组,掩码设置为标量 False。结果,鉴于在我的代码中我事先不知道掩码是否匹配数组中的任何条目,我被迫明确检查:
yy=np.ma.masked_values(y,0)
if np.isscalar(yy.mask):
yy.mask=np.zeros(y.shape,dtype=bool)
这对我来说似乎工作过度了。这种行为的原因是什么,有没有办法避免它?
您可以自己创建 MaskedArray
:
yy = np.ma.MaskedArray(y, mask=(y==0))
似乎 NumPy 试图最小化内存需求并加快计算速度,以防 MaskedArray
被揭露。
numpy.ma.nomask
Value indicating that a masked array has no invalid entry. nomask
is used internally to speed up computations when the mask is not needed.
如果您勾选:
>>> np.ma.nomask
False
所以单个False
代表“不戴口罩”。所以你也可以检查 maskedarr.mask is np.ma.nomask
(这是一个 garantueed constant):
yy = some_operation_that_creates_a_masked_array
if yy.mask is np.ma.nomask:
yy.mask = np.zeros(yy.shape, dtype=bool)
这比 np.isscalar
包含更多上下文。
如果值不在数组中,而是标量,np.ma.masked_equal
或 masked_values
不会创建 False 掩码,这让我感到有些惊讶。
示例:
y = np.arange(10)
yy = np.ma.masked_equal(y,0)
产生一个掩码数组,掩码是一个包含 10 个假值的数组,而
y = np.arange(1,10)
yy = np.ma.masked_equal(y,0)
生成一个掩码数组,掩码设置为标量 False。结果,鉴于在我的代码中我事先不知道掩码是否匹配数组中的任何条目,我被迫明确检查:
yy=np.ma.masked_values(y,0)
if np.isscalar(yy.mask):
yy.mask=np.zeros(y.shape,dtype=bool)
这对我来说似乎工作过度了。这种行为的原因是什么,有没有办法避免它?
您可以自己创建 MaskedArray
:
yy = np.ma.MaskedArray(y, mask=(y==0))
似乎 NumPy 试图最小化内存需求并加快计算速度,以防 MaskedArray
被揭露。
numpy.ma.nomask
Value indicating that a masked array has no invalid entry.
nomask
is used internally to speed up computations when the mask is not needed.
如果您勾选:
>>> np.ma.nomask
False
所以单个False
代表“不戴口罩”。所以你也可以检查 maskedarr.mask is np.ma.nomask
(这是一个 garantueed constant):
yy = some_operation_that_creates_a_masked_array
if yy.mask is np.ma.nomask:
yy.mask = np.zeros(yy.shape, dtype=bool)
这比 np.isscalar
包含更多上下文。