I have a numpy array of shape (224,224,160)
and a boolean mask of the same shape.
I mask the array with the mask as such:
new = arr[mask]
I expect new.shape to be 224*224*160 = (8028160,)
Printing new.shape actually gives (1300587,)
Can anyone tell me where this number is coming from?
CodePudding user response:
When you index an array with a mask, you get a flattened (i.e. shape = (N,)
) array containing only the values that the mask allows.
If you're looking for a more pandas-style mask, where values that fail to match the mask are replaced, use np.where
:
np.where(mask, a, np.nan)
That will replace values where mask == False
with np.nan
, and the shape of the returned array will be identical to that of the original array and the mask.
As hpaulj pointed out, the new.shape
number 1300587
is the number of True
values in your mask
. You should get the same number by running sum(mask)
.