始める無料で始める

`ggplot2` を使って決定境界とマージンを可視化する

この演習では、前の演習で作成したサポートベクターの散布図に、決定境界とマージン境界を追加します。SVM モデルは変数 svm_model に、重みベクトルはあらかじめ計算済みで変数 w に用意されています。ggplot2 ライブラリもすでに読み込まれています。

この演習はコースの一部です

Rで学ぶSupport Vector Machines

コースを見る

演習の手順

  • 決定境界の傾きと切片を計算します。
  • 決定境界をプロットに追加します。
  • マージン境界をプロットに追加します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

#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
コードを編集して実行