简体   繁体   中英

KNN finding the top N neighbors

Note: origininally posted in Cross Validated but recommendations said it would be a better fit topic wise here.

I am using the knn method in the FNN package for classification but I would like to see the N closest neighbors as opposed to only the top most one. I have tried with different packages (FastKNN and knncat for example) but I can't find a fast function that will do this for you.

This is a similar question (minus the part of the distance matrix): Find K nearest neighbors, starting from a distance matrix

This is what I tried: LINE contains one row of the distance matrix, LINE_N contains the top N neighbors for each prediction

line_n = c()

  tmp_min <- order(line)[1:ncol(distance)]
  tmp_id <- c()

  for (element in tmp_min)
    tmp_id <- c(tmp_id, colnames(distance)[element])

  for (element in tmp_id){

      if (!(element %in% line_n))
        line_n<- c(line_n, element)
      if (length(line_n) == N)
        break
  }

  line_n

I was wondering if there was an optimized version of it implemented already or if anyone had any ideas on how to make it faster.

Here is the code for what you are looking for:

line_ret = c()
while(length(line_ret) < M)
{
  tmp = which.min(line)
  if (!(col_name[tmp] %in% line_ret))
  line_ret = c(line_ret, col_name[tmp])
  line <- line[-c(tmp)]
  col_name <- col_name[-c(tmp)]
}
line_ret

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM