0

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

2 Answers 2

2

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)?

Sign up to request clarification or add additional context in comments.

Comments

1

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)

1 Comment

my response is the same as Quang Hoang I think he explains it better: np.where returns the arguments where the list have a non-zero element so you were using the original list (for your example a[a>=0] it's a )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.