1

I am trying to use python's numpy.argwhere to return the indexes of an array meeting various multiple conditions.

I am having an issue implementing & as well as or into the multiple conditions.

for example x y and z are arrays with the same length that represent simultaneous time series of different variables.

np.argwhere((x>2) & (y=0) & (z>-1) & (z<1)).squeeze()

is simple and works

but I can't get it to work to find where z<-1 or z>1 along with the other conditions met as well.

I keep getting

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

after many logical ways of using parenthesis.

Seanny123
  • 8,776
  • 13
  • 68
  • 124
  • `argwhere` is just the transpose of `where`. All either do is identify where `(x>2) & (y=0) & (z>-1) & (z<1)` boolean array is True. To avoid that `ambiguity` error, pay close attention to the `()`, and use `|` instead of `or` (or `and`). – hpaulj Oct 25 '18 at 19:38
  • i never knew that "|" could be used as "or" in python so i could use an expression like this np.argwhere((x>2) & (z<-1) | (z>1) & (y==0)) and it would give me the desired reslts – Thomas Owen Mazzetti Oct 25 '18 at 19:47
  • 1
    '|' is a `numpy` use, not general Python. It's the elementwise 'or', `np.logical_or`. The Python 'or' has a short circuiting behavior that doesn't play well with the numpy boolean operations. – hpaulj Oct 25 '18 at 19:48
  • so within a numpy functions arguments it serves that purpose – Thomas Owen Mazzetti Oct 25 '18 at 20:00

0 Answers0