KNN - Print the k nearest neighboors

Hi! I created a knn model to classify a group of cities into two classes (1 and 0). The model worked, but I would like to know who are the closest neighbors for each example that was classified. I don't know how to do this. It´s possible to print the k closest neighbors for each exemple? Below is the code I used to classify the cities in the test group.

#-> code_coment
keep <- condense(data_train[, -(1:2)], class = data_train[,2])

for(i in 1:10){
  set.seed(123456)
  previsoes[[i]]    <- knn(train = data_train[keep,-(1:2)], test= data_test[,-(1:2)],cl= data_train[keep,2],k=i) 
  perc.erro[i]      <- mean(data_teste[,2] != previsoes[[i]] ) 
}

R_code <- here

After this loop, i´m going to use k=2.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.