RBF 核函數

R 的支援向量機

Kailash Awati

Instructor

RBF 核函數一覽

  • 隨兩點距離增加而遞減的函數。
  • 模擬 k-NN 演算法。
R 的支援向量機

第 4.3 章-RBF 核函數-影響力隨點距的函數

R 的支援向量機

使用 RBF 核建立 SVM

  • 為複雜資料集建立 RBF 核的 SVM
svm_model <- svm(y ~ ., 
                data = trainset, 
                type = "C-classification",
                kernel = "radial")
  • 計算訓練/測試正確率,並對訓練資料集作圖。
pred_train <- predict(svm_model, trainset)
mean(pred_train == trainset$y)
0.93125
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
0.9416667
# 繪製決策邊界
plot(svm_model, trainset)
R 的支援向量機

第 4.3 章-複雜資料集、預設 RBF 核函數,使用 svm.plot 繪圖

R 的支援向量機

優化決策邊界

  • 使用 tune.svm() 微調 gammacost
# Tune parameters
tune_out <- tune.svm(x = trainset[,-3],
                     y = trainset[,3],
                     gamma = 5 * 10 ^ (-2:2),
                     cost = c(0.01, 0.1, 1, 10, 100),
                     type = "C-classification",
                     kernel = "radial")
  • 輸出最佳參數
# Print best values of cost and gamma
tune_out$best.parameters$cost
1
tune_out$best.parameters$gamma
5
R 的支援向量機

調參後的模型

  • 使用 best.parameters 建立調參後模型
svm_model <- svm(y ~ ., data = trainset, 
              type = "C-classification", 
              kernel = "radial",
              cost = tune_out$best.parameters$cost,
              gamma = tune_out$best.parameters$gamma)
  • 計算測試正確率
mean(pred_test == testset$y)
0.95
  • 繪製決策邊界
plot(svm_model, trainset)
R 的支援向量機

第 4.3 章-複雜資料集、調參後 RBF 核函數,使用 svm.plot 繪圖

R 的支援向量機

一起來練習吧!

R 的支援向量機

Preparing Video For Download...