简体   繁体   中英

how use cross validation to determine cost in multi-classification svm

HI I used the following for multi-classification in R. how I can choose the best cost with using cross-validation?

  n <- nrow(hd) 
  ntrain <- round(n*0.75)
  set.seed(314)
   tindex <- sample(n, ntrain)
   train_iris <- hd[tindex,]
   test_iris <- hd[-tindex,] 
   svm1 <- svm(res~., data=train_iris, 
        method="C-classification", kernal="radial", 
        gamma=0.1, cost=10)

e1071 has functions tune values of hyper-parameters to get better performance from the models. The example below is a demonstration on how to use the tune function in e1071 . It finds best the cost parameter from a given range (the cost = 1 give the best error rate of 0.333 )

hd <- iris
svm_tune <- tune(svm, Species~., data=hd ,kernel ="radial", 
              ranges = list(cost=c(0.001, 0.01,0.1, 1, 10, 100)))

summary(svm_tune)

# Parameter tuning of ‘svm’:
#   
#   - sampling method: 10-fold cross validation 
# 
# - best parameters:
#   cost
# 1
# 
# - best performance: 0.03333333 
# 
# - Detailed performance results:
#   cost      error dispersion
# 1 1e-03 0.74000000 0.15539674
# 2 1e-02 0.74000000 0.15539674
# 3 1e-01 0.10666667 0.07166451
# 4 1e+00 0.03333333 0.04714045
# 5 1e+01 0.04000000 0.05621827
# 6 1e+02 0.05333333 0.06126244

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM