Quantcast

knn - 10 fold cross validation

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

knn - 10 fold cross validation

Tim Smith-5
Hi,
   
  I was trying to get the optimal 'k' for the knn. To do this I was using the following function :
   
 
knn.cvk <- function(datmat, cl, k = 2:9) {
    datmatT <- (datmat)
  cv.err <- cl.pred <- c()
 
  for (i in k) {
    newpre <- as.vector(knn.cv(datmatT, cl, k = i))
    cl.pred <- cbind(cl.pred, newpre)
    cv.err <- c(cv.err, sum(cl != newpre))
   
  }
  k0 <- k[which.min(cv.err)]
  print(k0)
  return(k0)
}

   
  However, the knn.cv function does a 'leave one out' cross validation. I checked the documentation to see if I could change this, but it appears that I cannot. Since I have large datasets, I would like to do 10 fold cross validation, instead of the 'leave one out'.
   
  Is there some other function that I can use that will give me a 10 fold cross validation for KNN ?
   
  many thanks.

 __________________________________________________



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: knn - 10 fold cross validation

Liaw, Andy
You might want to check out the function tune.knn() in the e1071 package.
 
Andy

  _____  

From: [hidden email] on behalf of Tim Smith
Sent: Tue 6/6/2006 8:29 PM
To: [hidden email]
Subject: [R] knn - 10 fold cross validation [Broadcast]



Hi,
   
  I was trying to get the optimal 'k' for the knn. To do this I was using
the following function :
   
 
knn.cvk <- function(datmat, cl, k = 2:9) {
    datmatT <- (datmat)
  cv.err <- cl.pred <- c()
 
  for (i in k) {
    newpre <- as.vector(knn.cv(datmatT, cl, k = i))
    cl.pred <- cbind(cl.pred, newpre)
    cv.err <- c(cv.err, sum(cl != newpre))
   
  }
  k0 <- k[which.min(cv.err)]
  print(k0)
  return(k0)
}

   
  However, the knn.cv function does a 'leave one out' cross validation. I
checked the documentation to see if I could change this, but it appears that
I cannot. Since I have large datasets, I would like to do 10 fold cross
validation, instead of the 'leave one out'.

   
  Is there some other function that I can use that will give me a 10 fold
cross validation for KNN ?
   
  many thanks.

 __________________________________________________



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
<https://stat.ethz.ch/mailman/listinfo/r-help>  
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
<http://www.R-project.org/posting-guide.html>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: knn - 10 fold cross validation

Prof Brian Ripley
10-fold cross-validation is easily done at R level: there is generic code
in MASS, the book knn was written to support.

knn and lda have options for leave-one-out cross-validation just because
there are compuiationally efficient algorithms for those cases.

On Tue, 6 Jun 2006, Liaw, Andy wrote:

> You might want to check out the function tune.knn() in the e1071 package.
>
> Andy
>
>  _____
>
> From: [hidden email] on behalf of Tim Smith
> Sent: Tue 6/6/2006 8:29 PM
> To: [hidden email]
> Subject: [R] knn - 10 fold cross validation [Broadcast]
>
>
>
> Hi,
>
>  I was trying to get the optimal 'k' for the knn. To do this I was using
> the following function :
>
>
> knn.cvk <- function(datmat, cl, k = 2:9) {
>    datmatT <- (datmat)
>  cv.err <- cl.pred <- c()
>
>  for (i in k) {
>    newpre <- as.vector(knn.cv(datmatT, cl, k = i))
>    cl.pred <- cbind(cl.pred, newpre)
>    cv.err <- c(cv.err, sum(cl != newpre))
>
>  }
>  k0 <- k[which.min(cv.err)]
>  print(k0)
>  return(k0)
> }
>
>
>  However, the knn.cv function does a 'leave one out' cross validation. I
> checked the documentation to see if I could change this, but it appears that
> I cannot. Since I have large datasets, I would like to do 10 fold cross
> validation, instead of the 'leave one out'.
>
>
>  Is there some other function that I can use that will give me a 10 fold
> cross validation for KNN ?
>
>  many thanks.
>
> __________________________________________________
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> <https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
> <http://www.R-project.org/posting-guide.html>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Loading...