Get startedGet started for free

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.

This exercise is part of the course

Support Vector Machines in R

View Course

Exercise instructions

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

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

#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
Edit and Run Code