最大的点集,使所有点之间的距离最小

时间:2020-04-15 17:49:32

标签: python spatial kdtree

我有一组x和y坐标,并希望为其构造最大的一组 dist(x_i, x_j) >= limit代表每对点。由于集合相当大,比二次缩放更好。

作为对该集合的第一个近似值,我使用了Kdtree结构的范围搜索例程,通过该例程可以从原始集合中连续删除点。

import numpy as np
from scipy.special import cKDTree

def remove_pairs(xarr, yarr, max_sep):
    """
    Removes points which have neighbours closer than some distance.
    """
    npoints = len(xarr)
    tree = cKDTree(np.c_[xarr.ravel(), yarr.ravel()])
    tokeep = [True]*npoints
    for point in range(npoints):
        too_close = np.asarray(tree.query_ball_point([xarr[point], yarr[point]], max_sep))
        # Distance of two points symmetric --> remove points with lower indices
        if len(too_close[too_close>point]) > 0:
            tokeep[point] = False
    return np.asarray(tokeep)

尽管此函数给出的结果是正确的(意味着所有的点将被分开一个最小的距离),但是却不会产生最大的可能集合。在我的实现中,导致失败的原因是默认情况下,一旦排除范围内还有其他点,则总是删除点。

您是否知道一种在不增加很多运行时间的情况下达到(或至少接近)最大集合的方法?

0 个答案:

没有答案