Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting argsort of 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 the True elements. There is no get_loc method in numpy like Pandas Series and similarly no index method like a list. I don't want to convert it into a list and then use .index.

Any idea?

like image 608
Baktaawar Avatar asked Oct 20 '22 12:10

Baktaawar


1 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]),)
like image 74
wim Avatar answered Oct 22 '22 20:10

wim