2

I had a Masked array and a Numpy boolean array containing 3 dimensional values. However when I used indexing of numpy array inside masked array it led to loss of dimensions. I couldn't figure out the reason:

Masked_array = [[[--, 1, --],
                 [--, 1, --],
                 [--, 1, --]]]

Running this line gave me

masked_array = masked_array.mask
mm = ~np.logical_and.accumulate(masked_array)
list(masked_array[mm])

the output as [1, 1, 1] instead of [[1] [1] [1]] I couldnt understand the error and tried various methods. Could you please help me in clarifying the doubt. Thanks

2
  • 1
    You are misusing my answer to your previous question, stackoverflow.com/q/64020001/901925 Commented Sep 23, 2020 at 12:55
  • 1
    I updated my previous answer to show what happens when you apply the accumulate mask to the array. Boolean indexing of a numpy array flattens the result. You get to do your own reshape if you think the results make sense as 2d. numpy does not make any such assumptions for you. Commented Sep 23, 2020 at 18:36

1 Answer 1

1

When indexing a 2D array with a mask with the same shape, you get a 1D array:

a = np.random.random((4,4))
a[np.random.choice([True,False], (4,4))].shape
# (7,)

The original shape is not preserved because as a result from the boolean indexing you'd probably get a jagged array, which numpy does not support. So by default, it just flattens out the result for you as in the example above.

If you know your know that as a result you'll be indexing a column, and you want to preserve the 2D shape, you can always add a new axis:

a = np.array([[[0, 1, 0],
                 [0, 1, 0],
                 [0, 1, 0]]])

masked_array = np.ma.masked_array(a, a==0)

mask = masked_array.mask
mm = ~np.logical_and.accumulate(mask)

masked_array[mm,None].data
array([[1],
       [1],
       [1]])

Though as mentioned, you'll always end up with a squeezed array, which you'll have to reshape according to your needs.

Sign up to request clarification or add additional context in comments.

3 Comments

is there another choice not resulting in loss of dimensions?
I'm not sure what you mean. I think the answer explains why you only get a single dimention by indexing in this way @tom
Thanks Yatu, I wanted to know if there are any other methods that you know of which can prevent this issue from occurring at the first place. Because, for 2d array it messes up and makes it 3d

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.