Building and visualizing the tuned model
In the final exercise of this chapter, you will build a polynomial SVM using the optimal values of the parameters that you obtained from tune.svm()
in the previous exercise. You will then calculate the training and test accuracies and visualize the model using svm.plot()
. The e1071
library has been preloaded and the test and training datasets are available in the dataframes trainset
and testset
. The output of tune.svm()
is available in the variable tune_out
.
This exercise is part of the course
Support Vector Machines in R
Exercise instructions
- Build an SVM using a polynomial kernel of degree 2.
- Use the optimal parameters calculated using
tune.svm()
. - Obtain training and test accuracies.
- Plot the decision boundary against the training data.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
#Build tuned model
svm_model <- svm(y~ ., data = trainset, type = "C-classification",
kernel = ___, degree = ___,
cost = tune_out$___$cost,
gamma = tune_out$___$gamma,
coef0 = tune_out$___$coef0)
#Calculate training and test accuracies
pred_train <- predict(svm_model, ___)
mean(pred_train == ___$y)
pred_test <- predict(svm_model, ___)
mean(pred_test == ___$y)
#plot model
plot(svm_model, trainset)