Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert numpy array from values to indices

I have a numpy array a: [True, False, True, False, False, ...] And I like to have a numpy array that has the indices of the True and False values, i.e. [0, 2, ...] and [1, 3, 4, ...]

like image 725
Michel Keijzers Avatar asked Mar 28 '26 16:03

Michel Keijzers


1 Answers

To get the indices of the True values in a, you can use

a.nonzero()

For the indices of the False values, use

(~a).nonzero()
like image 86
Sven Marnach Avatar answered Mar 31 '26 09:03

Sven Marnach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!