Skip to content

Instantly share code, notes, and snippets.

@yonestra
Created January 6, 2012 20:43
Show Gist options
  • Save yonestra/1572327 to your computer and use it in GitHub Desktop.
Save yonestra/1572327 to your computer and use it in GitHub Desktop.
cross validation
library(mlbench)
library(kernlab)
n <- 200 #データ数
k <- 10 #分割数
dat <- mlbench.spirals(n, cycles=1.2, sd=0.16) #データ生成
x <- dat$x; y <- dat$classes
idx <- sample(k, n, replace=TRUE)
sigma.list <- seq(0.1, 15, by=0.1)
cv <- c()
for(sigma in sigma.list) {
err_estimate <- c()
for(j in 1:k) {
tmpx <- x[idx!=j,]; tmpy <- y[idx!=j]
rbfsvm<-ksvm(tmpx,tmpy,type="C-svc",kernel="rbfdot",kpar=list(sigma=sigma))
rbfpred <- predict(rbfsvm,x[idx==j,])
err_estimate <- c(err_estimate,mean(y[idx==j]!=rbfpred))
}
cv <- c(cv, mean(err_estimate))
}
sigma.list[which.min(cv)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment