如何使用 Scipy 的 cKDTree 查询最近邻居,包括距离为零的邻居?

Rai*_*old 2 python scipy

我想使用 Scipy cKDTree 在一定距离内找到邻居。最重要的是,我也想要点本身(零距离)。cKD 树。查询给出所有邻居但没有零距离。

有没有办法做到这一点?

mgc*_*mgc 5

我无法真正重现您的问题(或者它可能取决于您用来查询树的方法)。

考虑这个简单的代码片段:

>>> from scipy.spatial import cKDTree
>>> import numpy as np

>>> points_ref = np.array([(1, 1), (3, 3), (4, 4), (5, 4), (6, 6)])
>>> tree = cKDTree(points_ref)
Run Code Online (Sandbox Code Playgroud)

使用该方法查询2点周围距离的最近邻居可以给出类似的结果:(4, 4)cKDTree.query_ball_point

>>> idx = tree.query_ball_point((4, 4), 2)
>>> points_ref[idx]
# array([[3, 3], [4, 4], [5, 4]])
Run Code Online (Sandbox Code Playgroud)

它返回距离为 0 的点。

使用该方法查询n 个最近cKDTree.query似乎也返回距离为 0 的点:

>>> _, idx = tree.query((3, 3), k=2)
>>> points_ref[idx]
# array([[3, 3], [4, 4]])
Run Code Online (Sandbox Code Playgroud)