Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

knn

k-Nearest Neighbour Classification


Description

k-nearest neighbour classification for test set from training set. For each row of the test set, the k nearest (in Euclidean distance) training set vectors are found, and the classification is decided by majority vote, with ties broken at random. If there are ties for the kth nearest vector, all candidates are included in the vote.

Usage

knn(train, test, cl, k = 1, prob = FALSE, algorithm=c("kd_tree", 
      "cover_tree", "brute"))

Arguments

train

matrix or data frame of training set cases.

test

matrix or data frame of test set cases. A vector will be interpreted as a row vector for a single case.

cl

factor of true classifications of training set.

k

number of neighbours considered.

prob

if this is true, the proportion of the votes for the winning class are returned as attribute prob.

algorithm

nearest neighbor search algorithm.

Value

factor of classifications of test set. doubt will be returned as NA.

Author(s)

Shengqiao Li. To report any bugs or suggestions please email: lishengqiao@yahoo.com.

References

B.D. Ripley (1996). Pattern Recognition and Neural Networks. Cambridge.

M.N. Venables and B.D. Ripley (2002). Modern Applied Statistics with S. Fourth edition. Springer.

See Also

ownn, knn.cv and knn in class.

Examples

data(iris3)
    train <- rbind(iris3[1:25,,1], iris3[1:25,,2], iris3[1:25,,3])
    test <- rbind(iris3[26:50,,1], iris3[26:50,,2], iris3[26:50,,3])
    cl <- factor(c(rep("s",25), rep("c",25), rep("v",25)))
    knn(train, test, cl, k = 3, prob=TRUE)
    attributes(.Last.value)

FNN

Fast Nearest Neighbor Search Algorithms and Applications

v1.1.3
GPL (>= 2)
Authors
Alina Beygelzimer, Sham Kakadet and John Langford (cover tree library), Sunil Arya and David Mount (ANN library 1.1.2 for the kd-tree approach), Shengqiao Li
Initial release
2019-02-15

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.