Re: [R] k-nearest neighbour classification

2015-10-13 Thread Adams, Jean
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  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
> 
> 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
>   .
> 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@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]]

__
R-help@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.


Re: [R] k-nearest neighbour classification

2015-10-13 Thread Neverstop
I know that knn.cv(train=predictors.training, cl=classes.training, k=3,
prob=TRUE) works but by doing so I fix the tuning paramer k to be 3. Isn't
cross validation a technique to choose the optimal tuning parameter trying a
range of different values for the tuning parameter? 



--
View this message in context: 
http://r.789695.n4.nabble.com/k-nearest-neighbour-classification-tp4713523p4713531.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] k-nearest neighbour classification

2015-10-13 Thread Adams, Jean
Ah, I see what you're after.  I don't know of a built in function to search
for the best number of nearest neighbors.  You may have to run the code for
each k separately, then compare the resulting errors.

Jean

On Tue, Oct 13, 2015 at 9:13 AM, Neverstop  wrote:

> I know that knn.cv(train=predictors.training, cl=classes.training, k=3,
> prob=TRUE) works but by doing so I fix the tuning paramer k to be 3. Isn't
> cross validation a technique to choose the optimal tuning parameter trying
> a
> range of different values for the tuning parameter?
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/k-nearest-neighbour-classification-tp4713523p4713531.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@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]]

__
R-help@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.