Last data update: 2014.03.03
R: k-Nearest Neighbour Classification
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
k
th 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: shli@stat.wvu.edu.
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)
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(FNN)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/FNN/knn.Rd_%03d_medium.png", width=480, height=480)
> ### Name: knn
> ### Title: k-Nearest Neighbour Classification
> ### Aliases: knn
> ### Keywords: classif nonparametric
>
> ### ** 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)
[1] s s s s s s s s s s s s s s s s s s s s s s s s s c c v c c c c c v c c c c
[39] c c c c c c c c c c c c v c c v v v v v c v v v v c v v v v v v v v v v v
attr(,"prob")
[1] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[8] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[15] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[22] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 0.6666667
[29] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 0.6666667 1.0000000
[36] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[43] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[50] 1.0000000 1.0000000 0.6666667 0.6666667 1.0000000 1.0000000 1.0000000
[57] 1.0000000 1.0000000 0.6666667 1.0000000 1.0000000 1.0000000 1.0000000
[64] 0.6666667 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[71] 1.0000000 0.6666667 1.0000000 1.0000000 0.6666667
attr(,"nn.index")
[,1] [,2] [,3]
[1,] 10 2 13
[2,] 24 8 18
[3,] 1 18 8
[4,] 1 18 8
[5,] 4 12 10
[6,] 10 4 13
[7,] 21 18 11
[8,] 20 11 17
[9,] 16 17 15
[10,] 10 2 13
[11,] 2 3 10
[12,] 11 1 21
[13,] 5 1 8
[14,] 9 14 4
[15,] 8 1 18
[16,] 18 1 5
[17,] 9 14 13
[18,] 4 3 7
[19,] 24 22 18
[20,] 6 22 20
[21,] 2 13 3
[22,] 20 22 5
[23,] 4 3 7
[24,] 11 20 22
[25,] 8 1 18
[26,] 41 34 50
[27,] 34 28 30
[28,] 28 61 74
[29,] 39 37 31
[30,] 45 40 43
[31,] 45 29 35
[32,] 45 29 43
[33,] 43 45 47
[34,] 52 74 48
[35,] 42 31 37
[36,] 32 46 27
[37,] 28 41 34
[38,] 44 48 38
[39,] 37 42 31
[40,] 29 45 35
[41,] 31 43 29
[42,] 39 49 27
[43,] 43 45 47
[44,] 33 36 35
[45,] 31 43 45
[46,] 37 31 43
[47,] 37 31 43
[48,] 50 47 37
[49,] 33 36 40
[50,] 43 37 31
[51,] 53 58 71
[52,] 74 39 48
[53,] 46 74 32
[54,] 55 54 62
[55,] 53 58 63
[56,] 58 53 56
[57,] 68 56 60
[58,] 55 54 62
[59,] 48 74 39
[60,] 54 62 67
[61,] 56 53 58
[62,] 66 51 75
[63,] 67 54 62
[64,] 46 74 39
[65,] 63 71 75
[66,] 71 63 55
[67,] 63 61 66
[68,] 52 64 72
[69,] 71 75 55
[70,] 71 75 55
[71,] 63 66 61
[72,] 74 62 48
[73,] 61 62 67
[74,] 66 61 75
[75,] 52 46 72
attr(,"nn.dist")
[,1] [,2] [,3]
[1,] 0.2000000 0.2236068 0.3000000
[2,] 0.2000000 0.2236068 0.2645751
[3,] 0.1414214 0.1732051 0.2236068
[4,] 0.1414214 0.1732051 0.2236068
[5,] 0.1732051 0.2236068 0.2645751
[6,] 0.1732051 0.2236068 0.2449490
[7,] 0.2828427 0.3464102 0.3605551
[8,] 0.3741657 0.4582576 0.4582576
[9,] 0.3605551 0.3872983 0.4123106
[10,] 0.1000000 0.1414214 0.2000000
[11,] 0.3000000 0.3162278 0.3464102
[12,] 0.3000000 0.4123106 0.4242641
[13,] 0.1414214 0.2449490 0.2645751
[14,] 0.1414214 0.2449490 0.3000000
[15,] 0.1000000 0.1414214 0.1732051
[16,] 0.1414214 0.1732051 0.1732051
[17,] 0.6244998 0.7810250 0.7937254
[18,] 0.3000000 0.3000000 0.3162278
[19,] 0.2645751 0.3162278 0.3741657
[20,] 0.3741657 0.4123106 0.4123106
[21,] 0.1414214 0.2000000 0.2645751
[22,] 0.1414214 0.2449490 0.3000000
[23,] 0.1414214 0.1414214 0.2236068
[24,] 0.1000000 0.2449490 0.2828427
[25,] 0.1414214 0.2236068 0.2449490
[26,] 0.1414214 0.2449490 0.2645751
[27,] 0.3162278 0.3464102 0.3741657
[28,] 0.3162278 0.4242641 0.5196152
[29,] 0.2449490 0.3316625 0.3741657
[30,] 0.4358899 0.4472136 0.6164414
[31,] 0.1732051 0.3000000 0.5291503
[32,] 0.2645751 0.4358899 0.5830952
[33,] 0.2828427 0.3000000 0.3464102
[34,] 0.3605551 0.4123106 0.4242641
[35,] 0.2000000 0.4123106 0.5830952
[36,] 0.3741657 0.4242641 0.4582576
[37,] 0.2828427 0.3162278 0.3162278
[38,] 0.2645751 0.5744563 0.5916080
[39,] 0.3741657 0.4472136 0.4582576
[40,] 0.2000000 0.2449490 0.3872983
[41,] 0.3162278 0.4795832 0.5099020
[42,] 0.1414214 0.3000000 0.3872983
[43,] 0.2449490 0.2645751 0.3741657
[44,] 0.1414214 0.3605551 0.8485281
[45,] 0.3316625 0.3741657 0.4123106
[46,] 0.3605551 0.3741657 0.3872983
[47,] 0.3000000 0.3162278 0.3872983
[48,] 0.2000000 0.3316625 0.3872983
[49,] 0.3872983 0.7211103 0.9000000
[50,] 0.3316625 0.3605551 0.4000000
[51,] 0.3872983 0.4358899 0.6557439
[52,] 0.1732051 0.4358899 0.4472136
[53,] 0.3000000 0.3605551 0.4582576
[54,] 0.3162278 0.3316625 0.3741657
[55,] 0.5196152 0.5567764 0.7071068
[56,] 0.2645751 0.4582576 0.6082763
[57,] 0.4123106 0.8831761 0.9327379
[58,] 0.3000000 0.4242641 0.4358899
[59,] 0.3605551 0.3741657 0.4690416
[60,] 0.5385165 0.6633250 0.7000000
[61,] 0.5477226 0.6633250 0.6782330
[62,] 0.3872983 0.4242641 0.5196152
[63,] 0.1414214 0.2449490 0.4582576
[64,] 0.2236068 0.4358899 0.4358899
[65,] 0.1732051 0.3741657 0.4123106
[66,] 0.2645751 0.3464102 0.3605551
[67,] 0.4690416 0.5099020 0.5477226
[68,] 0.0000000 0.2645751 0.3162278
[69,] 0.2236068 0.3162278 0.3872983
[70,] 0.3000000 0.4000000 0.4795832
[71,] 0.3741657 0.3741657 0.4242641
[72,] 0.2449490 0.3741657 0.4123106
[73,] 0.2236068 0.3464102 0.3605551
[74,] 0.3000000 0.5567764 0.6244998
[75,] 0.3316625 0.3605551 0.4582576
Levels: c s v
> attributes(.Last.value)
$levels
[1] "c" "s" "v"
$class
[1] "factor"
$prob
[1] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[8] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[15] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[22] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 0.6666667
[29] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 0.6666667 1.0000000
[36] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[43] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[50] 1.0000000 1.0000000 0.6666667 0.6666667 1.0000000 1.0000000 1.0000000
[57] 1.0000000 1.0000000 0.6666667 1.0000000 1.0000000 1.0000000 1.0000000
[64] 0.6666667 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[71] 1.0000000 0.6666667 1.0000000 1.0000000 0.6666667
$nn.index
[,1] [,2] [,3]
[1,] 10 2 13
[2,] 24 8 18
[3,] 1 18 8
[4,] 1 18 8
[5,] 4 12 10
[6,] 10 4 13
[7,] 21 18 11
[8,] 20 11 17
[9,] 16 17 15
[10,] 10 2 13
[11,] 2 3 10
[12,] 11 1 21
[13,] 5 1 8
[14,] 9 14 4
[15,] 8 1 18
[16,] 18 1 5
[17,] 9 14 13
[18,] 4 3 7
[19,] 24 22 18
[20,] 6 22 20
[21,] 2 13 3
[22,] 20 22 5
[23,] 4 3 7
[24,] 11 20 22
[25,] 8 1 18
[26,] 41 34 50
[27,] 34 28 30
[28,] 28 61 74
[29,] 39 37 31
[30,] 45 40 43
[31,] 45 29 35
[32,] 45 29 43
[33,] 43 45 47
[34,] 52 74 48
[35,] 42 31 37
[36,] 32 46 27
[37,] 28 41 34
[38,] 44 48 38
[39,] 37 42 31
[40,] 29 45 35
[41,] 31 43 29
[42,] 39 49 27
[43,] 43 45 47
[44,] 33 36 35
[45,] 31 43 45
[46,] 37 31 43
[47,] 37 31 43
[48,] 50 47 37
[49,] 33 36 40
[50,] 43 37 31
[51,] 53 58 71
[52,] 74 39 48
[53,] 46 74 32
[54,] 55 54 62
[55,] 53 58 63
[56,] 58 53 56
[57,] 68 56 60
[58,] 55 54 62
[59,] 48 74 39
[60,] 54 62 67
[61,] 56 53 58
[62,] 66 51 75
[63,] 67 54 62
[64,] 46 74 39
[65,] 63 71 75
[66,] 71 63 55
[67,] 63 61 66
[68,] 52 64 72
[69,] 71 75 55
[70,] 71 75 55
[71,] 63 66 61
[72,] 74 62 48
[73,] 61 62 67
[74,] 66 61 75
[75,] 52 46 72
$nn.dist
[,1] [,2] [,3]
[1,] 0.2000000 0.2236068 0.3000000
[2,] 0.2000000 0.2236068 0.2645751
[3,] 0.1414214 0.1732051 0.2236068
[4,] 0.1414214 0.1732051 0.2236068
[5,] 0.1732051 0.2236068 0.2645751
[6,] 0.1732051 0.2236068 0.2449490
[7,] 0.2828427 0.3464102 0.3605551
[8,] 0.3741657 0.4582576 0.4582576
[9,] 0.3605551 0.3872983 0.4123106
[10,] 0.1000000 0.1414214 0.2000000
[11,] 0.3000000 0.3162278 0.3464102
[12,] 0.3000000 0.4123106 0.4242641
[13,] 0.1414214 0.2449490 0.2645751
[14,] 0.1414214 0.2449490 0.3000000
[15,] 0.1000000 0.1414214 0.1732051
[16,] 0.1414214 0.1732051 0.1732051
[17,] 0.6244998 0.7810250 0.7937254
[18,] 0.3000000 0.3000000 0.3162278
[19,] 0.2645751 0.3162278 0.3741657
[20,] 0.3741657 0.4123106 0.4123106
[21,] 0.1414214 0.2000000 0.2645751
[22,] 0.1414214 0.2449490 0.3000000
[23,] 0.1414214 0.1414214 0.2236068
[24,] 0.1000000 0.2449490 0.2828427
[25,] 0.1414214 0.2236068 0.2449490
[26,] 0.1414214 0.2449490 0.2645751
[27,] 0.3162278 0.3464102 0.3741657
[28,] 0.3162278 0.4242641 0.5196152
[29,] 0.2449490 0.3316625 0.3741657
[30,] 0.4358899 0.4472136 0.6164414
[31,] 0.1732051 0.3000000 0.5291503
[32,] 0.2645751 0.4358899 0.5830952
[33,] 0.2828427 0.3000000 0.3464102
[34,] 0.3605551 0.4123106 0.4242641
[35,] 0.2000000 0.4123106 0.5830952
[36,] 0.3741657 0.4242641 0.4582576
[37,] 0.2828427 0.3162278 0.3162278
[38,] 0.2645751 0.5744563 0.5916080
[39,] 0.3741657 0.4472136 0.4582576
[40,] 0.2000000 0.2449490 0.3872983
[41,] 0.3162278 0.4795832 0.5099020
[42,] 0.1414214 0.3000000 0.3872983
[43,] 0.2449490 0.2645751 0.3741657
[44,] 0.1414214 0.3605551 0.8485281
[45,] 0.3316625 0.3741657 0.4123106
[46,] 0.3605551 0.3741657 0.3872983
[47,] 0.3000000 0.3162278 0.3872983
[48,] 0.2000000 0.3316625 0.3872983
[49,] 0.3872983 0.7211103 0.9000000
[50,] 0.3316625 0.3605551 0.4000000
[51,] 0.3872983 0.4358899 0.6557439
[52,] 0.1732051 0.4358899 0.4472136
[53,] 0.3000000 0.3605551 0.4582576
[54,] 0.3162278 0.3316625 0.3741657
[55,] 0.5196152 0.5567764 0.7071068
[56,] 0.2645751 0.4582576 0.6082763
[57,] 0.4123106 0.8831761 0.9327379
[58,] 0.3000000 0.4242641 0.4358899
[59,] 0.3605551 0.3741657 0.4690416
[60,] 0.5385165 0.6633250 0.7000000
[61,] 0.5477226 0.6633250 0.6782330
[62,] 0.3872983 0.4242641 0.5196152
[63,] 0.1414214 0.2449490 0.4582576
[64,] 0.2236068 0.4358899 0.4358899
[65,] 0.1732051 0.3741657 0.4123106
[66,] 0.2645751 0.3464102 0.3605551
[67,] 0.4690416 0.5099020 0.5477226
[68,] 0.0000000 0.2645751 0.3162278
[69,] 0.2236068 0.3162278 0.3872983
[70,] 0.3000000 0.4000000 0.4795832
[71,] 0.3741657 0.3741657 0.4242641
[72,] 0.2449490 0.3741657 0.4123106
[73,] 0.2236068 0.3464102 0.3605551
[74,] 0.3000000 0.5567764 0.6244998
[75,] 0.3316625 0.3605551 0.4582576
>
>
>
>
>
> dev.off()
null device
1
>