0

When you look at the result of a sklearn.neighbors.NearestNeighbors query, the distances will always be in ascending order, and the indices are in the order "nearest neighbor", "second nearest neighbor", etc.

But the sklearn documentation never states the above.

My question would be: is that a deliberate omission, or is it just so standard that the result would be ordered that it does not require mentioning it?

Consider this example code, adapted from 53886289:

from sklearn.neighbors import NearestNeighbors
import numpy as np
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(X)
X_query = np.array([[0, 1]])
distances, indices = nbrs.kneighbors(X_query)

Output:

distances -> array([[1., 2.]])

indices -> array([[3, 4]])
ernstkl
  • 105
  • 3

0 Answers0