简体   繁体   English

掩码 Numpy 数组中的操作

[英]Operations in Masked Numpy Arrays

I have two masked numpy arrays.我有两个屏蔽的 numpy 数组。 These are images.这些是图像。

I am trying to subtract one from another.我试图从另一个中减去一个。

If I do the standard subtraction operator,如果我做标准减法运算符,

ma1 - ma2 

It will subtract them as if they were not masked (their masks are not considered).它将减去它们,就好像它们没有被屏蔽一样(不考虑它们的屏蔽)。 I would like them to subtract with their masks.我希望他们用他们的面具减去。

Does anyone know how to subtract masked numpy arrays from eachother?有谁知道如何从彼此中减去屏蔽的 numpy 数组?

It should work.它应该工作。 When you operate on masked arrays, it takes the union of the masks involved in the operation.当您对掩码数组进行操作时,它需要操作中涉及的掩码的并集。 The case below shows how numpy chooses the values that will be changed when you do a subtraction between two masked arrays:下面的案例显示了numpy如何选择在两个屏蔽数组之间进行减法时将更改的值:

a1 = np.random.random((100,100))
a2 = np.random.random((100,100))

a1 = np.ma.array(a1, mask=a1<0.5)
a2 = np.ma.array(a2, mask=a2<0.5)

umask = np.logical_or(a1.mask, a2.mask) # <-- union of the masks

test = a1.data - a2.data
test[umask] = a1.data[umask] # <-- "canceling" the operation according to the
                             #     combined mask

np.allclose((a1-a2), test)
#True

As you see, the result is the same...如你所见,结果是一样的......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM