開始使用免費開始

使用 `ggplot2` 視覺化決策邊界與邊際

在這個練習中,你要把決策邊界與邊際,加入到上一題建立的支援向量散佈圖中。SVM 模型已存在變數 svm_model,權重向量已替你預先計算好並存放在變數 wggplot2 函式庫也已預先載入。

本練習屬於課程

R 的支援向量機

檢視課程

練習說明

  • 計算決策邊界的斜率與截距。
  • 把決策邊界加入到圖上。
  • 把邊際加入到圖上。

動手互動練習

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

#calculate slope and intercept of decision boundary from weight vector and svm model
slope_1 <- -___/w[2]
intercept_1 <- ___$rho/w[2]

#build scatter plot of training dataset
scatter_plot <- ggplot(data = trainset, aes(x = x1, y = x2, color = y)) + 
    geom_point() + scale_color_manual(values = c("red", "blue"))
#add decision boundary
plot_decision <- scatter_plot + ___(slope = ___, intercept = ___) 
#add margin boundaries
plot_margins <- plot_decision + 
 ___(slope = ___, intercept = ___ - 1/w[2], linetype = "dashed")+
 ___(slope = ___, intercept = ___ + 1/w[2], linetype = "dashed")
#display plot
plot_margins
編輯並執行程式碼