RBF 核的动机

R 中的支持向量机

Kailash Awati

Instructor

二次核(默认参数)

  • 划分训练/测试集(未展示)
  • 使用二次多项式核(默认参数)
svm_model <- svm(y ~ ., data = trainset, 
                type = "C-classification", 
                kernel = "polynomial", 
                degree = 2)
svm_model
....
Number of Support Vectors:  204
# 预测
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
0.8666667
plot(svm_model, trainset)
R 中的支持向量机

第 4.2 章 - 复杂数据集,默认二次核,用 svm.plot 作图

R 中的支持向量机

尝试更高次多项式

  • 排除奇数阶 3、5、9 等
  • 试试四次
svm_model <- svm(y ~ ., data = trainset, 
                type = "C-classification", 
                kernel = "polynomial", 
                degree = 4)
svm_model
...
Number of Support Vectors: 203
# 预测
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
0.8583333
plot(svm_model, trainset)
R 中的支持向量机

第 4.2 章 - 复杂数据集,degree=4 的默认核,用 svm.plot 作图

R 中的支持向量机

另一种思路

  • 直觉:彼此接近的点应同类:
    • 类似 K 近邻算法。
  • 给定数据集中一点,如 X1 = (a, b):
    • 核在 (a, b) 处取最大值
    • 远离 (a, b) 时衰减
    • 各方向衰减速率相同
    • 衰减速率可调
  • 满足以上性质的简单函数是 exp(-gamma * r),其中 r 为 X1 与任一点 X 的距离
R 中的支持向量机

RBF 核随 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...