開始使用免費開始

使用 ggplot 視覺化支援向量

在這個練習中,你會把用來建立線性 SVM 的訓練資料集畫出來,並標示支援向量。訓練資料集已預先載入為資料框 trainset,SVM 模型則存在變數 svm_model 中。

本練習屬於課程

R 的支援向量機

檢視課程

練習說明

  • 載入 ggplot2
  • 繪出訓練資料集。
  • 使用 SVM 模型中的索引,將支援向量標示在圖上。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

#load ggplot
library(ggplot2)

#build scatter plot of training dataset
scatter_plot <- ggplot(data = ___, aes(x = x1, y = x2, color = y)) + 
    geom_point() + 
    scale_color_manual(values = c("red", "blue"))
 
#add plot layer marking out the support vectors 
layered_plot <- 
    scatter_plot + geom_point(data = trainset[svm_model$___, ], aes(x = x1, y = x2), color = "purple", size = 4, alpha = 0.5)

#display plot
layered_plot
編輯並執行程式碼