2

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

2
  • 1
    my_arr[my_mask.astype(bool)]. Commented Jan 29, 2021 at 3:07
  • It is working. Thank you very much. Would you please make it as a reply so I can check the green tick? (for accepting it) Commented Jan 29, 2021 at 3:16

1 Answer 1

3

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.

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

Comments

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.