Find K nearest neighbors, starting from a distance matrix

后端 未结 2 1989
温柔的废话
温柔的废话 2020-11-28 14:36

I\'m looking for a well-optimized function that accepts an n X n distance matrix and returns an n X k matrix with the indices of the k

相关标签:
2条回答
  • 2020-11-28 15:00

    For the record (I won't mark this as the answer), here is a quick-and-dirty solution. Suppose sd.dist is the special distance matrix. Suppose k.for.nn is the number of nearest neighbors.

    n = nrow(sd.dist)
    knn.mat = matrix(0, ncol = k.for.nn, nrow = n)
    knd.mat = knn.mat
    for(i in 1:n){
      knn.mat[i,] = order(sd.dist[i,])[1:k.for.nn]
      knd.mat[i,] = sd.dist[i,knn.mat[i,]]
    }
    

    Now knn.mat is the matrix with the indices of the k nearest neighbors in each row, and for convenience knd.mat stores the corresponding distances.

    0 讨论(0)
  • 2020-11-28 15:09

    Try to use FastKNN CRAN package (although it is not well documented). It offers k.nearest.neighbors function where an arbitrary distance matrix can be given. Below you have an example that computes the matrix you need.

    # arbitrary data
    train <- matrix(sample(c("a","b","c"),12,replace=TRUE), ncol=2) # n x 2
    n = dim(train)[1]
    distMatrix <- matrix(runif(n^2,0,1),ncol=n) # n x n
    
    # matrix of neighbours
    k=3
    nn = matrix(0,n,k) # n x k
    for (i in 1:n)
       nn[i,] = k.nearest.neighbors(i, distMatrix, k = k)
    

    Notice: You can always check Cran packages list for Ctrl+F='knn' related functions: https://cran.r-project.org/web/packages/available_packages_by_name.html

    0 讨论(0)
提交回复
热议问题