多類別問題

R 的支援向量機

Kailash Awati

Instructor

鳶尾花資料集:導覽

  • 150 筆、5 個屬性的量測
    • 花瓣寬、長-數值(預測變數)
    • 花萼寬、長-數值(預測變數)
    • 品種-類別:setosa、virginica、versicolor(被預測變數)
  • 資料集來自 UCI ML repository
R 的支援向量機

視覺化鳶尾花資料集

  • 繪製花瓣長度對花瓣寬度。
library(ggplot2)

# Plot petal length vs width for dataset, distinguish species by color
p <- ggplot(data = iris,
            aes(x = Petal.Width,
                y = Petal.Length,
                color = Species)) +
     geom_point()

# Display plot
p
R 的支援向量機

第 2.4 章-鳶尾花資料集:花瓣長度 vs 花瓣寬度,品種以顏色區分

R 的支援向量機

SVM 如何處理多類別問題?

  • SVM 本質上是二元分類器。
  • 可用以下投票策略處理多類別問題:
    • 將資料分割成僅含兩類的子集。
    • 對每個子集解二元分類問題。
    • 以多數決為每個資料點指派類別。
  • 稱為 one-against-one 分類策略。
R 的支援向量機

建立多類別線性 SVM

  • 為鳶尾花資料集建立線性 SVM
    • 80/20 訓練/測試切分(seed 10),cost 預設值
library(e1071)

# Build model
svm_model <- svm(Species ~ ., 
                data = trainset, 
                type = "C-classification", 
                kernel = "linear")
  • 計算準確率
pred_train <- predict(svm_model, trainset)
mean(pred_train == trainset$Species)
0.9756098
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$Species)
0.962963
R 的支援向量機

一起來練習吧!

R 的支援向量機

Preparing Video For Download...