Search code examples
pythonarraysnumpymasking

Masking numpy arrays to select specific rows, based on another boolean array


Suppose that I have an array like this:

my_arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

or a 2D array like:

my_arr = np.array([[1, 1, 11], [2, 1, 0], [3, 3, -1], ..., [10, 9, 0]])

And I define an array like

mask_arr = ([1, 1, 0, 0, 1, 0, 1, 1, 0, 1])

What I want to do from the mask array is to obtain a new array which is consisted of rows, wherein the mask_arr of their index, the element is equal to "1".

For example, the result of the first array would be like:

[1, 2, 0, 0, 5, 0, 7, 8, , 10]

I tried

my_arr[my_mask]

But it didn't work. Is there any solution without wanting to write a for loop to do that?

Thank you in advance


Solution

  • Your mask_arr looks like integer type, and when you slice with an integer array, the array is treated as indexes. So

    my_arr[[0,1,1]]
    

    would give you [row0,row1,row1]. As you mentioned, you want to treat mask_arr as boolean, then you can convert it to boolean:

    my_arr[mask_arr.astype('bool')]
    

    will extract the rows corresponding to the 1 in mask_arr.