调优线性 SVM

R 中的支持向量机

Kailash Awati

Instructor

线性 SVM,默认 cost

library(e1071)
svm_model <- svm(y ~ ., 
                data = trainset, 
                type = "C-classification", 
                kernel = "linear", 
                scale = FALSE)
# Print model summary
svm_model
Call:
svm(formula = y ~ .,
    data = trainset,
    type = "C-classification", 
    kernel = "linear",
    scale = FALSE)

Parameters:
SVM-Type:  C-classification 
 SVM-Kernel:  linear 
       cost:  1 
      gamma:  0.5 
Number of Support Vectors:  55
R 中的支持向量机

第 2.3 章 - 线性可分数据集,默认 cost 线性核,含支持向量、决策边界与间隔

R 中的支持向量机

线性 SVM(cost = 100)

library(e1071)
svm_model <- svm(y ~ ., 
                data = trainset, 
                type = "C-classification", 
                kernel = "linear", 
                cost = 100,
                scale = FALSE)
# Print model summary
svm_model
Call:
svm(formula = y ~ .,
    data = trainset,
    type = "C-classification", 
    kernel = "linear",
    cost = 100,
    scale = FALSE)

Parameters:
SVM-Type:  C-classification 
 SVM-Kernel:  linear 
       cost:  100 
      gamma:  0.5 
Number of Support Vectors:  6
R 中的支持向量机

第 2.3 章 - 线性可分数据,cost = 100 线性核,含支持向量、决策边界与间隔

R 中的支持向量机

含义

  • 若已知决策边界为线性,可适当减小间隔
  • 但现实中很少如此
R 中的支持向量机

第 2.3 章 - 非线性可分数据集

R 中的支持向量机

非线性数据集,线性 SVM(cost = 100)

  • 使用 80% 训练集,构建 cost=100 的模型
# Build model
library(e1071)
svm_model<- svm(y ~ ., 
                data = trainset, 
                type = "C-classification", 
                kernel = "linear", 
                cost = 100,
                scale = FALSE)
  • 计算准确率
# Train and test accuracy
pred_train <- predict(svm_model, trainset)
mean(pred_train == trainset$y)
0.8208333
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
0.85
  • 在 50 次随机划分上,测试准确率均值:82.9%
R 中的支持向量机

第 2.3 章 - 非线性可分数据集,cost=100 线性核,含支持向量、决策边界与间隔

R 中的支持向量机

非线性数据集,线性 SVM(cost = 1)

  • 重新构建模型,设置 cost = 1
# Trainset contains 80% of data
# Same train/test split as before.
# Build model
svm_model <- svm(y ~ ., 
                data = trainset, 
                type = "C-classification", 
                kernel = "linear", 
                cost = 1,
                scale = FALSE)
  • 计算测试集准确率
# Test accuracy
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
0.8666667
  • 在 50 次随机划分上,测试准确率均值:83.7%
R 中的支持向量机

第 2.3 章 - 非线性可分数据集,cost=1 线性核,含支持向量、决策边界与间隔

R 中的支持向量机

Passons à la pratique !

R 中的支持向量机

Preparing Video For Download...