我想在边界(B4X1,B4X2)和(B4Y1,B4Y2)之间找到dataX
和dataY
中的元素
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)
如何更快地做到这一点?
我做为:
OK = (B4X1 < dataX < B4X2) & (B4Y1 < dataY < B4Y2)
print OK
但是错误:
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()
最佳答案
快捷方式B4X1 < dataX < B4X2
不起作用。
您需要执行以下操作:
OK = (B4X1 < dataX) & (dataX < B4X2) & (B4Y1 < dataY) & (dataY < B4Y2)
编辑:
自从计时开始:
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