Motivating the RBF kernel

Support Vector Machines in R

Kailash Awati

Instructor

Quadratic kernel (default parameters)

  • Partition data into test/train (not shown)
  • Use degree 2 polynomial kernel (default params)
svm_model <- svm(y ~ ., data = trainset, 
                type = "C-classification", 
                kernel = "polynomial", 
                degree = 2)
svm_model
....
Number of Support Vectors:  204
# Predictions
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
0.8666667
plot(svm_model, trainset)
Support Vector Machines in R

Chapter 4.2 - complex dataset, default quadratic kernel, plot using svm.plot

Support Vector Machines in R

Try higher degree polynomial

  • Rule out odd degrees -3,5,9 etc.
  • Try degree 4
svm_model <- svm(y ~ ., data = trainset, 
                type = "C-classification", 
                kernel = "polynomial", 
                degree = 4)
svm_model
...
Number of Support Vectors: 203
# Predictions
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
0.8583333
plot(svm_model, trainset)
Support Vector Machines in R

Chapter 4.2 - complex dataset, default degree=4 kernel, plot using svm.plot

Support Vector Machines in R

Another approach

  • Heuristic: points close to each other have the same classification:
    • Akin to K-Nearest Neighbors algorithm.
  • For a given point in the dataset, say X1 = (a, b):
    • The kernel should have a maximum at (a, b)
    • Should decay as one moves away from (a, b)
    • The rate of decay should be the same in all directions
    • The rate of decay should be tunable
  • A simple function with this property is exp(-gamma * r), where r is the distance between X1 and any other point X
Support Vector Machines in R

How does the RBF kernel vary with gamma (code)

#rbf function
rbf <- function(r, gamma) exp(-gamma * r)

ggplot(data.frame(r = c(-0, 10)), aes(r)) + stat_function(fun = rbf, args = list(gamma = 0.2), aes(color = "0.2")) + stat_function(fun = rbf, args = list(gamma = 0.4), aes(color = "0.4")) + stat_function(fun = rbf, args = list(gamma = 0.6), aes(color = "0.6")) + stat_function(fun = rbf, args = list(gamma = 0.8), aes(color = "0.8")) + stat_function(fun = rbf, args = list(gamma = 1), aes(color = "1")) + stat_function(fun = rbf, args = list(gamma = 2), aes(color = "2")) + scale_color_manual("gamma", values = c("red","orange","yellow", "green","blue","violet")) + ggtitle("Radial basis function (gamma = 0.2 to 2)")
Support Vector Machines in R

image description

Support Vector Machines in R

Time to practice!

Support Vector Machines in R

Preparing Video For Download...