-1

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.

Community
  • 1
  • 1
rdm11
  • 5
  • 2
  • thanks, I hadn't seen that one. Though (and I'm new to R so I might have it wrong) I think that is what I'm doing. This line_n vector I add to the final result matrix that was declared first. And to get line_n I loop through the distance matrix (which I am never modifying) and process one row at a time. – rdm11 Apr 06 '16 at 09:37

1 Answers1

0

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
abhi
  • 1,412
  • 19
  • 25