我有两个带xy坐标的点阵列:
basic_pts = np.array([[0, 0], [1, 0], [2, 0], [0, 1], [1, 1], [0, 2]])
new_pts = np.array([[2, 2], [2, 1], [0.5, 0.5], [1.5, 0.5]])
结果我只希望从数组new_pts
那些点满足basic_pts
中没有点且x和y值更大的条件。结果就是
res_pts = np.array([[2, 2], [2, 1], [1.5, 0.5]])
我有一个有效的解决方案,但由于使用列表理解,它不适合更大量的数据。
x_cond = ([basic_pts[:, 0] > x for x in new_pts[:, 1]])
y_cond = ([basic_pts[:, 1] > y for y in new_pts[:, 1]])
xy_cond_ = np.logical_and(x_cond, y_cond)
xy_cond = np.swapaxes(xy_cond_, 0, 1)
mask = np.invert(np.logical_or.reduce(xy_cond))
res_pts = new_pts[mask]
有没有更好的方法只用numpy和没有列表理解来解决这个问题?
答案 0 :(得分:1)
您可以使用NumPy broadcasting
-
# Get xy_cond equivalent after extending dimensions of basic_pts to a 2D array
# version by "pushing" separately col-0 and col-1 to axis=0 and creating a
# singleton dimension at axis=1.
# Compare these two extended versions with col-1 of new_pts.
xyc = (basic_pts[:,0,None] > new_pts[:,1]) & (basic_pts[:,1,None] > new_pts[:,1])
# Create mask equivalent and index into new_pts to get selective rows from it
mask = ~(xyc).any(0)
res_pts_out = new_pts[mask]
答案 1 :(得分:0)
正如val指出的那样,创建中间len(basic_pts)
×len(new_pts)
数组的解决方案可能会占用大量内存。另一方面,在循环中测试new_pts
中的每个点的解决方案可能太耗时。我们可以通过选择批量大小 k 并使用Divakar的解决方案以批量 k 批量测试new_pts
来缩小差距:
basic_pts = np.array([[0, 0], [1, 0], [2, 0], [0, 1], [1, 1], [0, 2]])
new_pts = np.array([[2, 2], [2, 1], [0.5, 0.5], [1.5, 0.5]])
k = 2
subresults = []
for i in range(0, len(new_pts), k):
j = min(i + k, len(new_pts))
# Process new_pts[i:j] using Divakar's solution
xyc = np.logical_and(
basic_pts[:, np.newaxis, 0] > new_pts[np.newaxis, i:j, 0],
basic_pts[:, np.newaxis, 1] > new_pts[np.newaxis, i:j, 1])
mask = ~(xyc).any(axis=0)
# mask indicates which points among new_pts[i:j] to use
subresults.append(new_pts[i:j][mask])
# Concatenate subresult lists
res = np.concatenate(subresults)
print(res)
# Prints:
array([[ 2. , 2. ],
[ 2. , 1. ],
[ 1.5, 0.5]])