Reputation: 63
I want to find the neighbors within a distance using Scipy cKDTree. Most important, I want the point itself (zero distance) as well. cKDTree. query gives all the neighbors but without zero distance.
Is there a way to do this?
Upvotes: 5
Views: 10766
Reputation: 5443
I can't really reproduce your issue (or it might depends on the method that you are using to query the tree).
Considering this simple snippet of code :
>>> 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)
Querying for the nearest neighbors at a distance of 2
around the point (4, 4)
with the method cKDTree.query_ball_point
can give something like :
>>> idx = tree.query_ball_point((4, 4), 2)
>>> points_ref[idx]
# array([[3, 3], [4, 4], [5, 4]])
Which returns the point with a distance of 0.
Querying for the n-nearest neighbors with the method cKDTree.query
also seems to return the point with a distance of 0 :
>>> _, idx = tree.query((3, 3), k=2)
>>> points_ref[idx]
# array([[3, 3], [4, 4]])
Upvotes: 7