Home > Back-end >  Numpy 'where' returns empty array for all 0s array for condition >= 0
Numpy 'where' returns empty array for all 0s array for condition >= 0

Time:10-22

I am using np.where function to get indices where the value matches 0. But the following code returns me empty array, which is not expected.

import numpy as np
a = np.array([0,0,0,0])
np.where(a[a>=0])

This gives: (array([], dtype=int64),).

Could anyone please point out what I am missing?

https://numpy.org/doc/stable/reference/generated/numpy.where.html

CodePudding user response:

That's expected. a>=0 gives you all True, i.e a[a>=0] gives you a, which does not contain any non-zero element. So np.where(a) returns empty array.

Are you looking for np.where(a>=0)?

CodePudding user response:

you are telling to find index where a[ a>=0] a>=0 returns a [True,True,True,True] then a[a>=0] returns [0,0,0,0] then there's no True condition to return.

Maybe you want to do

 np.where(a>=0)
  • Related