Last data update: 2014.03.03

R: K nearest neighbor (KNN) search
search_knnR Documentation

K nearest neighbor (KNN) search

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

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

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))

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(LICORS)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/LICORS/search_knn.Rd_%03d_medium.png", width=480, height=480)
> ### Name: search_knn
> ### Title: K nearest neighbor (KNN) search
> ### Aliases: search_knn
> ### Keywords: classif cluster nonparametric
> 
> ### ** Examples
> 
> 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))
Warning message:
In if (xc < 0) text.width <- -text.width :
  the condition has length > 1 and only the first element will be used
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>