Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unexpected behavior from masking numpy array with boolean mask

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,)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Printing new.shape actually gives (1300587,)

Can anyone tell me where this number is coming from?

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading