1

I need to get k nearest neighbors from distance matrix. Example: I have two "training" vectors "a" <- c(1,1) and "b" <- c(2,2) which are two dimensional vectors. I have to classify c(3,3) and I didn't have regular distance because numbers are codes for characteristics, and distance(2,3) > distance(1,3)...so c(3,3) has "a" for nearest neighbor. Later I have to generalize and output n nearest neighbors, but only for one vector at a time.

This was most promising at first, but when I looked into documentation for k.nearest.neighbors I realized it won't help me. I can't do this with Python's scikit-learn, but have some hope for R implementation, any suggestions? I need speed with this so if I'm going to implement it in high level language I need to do it with some library...I can easily code this up in Python's numpy, but will be almost certainly too slow.

EDIT:

library(FNN)
distance_matrix <- matrix( rep( 0, len=9), nrow = 3)
distance_matrix[1,3] <- 2
distance_matrix[3,1] <- 2
distance_matrix[2,3] <- 3
distance_matrix[3,2] <- 3
train <- rbind(c(1,1), c(2,2)
test <- rbind(c(3,3))
y <- c("one", "two")
fit <- knn(train, test, y, distance_matrix, k=1, prob=TRUE)
result <- data.frame(test, pred=fit, prob=attr(fit, "prob"))

But when I look at dataframe result I see result based on euclidian metric or something alike, not my distance matrix.

spartan
  • 41
  • 1
  • 4
  • Can you please make this a [reproducible question](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (SO help ref: https://stackoverflow.com/help/mcve)? It sounds like you're asking for a software recommendation, specifically [off-topic for SO](https://stackoverflow.com/help/on-topic). If you have specific code that is mis-behaving, then please include it and be clear why it is misbehaving. (And a small request: please place this reproducible code within a [preformatted code block](https://stackoverflow.com/editing-help#code).) – r2evans Dec 31 '17 at 17:53
  • Sorry, I didn't do it at first because I'm looking for correct package. – spartan Dec 31 '17 at 19:29
  • did you find anything? @spartan – liguang May 02 '19 at 13:28

0 Answers0