Search code examples
pythonmachine-learningscikit-learnknn

Print labels of the nearest neighbours from scikit KNeighborsClassifier?


I am using KNeighborsClassifier algorithm to train my data as below:

knn_clf = neighbors.KNeighborsClassifier(n_neighbors=3, 
algorithm="ball_tree", weights='distance')

I got the nearest 3 neighbours of the data from:

closest_distances = knn_clf.kneighbors(faces_encodings, n_neighbors=3)
print(closest_distances) #printouts: (array([[0.1123 , 0.29189484, 0.312]]

I want to link the distances of these 3 nearest neighbours with the labels. Any ideas ?

My training data: X:[0.1,0.2,0.3,0.4,0.5] y:[one, two, three, four, five]. If i give predict(0.2), how can i link the labels of the found distances as two, three, four ?

Thank you,


Solution

  • You should not assign knn_clf.kneighbors to a single variable closest_distances, as you do; kneighbors method returns two variables:

    Returns:

    dist : array

    Array representing the lengths to points, only present if return_distance=True

    ind : array

    Indices of the nearest points in the population matrix.

    So, you should give

    closest_distances, indices = knn_clf.kneighbors(faces_encodings, n_neighbors=3)
    

    and the indices variable will contain the required indices.