[R] k-nearest neighbour classification

Adams, Jean jvadams at usgs.gov
Tue Oct 13 14:54:27 CEST 2015


The argument k should be a scalar, not a vector.  So, for example, this
works:

knn.cv(train=predictors.training, cl=classes.training, k=3, prob=TRUE)

Jean


On Tue, Oct 13, 2015 at 3:59 AM, Neverstop <neverstop at hotmail.it> wrote:

> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list