線形SVMのチューニング

Rで学ぶSupport Vector Machines

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で学ぶSupport Vector Machines

第2章2.3 - 線形分離データセット、デフォルトcostの線形カーネル(サポートベクター・決定境界・マージン境界付き)

Rで学ぶSupport Vector Machines

cost = 100の線形SVM

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で学ぶSupport Vector Machines

第2章2.3 - 線形分離データ、cost=100の線形カーネル(サポートベクター・決定境界・マージン境界付き)

Rで学ぶSupport Vector Machines

示唆

  • 決定境界が線形であることが分かっている場合、マージンを縮小することが有効な場合がある
  • ...ただし、実際にはそのようなケースはほとんどない
Rで学ぶSupport Vector Machines

第2章2.3 - 非線形分離データセット

Rで学ぶSupport Vector Machines

非線形データセット、線形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で学ぶSupport Vector Machines

第2章2.3 - 非線形分離データセット、cost=100の線形カーネル(サポートベクター・決定境界・マージン境界付き)

Rで学ぶSupport Vector Machines

非線形データセット、線形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で学ぶSupport Vector Machines

第2章2.3 - 非線形分離データセット、cost=1の線形カーネル(サポートベクター・決定境界・マージン境界付き)

Rで学ぶSupport Vector Machines

練習しましょう!

Rで学ぶSupport Vector Machines

Preparing Video For Download...