K nearest neighbor (KNN) search
This is a wrapper for several k nearest neighbors (KNNs)
algorithms in R. Currently wrapped functions are from the
FNN
, RANN
, and yaImpute
package.
It searches for KNN in a N \times d data matrix
data
where N are the number of samples, and
d is the dimension of space.
Either knn search in itself query=NULL
or to query
new data points wrt to training dataset.
search_knn(data, k = 1, query = NULL, method = c("FNN", "RANN", "yaImpute"), ...)
data |
an N \times d matrix, where N are the samples and d is the dimension of space. For large d knn search can be very slow. |
k |
number of nearest neighbors (excluding point
itself). Default: |
query |
(optional) an \tilde{N} \times d
matrix to find KNN in the training data for. Must have
the same d as |
method |
what method should be used: |
... |
other parameters passed to the knn functions in each package. |
Packages FNN, RANN, and yaImpute for
other options (...
).
set.seed(1984) XX <- matrix(rnorm(40), ncol = 2) YY <- matrix(runif(length(XX) * 2), ncol = ncol(XX)) knns_of_XX_in_XX <- search_knn(XX, 1) knns_of_YY_in_XX <- search_knn(XX, 1, query = YY) plot(rbind(XX, YY), type = "n", xlab = "", ylab = "") points(XX, pch = 19, cex = 2, xlab = "", ylab = "") arrows(XX[, 1], XX[, 2], XX[knns_of_XX_in_XX, 1], XX[knns_of_XX_in_XX, 2], lwd = 2) points(YY, pch = 15, col = 2) arrows(YY[, 1], YY[, 2], XX[knns_of_YY_in_XX, 1], XX[knns_of_YY_in_XX, 2], col = 2) legend("left", c("X", "Y"), lty = 1, pch = c(19, 15), cex = c(2, 1), col = c(1, 2))
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.