Reputation: 3241
I want to find the elements in dataX
and dataY
which are between boundaries (B4X1, B4X2) and (B4Y1, B4Y2)
import numpy as np
B4X1 = 50.
B4X2 = 60.
B4Y1 = 150.
B4Y2 = 160.
dataX = np.array([40, 25, 50, 60, 55])
dataY = np.array([140, 125, 150, 160, 155])
Expected result is:
result = array([False, False, False, False, True], dtype=bool)
How would be the faster way of doing it?
I did as:
OK = (B4X1 < dataX < B4X2) & (B4Y1 < dataY < B4Y2)
print OK
But the error:
Traceback (most recent call last):
File "C:\Users\je\Desktop\test.py", line 14, in <module>
OK = (B4X1 < dataX < B4X2) & (B4Y1 < dataY < B4Y2)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Upvotes: 2
Views: 76
Reputation: 944
The shortcut notation B4X1 < dataX < B4X2
doesn't work.
You need to do the following:
OK = (B4X1 < dataX) & (dataX < B4X2) & (B4Y1 < dataY) & (dataY < B4Y2)
EDIT:
Since timing was brought up:
In [23]: dataX = np.random.randint(200, size=100)
In [24]: dataY = np.random.randint(200, size=100)
In [25]: %timeit OK = (B4X1 < dataX) & (dataX < B4X2) & (B4Y1 < dataY) & (dataY < B4Y2)
10000 loops, best of 3: 23.6 µs per loop
In [26]: %timeit OK = np.logical_and.reduce([B4X1<dataX,dataX<B4X2,B4Y1<dataY,dataY<B4Y2])
10000 loops, best of 3: 26.7 µs per loop
In [27]: %timeit for i in dataX: OK = (B4X1 < i and i < B4X2) and (B4Y1 < i and i < B4Y2)
1000 loops, best of 3: 449 µs per loop
In [28]: %timeit for i in dataX: OK = (B4X1 < i and i < B4X2) and (B4Y1 < i and i < B4Y2)
1000 loops, best of 3: 329 µs per loop
Upvotes: 2
Reputation: 107347
Since you want to apply a logical_and
operation on all of your conditions as an elegant way you can use np.logical_and.reduce
:
>>> np.logical_and.reduce([B4X1<dataX,dataX<B4X2,B4Y1<dataY,dataY<B4Y2])
array([False, False, False, False, True], dtype=bool)
Upvotes: 1