【问题标题】:How to query the nearest neighbors including the ones at distance zero with Scipy's cKDTree?如何使用 Scipy 的 cKDTree 查询最近的邻居,包括距离为零的邻居?
【发布时间】:2020-03-01 04:19:55
【问题描述】:

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

有没有办法做到这一点?

【问题讨论】:

  • 您能否提供有关您的代码以及如何查询树的更多详细信息?我不确定你的问题。如果您构建类似tree = cKDTree(ref_points) 的树并使用类似_, idx = tree.query(other_points, k=3) 的内容进行查询,则idx 变量将包含other_points 的每个点的ref_points 中最近的3 个ID,包括其中一个是否被索引点与查询点具有相同的坐标(即零距离)。
  • 感谢您的回复。在列表 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 中,我想在 2 的距离内找到点 [5] 的邻居。cKDTree 给我点 [3, 4, 6、7]。但我想要 [3, 4, 5, 6, 7]。这是否澄清了我的问题?

标签: python scipy


【解决方案1】:

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

考虑到这个简单的sn-p代码:

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

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

>>> idx = tree.query_ball_point((4, 4), 2)
>>> points_ref[idx]
# array([[3, 3], [4, 4], [5, 4]])

返回距离为0的点。

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

>>> _, idx = tree.query((3, 3), k=2)
>>> points_ref[idx]
# array([[3, 3], [4, 4]])

【讨论】:

    猜你喜欢
    • 2019-06-05
    • 2011-11-12
    • 2015-12-02
    • 2017-06-25
    • 2019-06-30
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多