K nearest neighbor (KNN) search

Share:

Description

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.

Usage

1
search_knn(data, k = 1, query = NULL, method = c("FNN", "RANN", "yaImpute"), ...)

Arguments

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: k=1.

query

(optional) an \tilde{N} \times d matrix to find KNN in the training data for. Must have the same d as data; can have lower or larger \tilde{N} though. Default: query=NULL meaning that nearest neighbors should be looked for in the training data itself.

method

what method should be used: 'FNN', 'RANN', or 'yaImpute'.

...

other parameters passed to the knn functions in each package.

See Also

Packages FNN, RANN, and yaImpute for other options (...).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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))

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.