The RBF Kernel

Support Vector Machines in R

Kailash Awati

Instructor

RBF Kernel in a nutshell

  • Decreasing function of distance between two points in dataset.
  • Simulates k-NN algorithm.
Support Vector Machines in R

Chapter 4.3 - RBF kernel - influence as a function of distance between points

Support Vector Machines in R

Building an SVM using the RBF kernel

  • Build RBF kernel SVM for complex dataset
svm_model <- svm(y ~ ., 
                data = trainset, 
                type = "C-classification",
                kernel = "radial")
  • Calculate training/test accuracy and plot against training dataset.
pred_train <- predict(svm_model, trainset)
mean(pred_train == trainset$y)
0.93125
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
0.9416667
#plot decision boundary
plot(svm_model, trainset)
Support Vector Machines in R

Chapter 4.3 - complex dataset, default RBF kernel, plot using svm.plot

Support Vector Machines in R

Refining the decision boundary

  • Tune gamma and cost using tune.svm()
# Tune parameters
tune_out <- tune.svm(x = trainset[,-3],
                     y = trainset[,3],
                     gamma = 5 * 10 ^ (-2:2),
                     cost = c(0.01, 0.1, 1, 10, 100),
                     type = "C-classification",
                     kernel = "radial")
  • Print best parameters
# Print best values of cost and gamma
tune_out$best.parameters$cost
1
tune_out$best.parameters$gamma
5
Support Vector Machines in R

The tuned model

  • Build tuned model using best.parameters
svm_model <- svm(y ~ ., data = trainset, 
              type = "C-classification", 
              kernel = "radial",
              cost = tune_out$best.parameters$cost,
              gamma = tune_out$best.parameters$gamma)
  • Calculate test accuracy
mean(pred_test == testset$y)
0.95
  • Plot decision boundary
plot(svm_model, trainset)
Support Vector Machines in R

Chapter 4.3 - complex dataset, tuned RBF kernel, plot using svm.plot

Support Vector Machines in R

Time to practice!

Support Vector Machines in R

Preparing Video For Download...