python - Scipy KDTree 获取由两点定义的网格的矩形子集

标签 python scipy kdtree

我使用以下示例:

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]])

如何从树数据中实现这一点?

最佳答案

似乎我找到了解决方案:

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])))]

但是,我希望看到使用查询选项的解决方案!

关于python - Scipy KDTree 获取由两点定义的网格的矩形子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54311732/

相关文章:

python - 如何使用基于星期几的日期时间创建日期?

python - 包含最大值的字典

python - Numpy __array_prepare__ 错误

python - 在 Python 中求解 6 个非线性方程组时出现问题

python - 从继承实例访问本地类变量

python - 使用python逐字打印乌尔都语(非拉丁语言)

c++ - 分割大量的3D点数据

c++ - 用于查找 3D 中最近三角形的数据结构

python - 在 Windows 7 64 位上从 IDLE 运行 Python 脚本

python - scipy.spatial ValueError : "x must consist of vectors of length %d but has shape %s"