為何使用 RBF kernel

R 的支援向量機

Kailash Awati

Instructor

二次 kernel(預設參數)

  • 將資料分成訓練/測試(未顯示)
  • 使用二次(degree 2)多項式 kernel(預設參數)
svm_model <- svm(y ~ ., data = trainset, 
                type = "C-classification", 
                kernel = "polynomial", 
                degree = 2)
svm_model
....
Number of Support Vectors:  204
# Predictions
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
0.8666667
plot(svm_model, trainset)
R 的支援向量機

第 4.2 章-複雜資料集,預設二次 kernel,使用 svm.plot 繪圖

R 的支援向量機

嘗試更高次的多項式

  • 排除奇數次(3、5、9 等)
  • 嘗試 degree 4
svm_model <- svm(y ~ ., data = trainset, 
                type = "C-classification", 
                kernel = "polynomial", 
                degree = 4)
svm_model
...
Number of Support Vectors: 203
# Predictions
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
0.8583333
plot(svm_model, trainset)
R 的支援向量機

第 4.2 章-複雜資料集,預設 degree=4 kernel,使用 svm.plot 繪圖

R 的支援向量機

另一種思路

  • 「經驗法則」:彼此接近的點應有相同分類:
    • 類似 K-Nearest Neighbors 演算法。
  • 對於資料集中的一個點,設 X1 = (a, b):
    • kernel 應在 (a, b) 取最大值
    • 離開 (a, b) 後應逐漸衰減
    • 衰減速率在各方向相同
    • 衰減速率可調
  • 具此特性的簡單函式是 exp(-gamma * r),其中 r 為 X1 與任一點 X 的距離
R 的支援向量機

RBF kernel 隨 gamma 變化(程式碼)

#rbf function
rbf <- function(r, gamma) exp(-gamma * r)

ggplot(data.frame(r = c(-0, 10)), aes(r)) + stat_function(fun = rbf, args = list(gamma = 0.2), aes(color = "0.2")) + stat_function(fun = rbf, args = list(gamma = 0.4), aes(color = "0.4")) + stat_function(fun = rbf, args = list(gamma = 0.6), aes(color = "0.6")) + stat_function(fun = rbf, args = list(gamma = 0.8), aes(color = "0.8")) + stat_function(fun = rbf, args = list(gamma = 1), aes(color = "1")) + stat_function(fun = rbf, args = list(gamma = 2), aes(color = "2")) + scale_color_manual("gamma", values = c("red","orange","yellow", "green","blue","violet")) + ggtitle("Radial basis function (gamma = 0.2 to 2)")
R 的支援向量機

圖片說明

R 的支援向量機

一起來練習吧!

R 的支援向量機

Preparing Video For Download...