Login

0 votes

You can use np.where:

>>> a = np.arange(5,10)

>>> np.where(a < 8) # tell me where in a, entries are < 8

(array([0, 1, 2]),) # answer: entries indexed by 0, 1, 2

You can also use np.where if you want to get the entries in array which satisfies the condition

>>> a[np.where(a < 8)]

array([5, 6, 7]) # selects from a entries 0, 1, 2

In case, if a is a 2d array, then np.where will returns the array of column idx's and arrary of row idx's:

>>> a = np.arange(4,10).reshape(2,3)

array([[4, 5, 6],

[7, 8, 9]])

>>> np.where(a > 8)

(array(1), array(2))

In case of 1d array, np.where will be used to get the entries inm 2d array which will satisfies the condition:

>>> a[np.where(a > 8)] # selects from a entries 0, 1, 2

array([9])

Want to learn python to get expertise in the concepts of python? Join python certification course and get certified