getknn {rchemo} | R Documentation |
KNN selection
Description
Function getknn
selects the k
nearest neighbours of each row observation of a new data set (= query) within a training data set, based on a dissimilarity measure.
getknn
uses function get.knnx
of package FNN
(Beygelzimer et al.) available on CRAN.
Usage
getknn(Xtrain, X, k = NULL, diss = c("eucl", "mahal"),
algorithm = "brute", list = TRUE)
Arguments
Xtrain |
Training X-data ( |
X |
New X-data ( |
k |
The number of nearest neighbors to select in |
diss |
The type of dissimilarity used. Possible values are "eucl" (default; Euclidean distance) or "mahal" (Mahalanobis distance). |
algorithm |
Search algorithm used for Euclidean and Mahalanobis distances. Default to |
list |
If |
Value
A list of outputs, such as:
nn |
A dataframe ( |
d |
A dataframe ( |
listnn |
Same as |
listd |
Same as |
Examples
n <- 10
p <- 4
X <- matrix(rnorm(n * p), ncol = p)
Xtrain <- X
Xtest <- X[c(1, 3), ]
m <- nrow(Xtest)
k <- 3
getknn(Xtrain, Xtest, k = k)
fm <- pcasvd(Xtrain, nlv = 2)
Ttrain <- fm$T
Ttest <- transform(fm, Xtest)
getknn(Ttrain, Ttest, k = k, diss = "mahal")