Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy where returns empty array

I have an array e.g.

a = [5,1,3,0,2]

I apply the where function:

np.where(a == 2)

The output is an empty array

(array([], dtype=int64),)

I found kind of the same problem here, but in my case it really dosen't make any sense or dose it?

Btw. i'm on a Mac using Python 2.7.10

like image 589
Sebastian N Avatar asked Jan 02 '23 22:01

Sebastian N


1 Answers

You're passing a list to where() function not a Numpy array. Use an array instead:

In [20]: a = np.array([5,1,3,0,2])

In [21]: np.where(a == 2)
Out[21]: (array([4]),)

Also as mentioned in comments, in this case the value of a == 2 is False, and this is the value passed to where. If a is a numpy array, then the value of a == 2 is a numpy array of bools, and the where function will give you the desire results.

like image 88
Mazdak Avatar answered Jan 05 '23 17:01

Mazdak