Reputation: 33
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
Upvotes: 2
Views: 178
Reputation: 88226
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.
Upvotes: 1