Boolean Indexing, but turns out to be another operation

I tried to do logical indexing but ..

np.random.randn(8).reshape((4,2)) Out[11]: array([[-1.13058416, 1.08397186], [-1.2730122 , 0.78306498], [-0.05370502, -1.16723298], [ 1.01750955, -0.95029671]]) a=np.random.randn(8).reshape((4,2)) a[[2==3,3==0,0==0,1!=1]] Out[13]: array([[ 0.18235299, -2.53482367], [ 0.18235299, -2.53482367], [-1.03752809, -2.2790847 ], [ 0.18235299, -2.53482367]]) 

What just happened? I was thinking more about Bool indexing. What kind of operation is this? I do not ask to fix it for indexing Bool. Rather, I ask what happens in this operation? Is it legal?

+2
source share
1 answer

It's easy to think of ndarray as an oval list s. Broadcast and array operations automatically apply to the lists involved in these operations, so you can add an array and a list of broadcast-compatible figures, and numpy will not try to combine the two (as it will try to do with two lists).

One huge (and for me, confusing) exception is fancy indexing . Unusual indexing is already confusing me (like someone from MATLAB), since it is strange that the following two give a different result:

 import numpy as np A = np.random.rand(3,3) A[0:1,0:1] A[range(2),range(2)] 

The first operation cuts and returns the submatrix 2 by 2. The latter refers to fancy indexing and returns only a 2-element array containing A[0,0] and A[1,1] .

Your question is related to something odd: lists and arrays of booleans behave differently when used in fantastic indexing. Consider the following two examples according to your question:

 A = np.random.rand(4,2) bool_index_list = [False, True, True, False] bool_index_array = np.array(bool_index_list) A[bool_index_list].shape A[bool_index_array].shape 

The former returns (4,2) , the latter (2,2) .

In the first case, since the index is list , Boolean values ​​are converted to the corresponding integers, and the resulting values [0,1,1,0] are used as actual indices in the matrix, returning [first, second, second, first], respectively.

In the latter case, the index of array of dtype=bool used as you would expect it to be: it is used as a mask to ignore those rows A for which the index is False .

numpy release notes , among other things, indicate that

In the future, Boolean array-likes (e.g. python bools lists) will always be treated as logical indexes, and Boolean scalars (including python True ) will be legal logical index.

Accordingly, the list-based indexing examples above give me the following warning in numpy 1.10.1:

FutureWarning: in the future, boolean array-likes will be treated as an index of a boolean array

So, the short answer to your question is that it is legal, but not for long. Stick to ndarray phase-indexing and you should not experience any bumps along the way.

+2
source

All Articles