[R] k-nearest neighbour classification
Neverstop
neverstop at hotmail.it
Tue Oct 13 10:59:44 CEST 2015
Hi, I'm trying to perform a cross validation to choose the optimal k in the
k-nearest-neighbors algorithm for classification. I'm using the knn
<http://stat.ethz.ch/R-manual/R-devel/library/class/html/knn.html>
function of the package class. Reading the R documentation, I've found out
that there's already a function to perform cross validation: knn.cv
<http://stat.ethz.ch/R-manual/R-devel/library/class/html/knn.cv.html> . The
problem is that I don't understand how I should use it.
data(iris)
head(iris)
predictors.training=iris[c(1:25,51:75,101:125),c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width")]
predictors.test=iris[c(26:50,76:100,126:150),c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width")]
classes.training=iris[c(1:25,51:75,101:125),"Species"]
library(class)
knn.cv(train=predictors.training, cl=classes.training, k=c(1,3,5,7),
prob=TRUE)
Warning messages:
1: In if (ntr - 1 < k) { :
the condition has length > 1 and only the first element will be used
2: In if (k < 1) stop(gettextf("k = %d must be at least 1", k), domain = NA)
:
the condition has length > 1 and only the first element will be used
Thank you.
--
View this message in context: http://r.789695.n4.nabble.com/k-nearest-neighbour-classification-tp4713523.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list