SVM 调参与优化

R 中的支持向量机

Kailash Awati

Instructor

调参目标

  • 复杂核函数下,手动找最优参数很难。
  • 目标:用 tune.svm() 寻找最优参数组合。
R 中的支持向量机

调参要点

  • 工作原理:
    • 为每个参数设定搜索范围。如:cost = 10^(-1:3),gamma = c(0.1,1,10),coef0 = c(0.1,1,10)
    • 针对所有参数组合训练 SVM,并评估准确率。
    • 返回准确率最高的参数组合。
  • 计算量大!
R 中的支持向量机
  • 为先前创建的径向可分数据集调优 SVM
    • 上一课使用了多项式核 SVM
    • 当时准确率约 94%。
  • 通过调优 gamma、cost 和 coef0 能否更好?
tune_out <- tune.svm(x = trainset[,-3], y = trainset[,3], 
                     type = "C-classification", kernel = "polynomial", degree = 2,
                     cost = 10^(-1:2), gamma = c(0.1,1,10), coef0 = c(0.1,1,10))

#print out tuned parameters tune_out$best.parameters$cost tune_out$best.parameters$gamma tune_out$best.parameters$coef0
0.1
10
1
R 中的支持向量机
  • 使用 tune.svm() 给出的最佳参数训练 SVM。
svm_model <- svm(y ~ ., data = trainset, type = "C-classification", kernel = "polynomial", degree = 2,
        cost = tune_out$best.parameters$cost,
        gamma = tune_out$best.parameters$gamma,
        coef0 = tune_out$best.parameters$coef0)
  • 评估训练集与测试集准确率
pred_train <- predict(svm_model, trainset)
mean(pred_train == trainset$y)
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
1

0.9677419
#plot using svm plot
plot(svm_model, trainset)
R 中的支持向量机

第 3.4 章——径向可分数据集,调优后的二次核,使用 svm.plot 作图

R 中的支持向量机

开始练习!

R 中的支持向量机

Preparing Video For Download...