1

How can I filter a numpy array using a pair of inequalities, such as:

>>> a = np.arange(10)
>>> a[a <= 6]
array([0, 1, 2, 3, 4, 5, 6])
>>> a[3 < a]
array([4, 5, 6, 7, 8, 9])
>>>
>>> a[3 < a <= 6]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous.
 Use a.any() or a.all()

I get the same response if I try a.all(3 < a <= 6)

np.array([x for x in a if 3 < x <= 6]) works, but it seems nasty. What's the right way to do this?

Nick T
  • 25,754
  • 12
  • 83
  • 121

1 Answers1

6

You need to do:

a[(3 < a) & (a <= 6)]

It's a "wart" in python. In python (3 < a <=6) is translated to ((3 < a) and (a <= 6)). However numpy arrays don't work with the and operation because python doesn't allow overloading of the and and or operators. Because of that numpy uses & and |. There was some discussion about fixing this about a year ago, but I haven't seem much about it since.

http://mail.python.org/pipermail/python-dev/2012-March/117510.html

Bi Rico
  • 25,283
  • 3
  • 52
  • 75
  • Ah, I tried breaking it into `a[3 < a and a <= 6]` but it spat out the same thing so I gave up there; didn't consider the bitwise operators. – Nick T Mar 08 '13 at 00:04