ComeçarComece de graça

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.

Este exercício faz parte do curso

Support Vector Machines in R

Ver curso

Instruções do exercício

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

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

#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
Editar e executar o código