Get startedGet started for free

Visualizing decision boundaries and margins

In the previous exercise you built two linear classifiers for a linearly separable dataset, one with cost = 1 and the other cost = 100. In this exercise you will visualize the margins for the two classifiers on a single plot. The following objects are available for use:

  • The training dataset: trainset.
  • The cost = 1 and cost = 100 classifiers in svm_model_1 and svm_model_100, respectively.
  • The slope and intercept for the cost = 1 classifier is stored in slope_1 and intercept_1.
  • The slope and intercept for the cost = 100 classifier is stored in slope_100 and intercept_100.
  • Weight vectors for the two costs are stored in w_1 and w_100, respectively
  • A basic scatter plot of the training data is stored in train_plot

The ggplot2 library has been preloaded.

This exercise is part of the course

Support Vector Machines in R

View Course

Hands-on interactive exercise

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

#add decision boundary and margins for cost = 1 to training data scatter plot
train_plot_with_margins <- train_plot + 
    geom_abline(slope = ___, intercept = ___) +
    geom_abline(slope = ___, intercept = ___-1/w_1[2], linetype = "dashed")+
    geom_abline(slope = ___, intercept = ___+1/w_1[2], linetype = "dashed")

#display plot
train_plot_with_margins
Edit and Run Code