One Mask subtracting another mask on numpy
Question:
I am new to numpy so any help is appreciated. Say I have two 1-0 masks A and B in 2D numpy array with the same dimension.
Now I would like to do logical operation to subtract B from A
A B Expected Result
1 1 0
1 0 1
0 1 0
0 0 0
But i am not sure it works when a = 0 and b = 1 where a and b are elements from A and B respectively for A = A - B
So I do something like
A = np.where(B == 0, A, 0)
But this is not very readable. Is there a better way to do that
Because for logical or, I can do something like
A = A | B
Is there a similar operator that I can do the subtraction?
Answers:
Since subtraction is not supported for booleans, you need to cast at least one of the arrays to an integer dtype before subtracting. If you want to make sure that the result can’t be negative, you can use numpy.maximum
.
np.maximum(A.astype(int) - B, 0)
The operation that you have described can be given by the following boolean operation
A = A & (~B)
where &
is the element-wise AND operation and ~
is the elementwise NOT operation.
for each elements a
and b
in A
and B
respectively, we have
a = 1 and b = 1 => a & (~b) = 0
a = 1 and b = 0 => a & (~b) = 1
a = 0 and b = 1 => a & (~b) = 0
a = 0 and b = 0 => a & (~b) = 0
Intuitively, this can be simply understood as the following. We interpret each array A and B as sets, each containing only the indices for which the value is 1. (in your case A = {0, 1}
and B = {0,2}
). Then the result we want is a set that contains the elements such that that element is in A AND
NOT
in B.
Note that boolean algebra proves that any binary boolean operation can be acheived using AND
, NOT
, and OR
gates (strictly you need only NOT
and either the AND
or the OR
gate), so naturally, the operation you have specified is no exception.
I am new to numpy so any help is appreciated. Say I have two 1-0 masks A and B in 2D numpy array with the same dimension.
Now I would like to do logical operation to subtract B from A
A B Expected Result
1 1 0
1 0 1
0 1 0
0 0 0
But i am not sure it works when a = 0 and b = 1 where a and b are elements from A and B respectively for A = A - B
So I do something like
A = np.where(B == 0, A, 0)
But this is not very readable. Is there a better way to do that
Because for logical or, I can do something like
A = A | B
Is there a similar operator that I can do the subtraction?
Since subtraction is not supported for booleans, you need to cast at least one of the arrays to an integer dtype before subtracting. If you want to make sure that the result can’t be negative, you can use numpy.maximum
.
np.maximum(A.astype(int) - B, 0)
The operation that you have described can be given by the following boolean operation
A = A & (~B)
where &
is the element-wise AND operation and ~
is the elementwise NOT operation.
for each elements a
and b
in A
and B
respectively, we have
a = 1 and b = 1 => a & (~b) = 0
a = 1 and b = 0 => a & (~b) = 1
a = 0 and b = 1 => a & (~b) = 0
a = 0 and b = 0 => a & (~b) = 0
Intuitively, this can be simply understood as the following. We interpret each array A and B as sets, each containing only the indices for which the value is 1. (in your case A = {0, 1}
and B = {0,2}
). Then the result we want is a set that contains the elements such that that element is in A AND
NOT
in B.
Note that boolean algebra proves that any binary boolean operation can be acheived using AND
, NOT
, and OR
gates (strictly you need only NOT
and either the AND
or the OR
gate), so naturally, the operation you have specified is no exception.