Getting argsort from a numpy array

I have a numpy array as follows:

array([ True, True, True, True, True, False, True, True, False, True, False, True, True, True, True, True, True, False, False, False, False, False, True, True, False, False, False, True, True, True, True, True, True, True, False, True, True, True, True, False, True, True, False, False, True, True, True, False, True, True, True, False], 

I want to get the indices of all True elements. There is no get_loc method in numpy like the Pandas Series, and likewise there is no index method such as a list. I do not want to convert it to a list and then use .index.

Any idea?

+5
source share
3 answers

Use ndarray.nonzero :

 >>> a.nonzero() (array([ 0, 1, 2, 3, 4, 6, 7, 9, 11, 12, 13, 14, 15, 16, 22, 23, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 40, 41, 44, 45, 46, 48, 49, 50]),) 
+4
source

To do this in pandas:

 In [255]: s[s==True].index Out[255]: Int64Index([0, 1, 2, 3, 4, 6, 7, 9, 11, 12, 13, 14, 15, 16, 22, 23, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 40, 41, 44, 45, 46, 48, 49, 50], dtype='int64') 

Update

In fact, you can use the fact that the values โ€‹โ€‹are already Boolean values โ€‹โ€‹to mask the series:

 In [256]: s[s].index Out[256]: Int64Index([0, 1, 2, 3, 4, 6, 7, 9, 11, 12, 13, 14, 15, 16, 22, 23, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 40, 41, 44, 45, 46, 48, 49, 50], dtype='int64') 

Similarly for numpy arrays, you can use booleans to mask the array and get index values โ€‹โ€‹using np.where :

 In [261]: np.where(a)โ€‹ Out[261]: (array([ 0, 1, 2, 3, 4, 6, 7, 9, 11, 12, 13, 14, 15, 16, 22, 23, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 40, 41, 44, 45, 46, 48, 49, 50], dtype=int64),) 
0
source

np.ix_ path seems the slowest.

 In [846]: % timeit a.nonzero() 1000000 loops, best of 3: 707 ns per loop In [845]: % timeit np.where(a) 1000000 loops, best of 3: 883 ns per loop In [849]: %timeit np.ix_(a==True) 100000 loops, best of 3: 9.21 ยตs per loop 
0
source

All Articles