-1

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)
kangaroo_cliff
  • 6,067
  • 3
  • 29
  • 42
sherek_66
  • 501
  • 5
  • 14

1 Answers1

2

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
kangaroo_cliff
  • 6,067
  • 3
  • 29
  • 42