nearest neighbour search kdTree

前端 未结 4 803
礼貌的吻别
礼貌的吻别 2021-01-20 17:51

To a list of N points [(x_1,y_1), (x_2,y_2), ... ] I am trying to find the nearest neighbours to each point based on distance. My dataset is too la

4条回答
  •  无人共我
    2021-01-20 18:26

    You can use sklearn.neighbors.KDTree's query_radius() method, which returns a list of the indices of the nearest neighbours within some radius (as opposed to returning k nearest neighbours).

    from sklearn.neighbors import KDTree
    
    points = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
    
    tree = KDTree(points, leaf_size=2)
    all_nn_indices = tree.query_radius(points, r=1.5)  # NNs within distance of 1.5 of point
    all_nns = [[points[idx] for idx in nn_indices] for nn_indices in all_nn_indices]
    for nns in all_nns:
        print(nns)
    

    Outputs:

    [(1, 1), (2, 2)]
    [(1, 1), (2, 2), (3, 3)]
    [(2, 2), (3, 3), (4, 4)]
    [(3, 3), (4, 4), (5, 5)]
    [(4, 4), (5, 5)]
    

    Note that each point includes itself in its list of nearest neighbours within the given radius. If you want to remove these identity points, the line computing all_nns can be changed to:

    all_nns = [
        [points[idx] for idx in nn_indices if idx != i]
        for i, nn_indices in enumerate(all_nn_indices)
    ]
    

    Resulting in:

    [(2, 2)]
    [(1, 1), (3, 3)]
    [(2, 2), (4, 4)]
    [(3, 3), (5, 5)]
    [(4, 4)]
    

提交回复
热议问题