LoslegenKostenlos loslegen

Visualizing decision & margin bounds using `ggplot2`

In this exercise, you will add the decision and margin boundaries to the support vector scatter plot created in the previous exercise. The SVM model is available in the variable svm_model and the weight vector has been precalculated for you and is available in the variable w. The ggplot2 library has also been preloaded.

Diese Übung ist Teil des Kurses

Support Vector Machines in R

Kurs anzeigen

Anleitung zur Übung

  • Calculate the slope and intercept of the decision boundary.
  • Add the decision boundary to the plot.
  • Add the margin boundaries to the plot.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

#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
Code bearbeiten und ausführen