10

I have a few numpy arrays, lets say a, b, and c, and have created a mask to apply to all of them.

I am trying to mask them as such:

a = a[mask]

where mask is a bool array. It is worth noting that I have verified that

len(a) = len(b) = len(c) = len(mask)

And I am getting a rather cryptic sounding warning:

FutureWarning: in the future, boolean array-likes will be handled as a boolean array index

2
  • 1
    That error indicates that you're trying to use a 0-dimensional boolean array as an index. The semantics of that operation are in the process of changing. How did you verify that mask is even a thing with a len? Commented Aug 3, 2016 at 3:41
  • Wait, no, wrong warning. Did you get a list for mask somehow? Commented Aug 3, 2016 at 3:44

1 Answer 1

18

False == 0, and True == 1. If your mask is a list, and not an ndarray, you can get some unexpected behaviour:

>>> a = np.array([1,2,3])
>>> mask_list = [True, False, True]
>>> a[mask_list]
__main__:1: FutureWarning: in the future, boolean array-likes will be handled as a boolean array index
array([2, 1, 2])

where this array is made up of a[1], a[0], and a[1], just like

>>> a[np.array([1,0,1])]
array([2, 1, 2])

On the other hand:

>>> mask_array = np.array(mask_list)
>>> mask_array
array([ True, False,  True], dtype=bool)
>>> a[mask_array]
array([1, 3])

The warning is telling you that eventually a[mask_list] will give you the same as a[mask_array] (which is probably what you wanted it to give you in the first place.)

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

1 Comment

Ahh, thanks so much, makes sense. Fixed by casting the list to a numpy array via mask = np.array(mask_expression, dtype = bool)

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.