in python, the knn algorithm can be applied on training data with the code like below:
from sklearn.neighbors import KNeighborsClassifier
KNN_classifier = KNeighborsClassifier(n_neighbors=k)
KNN_classifier.fit(X_train, y_train)
y_pred = KNN_classifier.predict(X_test)
now for a given data point $x$, I want to find exactly the $k$ neighbors of $x$ that knn algorithm has used in learning process. is there any access to neighbors of a given data point $x$ at all? I’d appreciate it if anyone can help me with this issue.
1
You can use this function, https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html#sklearn.neighbors.KNeighborsClassifier.kneighbors, to get the (indices of) the nearest neighbors.