使用Scipy KDTree获取由两个点定义的网格矩形子集

3

我正在使用以下示例:

from scipy import spatial
x, y = np.mgrid[0:5, 2:8]
tree = spatial.KDTree(list(zip(x.ravel(), y.ravel())))
pts = np.array([[0, 0], [2.1, 2.9]])
idx = tree.query(pts)[1]
data = tree.data[??????????]

如果我输入两个任意点(请参见变量pts),我希望返回所有坐标对,这些坐标在由这两个点定义的矩形内(KDTree找到最近邻)。因此,在这种情况下:
array([[0, 0],
       [0, 1],
       [0, 2],
       [1, 0],
       [1, 1],
       [1, 2],
       [2, 0],
       [2, 1],
       [2, 2]])

我该如何从树形数据中实现这一点?
1个回答

0

看起来我找到了一个解决方案:

from scipy import spatial
import numpy as np
x, y = np.mgrid[0:5, 0:5]
tree = spatial.KDTree(list(zip(x.ravel(), y.ravel())))
pts = np.array([[0, 0], [2.1, 2.2]])
idx = tree.query(pts)[1]
data = tree.data[[idx[0], idx[1]]]
rectangle = tree.data[np.where((tree.data[:,0]>=min(data[:,0])) & (tree.data[:,0]<=max(data[:,0])) & (tree.data[:,1]>=min(data[:,1])) & (tree.data[:,1]<=max(data[:,1])))]

不过,我很想看到使用查询选项的解决方案!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接