建立並視覺化已調參的模型
在本章最後一個練習中,你會使用前一題 tune.svm() 找到的最佳參數,建立一個多項式 SVM。接著你會計算訓練與測試的正確率,並使用 svm.plot() 視覺化模型。e1071 套件已預先載入,測試與訓練資料集已提供於資料框 trainset 與 testset。tune.svm() 的輸出已存於變數 tune_out。
本練習屬於課程
R 的支援向量機
練習說明
- 建立一個使用 2 次多項式 kernel 的 SVM。
- 使用
tune.svm()計算出的最佳參數。 - 取得訓練與測試的正確率。
- 將決策邊界與訓練資料一同繪製出來。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
#Build tuned model
svm_model <- svm(y~ ., data = trainset, type = "C-classification",
kernel = ___, degree = ___,
cost = tune_out$___$cost,
gamma = tune_out$___$gamma,
coef0 = tune_out$___$coef0)
#Calculate training and test accuracies
pred_train <- predict(svm_model, ___)
mean(pred_train == ___$y)
pred_test <- predict(svm_model, ___)
mean(pred_test == ___$y)
#plot model
plot(svm_model, trainset)