調整 SVM

R 的支援向量機

Kailash Awati

Instructor

調參目標

  • 對複雜 kernel,手動找最佳參數很難。
  • 目標:用 tune.svm() 找出最佳參數組合。
R 的支援向量機

調參重點

  • 運作方式:
    • 為每個參數設定搜尋範圍。例如:cost = 10^(-1:3)gamma = c(0.1,1,10)coef0 = c(0.1,1,10)
    • 對每種參數組合建 SVM 並評估準確率。
    • 回傳準確率最高的參數組合。
  • 計算量很大!
R 的支援向量機
  • 對先前建立的徑向可分資料集調參 SVM
    • 上一節用了 polynomial kernel 的 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 章-徑向可分資料集,調參後二次 kernel,使用 svm.plot 繪圖

R 的支援向量機

一起來練習吧!

R 的支援向量機

Preparing Video For Download...