title: Custom distance matrix with KNN
body: <p>I need to get k nearest neighbors from distance matrix. Example:
  I have two "training" vectors <code>"a" &lt;- c(1,1)</code> and <code>"b" &lt;- c(2,2)</code> which are two dimensional vectors. I have to classify <code>c(3,3)</code> and I didn't have regular distance because numbers are codes for characteristics, and <code>distance(2,3) &gt; distance(1,3)</code>...so <code>c(3,3)</code> has <code>"a"</code> for nearest neighbor. Later I have to generalize and output n nearest neighbors, but only for one vector at a time.</p>
  
  <p><a href="https://stackoverflow.com/questions/23449726/find-k-nearest-neighbors-starting-from-a-distance-matrix">This</a> was most promising at first, but when I looked into <a href="https://cran.r-project.org/web/packages/FastKNN/FastKNN.pdf" rel="nofollow noreferrer">documentation</a> for <code>k.nearest.neighbors</code> 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.</p>
  
  <p>EDIT:</p>
  
  <pre><code>library(FNN)
  distance_matrix &lt;- matrix( rep( 0, len=9), nrow = 3)
  distance_matrix[1,3] &lt;- 2
  distance_matrix[3,1] &lt;- 2
  distance_matrix[2,3] &lt;- 3
  distance_matrix[3,2] &lt;- 3
  train &lt;- rbind(c(1,1), c(2,2)
  test &lt;- rbind(c(3,3))
  y &lt;- c("one", "two")
  fit &lt;- knn(train, test, y, distance_matrix, k=1, prob=TRUE)
  result &lt;- data.frame(test, pred=fit, prob=attr(fit, "prob"))
  </code></pre>
  
  <p>But when I look at dataframe <code>result</code> I see result based on euclidian metric or something alike, not my distance matrix.</p>