Fast fuse of close points in a numpy-2d (vectorized)
If you have a large number of points then it may be faster to build a k-D tree using scipy.spatial.cKDTree
, then query it for pairs of points that are closer than some threshold:
import numpy as np
from scipy.spatial import cKDTree
tree = cKDTree(points)
rows_to_fuse = tree.query_pairs(r=30)
print(repr(rows_to_fuse))
# {(8, 9)}
print(repr(points[list(rows_to_fuse)]))
# array([[ 820.57417943, 84.27702407],
# [ 806.71416007, 108.50307828]])
The major advantage of this approach is that you don't need to compute the distance between every pair of points in your dataset.