gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:20:25 26 4
gpt4 key购买 nike

我使用以下示例:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com