Warning boolean numpy array?

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

I am trying to disguise them as such:

a = a[mask]

where mask is the bool array. It is worth noting that I confirmed that

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

And I get a rather cryptic sound warning:

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

+6
source share
1 answer

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

 >>> 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 consists of [1], a [0] and a [1], 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 tells you that ultimately a[mask_list] will give you the same thing as a[mask_array] (which you probably wanted it to give you first.)

+13
source

All Articles