0

I have a boolean array, from which I want to get the index values of all the True elements in that bool array. I read this answer on stack overflow which suggested using np.where(mask). In their answer the tuple that was returned contained only the indices and had 1 empty entry. In the case below I seem to also get a array of 0s. Why? Also I cant find what the default entries are in the documentation so I have no idea what np.where(mask) is actually doing. Normally I thought you need to pass both a condition and values to yield where condition is true and where its false, what is the condition here and what is it returning?

Code:

import numpy as np 

x = np.arange(9)
mask  = [x%3==0]
print('mask:',mask)
true_ixs = np.where(mask)
print('trueixs:',true_ixs)

Output:

mask: [array([ True, False, False,  True, False, False,  True, False, False])]
trueixs: (array([0, 0, 0]), array([0, 3, 6]))
visual360
  • 5
  • 2

1 Answers1

0

As the documentation says:

The function that is called when x and y are omitted: np.nonzero

v.tralala
  • 1,444
  • 3
  • 18
  • 39