Describe the bug
The CoverTree k-nearest neighbors query is not returning the k-nearest neighbors.
Expected behavior
After creating a CoverTree with 15 data points (length 2 double arrays) with euclidean distance, I submit a query for the 12 nearest neighbors to a single data point. I expect the 12 nearest neighbors to be returned.
Actual behavior
Instead, the 12 furthest neighbors are returned.
The above code returns neighbors that don't include the actual nearest neighbors to [4, 0] which are [4, 0], [4, -1], and [4, 2]. I believe this is because of the following lines of code in CoverTree.java:
Neighbors are sorted in increasing order of distance, then reversed, and then the first K elements are taken. If neighbors has greater than K elements, this returns the subset of neighbors with the K largest distances from the query point.
Describe the bug The CoverTree k-nearest neighbors query is not returning the k-nearest neighbors.
Expected behavior After creating a CoverTree with 15 data points (length 2 double arrays) with euclidean distance, I submit a query for the 12 nearest neighbors to a single data point. I expect the 12 nearest neighbors to be returned.
Actual behavior Instead, the 12 furthest neighbors are returned.
Input data
Code snippet
The above code returns neighbors that don't include the actual nearest neighbors to [4, 0] which are [4, 0], [4, -1], and [4, 2]. I believe this is because of the following lines of code in CoverTree.java:
(CoverTree.java lines 557-563)
Neighbors are sorted in increasing order of distance, then reversed, and then the first K elements are taken. If neighbors has greater than K elements, this returns the subset of neighbors with the K largest distances from the query point.
Additional context