Find distance to nearest neighbor in 2d array Find distance to nearest neighbor in 2d array numpy numpy

Find distance to nearest neighbor in 2d array


KDTree can do this. The process is almost the same as when using cdist. But cdist is much faster. And as pointed out in the comments, cKDTree is even faster:

import numpy as npfrom scipy.spatial.distance import cdistfrom scipy.spatial import KDTreefrom scipy.spatial import cKDTreeimport timeit# Random datadata = np.random.uniform(0., 1., (1000, 2))def scipy_method():    # Distance between the array and itself    dists = cdist(data, data)    # Sort by distances    dists.sort()    # Select the 1st distance, since the zero distance is always 0.    # (distance of a point with itself)    nn_dist = dists[:, 1]    return nn_distdef KDTree_method():    # You have to create the tree to use this method.    tree = KDTree(data)    # Then you find the closest two as the first is the point itself    dists = tree.query(data, 2)    nn_dist = dists[0][:, 1]    return nn_distdef cKDTree_method():    tree = cKDTree(data)    dists = tree.query(data, 2)    nn_dist = dists[0][:, 1]    return nn_distprint(timeit.timeit('cKDTree_method()', number=100, globals=globals()))print(timeit.timeit('scipy_method()', number=100, globals=globals()))print(timeit.timeit('KDTree_method()', number=100, globals=globals()))

Output:

0.349525076355575957.90408371519357920.765962179145546

Once again, then very unneeded proof that C is awesome!