Say I have a two dimensional array of coordinates that looks something like
x = array([[1,2],[2,3],[3,4]])
Previously in my work so far, I generated a mask that ends up looking something like
mask = [False,False,True]
When I try to use this mask on the 2D coordinate vector, I get an error
newX = np.ma.compressed(np.ma.masked_array(x,mask))
>>>numpy.ma.core.MaskError: Mask and data not compatible: data size
is 6, mask size is 3.`
which makes sense, I suppose. So I tried to simply use the following mask instead:
mask2 = np.column_stack((mask,mask))
newX = np.ma.compressed(np.ma.masked_array(x,mask2))
And what I get is close:
>>>array([1,2,2,3])
to what I would expect (and want):
>>>array([[1,2],[2,3]])
There must be an easier way to do this?
In your last example, the problem is not the mask. It is your use of
compressed
. From the docstring ofcompressed
:So
compressed
flattens the nonmasked values into a 1-d array. (It has to, because there is no guarantee that the compressed data will have an n-dimensional structure.)Take a look at the masked array before you compress it:
Your
x
is 3x2:Make a 3 element boolean mask:
That can be used to select the rows where it is True, or where it is False. In both cases the result is 2d:
This is without using the MaskedArray subclass. To make such array, we need a mask that matches
x
in shape. There isn't provision for masking just one dimension.Applying
compressed
to that produces a raveled array:array([1, 2, 2, 3])
Since masking is element by element, it could mask one element in row 1, 2 in row 2 etc. So in general
compressing
, removing the masked elements, will not yield a 2d array. The flattened form is the only general choice.np.ma
makes most sense when there's a scattering of masked values. It isn't of much value if you want want to select, or deselect, whole rows or columns.===============
Here are more typical masked arrays:
Is this what you are looking for?
Or from numpy masked array:
Since none of these solutions worked for me, I thought to write down what solution did, maybe it will useful for somebody else. I use python 3.x and I worked on two 3D arrays. One, which I call
data_3D
contains float values of recordings in a brain scan, and the other,template_3D
contains integers which represent regions of the brain. I wanted to choose those values fromdata_3D
corresponding to an integerregion_code
as pertemplate_3D
:which gives me a 1D array of only relevant recordings.