Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple conditions in numpy.where [duplicate]

Tags:

python

numpy

With some numpy array a, what I'd like to do is

indices = np.where((a < 4) or (a > 12))

This isn't valid. It just returns "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()". But this expression isn't ambiguous, and any and all don't do what I want to do. (any and all can't take compound expressions either. But if can. Confused...)

like image 846
user2162806 Avatar asked Jan 13 '23 23:01

user2162806


1 Answers

You want to get a logical/boolean array as your argument for where

You can do x | y or np.logical_or(x,y) , where x and y are a < 4 and a > 12

like image 95
YXD Avatar answered Jan 19 '23 00:01

YXD