Support Vector Machines

Slide 1 of 24: Support Vector Machines
Support Vector Machines Abhinav Mishra, Kristin Köhler, Sanket Gosavi, Utkarsha Kandale Group 4 Data Science in the Life Sciences

Table of Contents

Slide 2 of 24: Table of Contents
Table of Contents Introduction: Hyperplane Maximal Margin Classifier Support Vector Classifier Support Vector Machine SVM (with more than two classes) 6. SVM vs Logistic Regression 7. Example in R

Hyperplanes

Slide 3 of 24: Hyperplanes
Hyperplanes ● Hyperplanes are a way to divide (classify) your data. ● For p-dimensional data the hyperplane will be of dimension p-1. Mathematical definition ● For a two dimensional data the hyperplane would be defined as: ● For n-dimensional data: ● For classification:

Maximal Margin Classifier

Slide 4 of 24: Maximal Margin Classifier
Maximal Margin Classifier ● There are potentially infinite number of ways in which you can divide the data using a hyperplane. ● One way to find a good hyperplane is to use a Maximal Margin Classifier. ● The Maximal Margin Classifier is composed of three components: ○ Dividing hyperplane ○ Supporting vectors ○ Margins

Construction of the Maximal Margin Classifier

Slide 5 of 24: Construction of the Maximal Margin Classifier
Construction of the Maximal Margin Classifier ● Equation three ensures that each observation is classified correctly for all the observations provided that M is positive. ● The second equation allows one to calculate the perpendicular distance of an observation from the hyperplane using the equation: ● Together equations two and three ensure that each observation is on the correct side of the hyperplane and at least a distance M from the hyperplane. ● M represents the margin of our hyperplane, and the optimization problem chooses β0, β1, . . . , βp to maximize M

Support Vector Classifier

Slide 6 of 24: Support Vector Classifier
Support Vector Classifier Problem 1: Non-separable data

Support Vector Classifier

Slide 7 of 24: Support Vector Classifier
Support Vector Classifier Problem 2: Noisy data > overfitting to training data

Support Vector Classifier

Slide 8 of 24: Support Vector Classifier
Support Vector Classifier The support vector classifier maximizes a soft margin.

Support Vector Classifier

Slide 9 of 24: Support Vector Classifier
Support Vector Classifier The support vector classifier maximizes a soft margin. ε- slack variable allowing misclassifications C- tuning parameter

Support Vector Classifier

Slide 10 of 24: Support Vector Classifier
Support Vector Classifier ε = 0: ith observation on the correct side of the margin ε > 0: ith observation on the wrong side of the margin ε > 1: ith observation on the wrong side of the hyperplane

Support Vector Classifier

Slide 11 of 24: Support Vector Classifier
Support Vector Classifier ε = 0: ith observation on the correct side of the margin ε > 0: ith observation on the wrong side of the margin ε > 1: ith observation on the wrong side of the hyperplane

Support Vector Classifier

Slide 12 of 24: Support Vector Classifier
Support Vector Classifier large C: wider margin, more misclassifications allowed > high bias-low variance small C: small margin, less misclassifications allowed > low bias-high variance only observations that either lie on the margin or that violate the margin will affect the hyperplane => support vectors

Support Vector Machines

Slide 13 of 24: Support Vector Machines
Support Vector Machines Problem: Non-linear decision boundaries

Support Vector Machines

Slide 14 of 24: Support Vector Machines
Support Vector Machines Idea: enlarge the feature space to get a separating hyperplane in a higher dimension

Support Vector Machines To estimate the parameters α 1 , . . . , α n and β 0 , all we need are the inner products ⟨x i ,

Slide 15 of 24: Support Vector Machines To estimate the parameters α 1 , . . . , α n and β 0 , all we need are the inner products ⟨x i ,
Support Vector Machines The linear support vector classifier can be represented as: with To estimate the parameters α1, . . . , αn and β0, all we need are the inner products ⟨xi, xi′⟩ between all pairs of training observations. αi is nonzero only for the support vectors in the solution with S as indices of support points

Support Vector Machines Replace the inner product with a generalization of the form K(x,x i )

Slide 16 of 24: Support Vector Machines Replace the inner product with a generalization of the form K(x,x i )
Support Vector Machines Replace the inner product with a generalization of the form K(x,xi) called kernel function Linear kernel Polynomial kernel of degree ‘d’ Radial kernel

Support Vector Machines

Slide 17 of 24: Support Vector Machines
Support Vector Machines Polynomial kernel (degree = 3) Radial kernel

SVM ( > 2 Classes)

Slide 18 of 24: SVM ( > 2 Classes)
SVM ( > 2 Classes) As defined before, it works for 𝑲 > 2 classes. OVA One versus All. Fit 𝑲 different 2-class SVM classifiers . . , 𝑲 ; each class versus the rest. Classify 𝑥* to the class for which is largest. OVO One versus One. Fit all pairwise classifiers the class that wins the most pairwise competitions. . Classify 𝑥* to Which to choose? If 𝑲 is not too large, use OVO.

Support Vector versus Logistic Regression ?

Slide 19 of 24: Support Vector versus Logistic Regression ?
Support Vector versus Logistic Regression ? With , can be written as: This has the form loss plus penalty. The loss is known as the hinge loss. Very similar to “loss” in logistic regression (negative log-likelihood).

Which to use: SVM or Logistic Regression ?

Slide 20 of 24: Which to use: SVM or Logistic Regression ?
Which to use: SVM or Logistic Regression ? • When classes are (nearly) separable, SVM does better than LR. So does LDA. • When not, LR (with ridge penalty) and SVM very similar. • If you wish to estimate probabilities, LR is the choice. • For nonlinear boundaries, kernel SVMs are popular. Can use kernels with LR and LDA as well, but computations are more expensive.

Example in R

Slide 21 of 24: Example in R
Example in R The e1071 library contains implementations for a number of statistical learning methods. In particular, the svm() function can be used to fit a support vector classifier when the argument kernel = "linear" is used. > set.seed (1) > x <- matrix(rnorm (20 * 2), ncol = 2) > y <- c(rep(-1, 10), rep(1, 10)) > x[y == 1, ] <- x[y == 1, ] + 1 > plot(x, col = (3 - y)) Next, we fit the support vector classifier. > dat <- data.frame(x = x, y = as.factor(y)) > library(e1071) > svmfit <- svm(y ∼ ., data = dat , kernel = "linear", cost = 10, scale = FALSE) We can now plot the support vector classifier obtained > plot(svmfit , dat)

The support vectors are plotted as crosses and the remaining observations are plotted as circles; we see here that there

Slide 22 of 24: The support vectors are plotted as crosses and the remaining observations are plotted as circles; we see here that there
The support vectors are plotted as crosses and the remaining observations are plotted as circles; we see here that there are seven support vectors. We can determine their identities as follows: > svmfit$index We can obtain some basic information about the support vector classifier fit using the summary() command: > summary(svmfit) Call: svm(formula = y ∼ ., data = dat , kernel = "linear", cost = 10, scale = FALSE) Parameters: SVM -Type: C- classification SVM -Kernel: linear cost: 10 Number of Support Vectors: 7 Number of Classes: 2 Levels:

References

Slide 23 of 24: References
References ● James, Gareth, Daniela Witten, Trevor Hastie, und Robert Tibshirani. An Introduction to Statistical Learning: With Applications in R. Second edition. Springer Texts in Statistics. New York NY: Springer, 2021. https://doi.org/10.1007/978-1-0716-1418-1. ● Noel Bambrick, SVM: Feature Selection and Kernels, https://towardsdatascience.com/svm-feature-selection-and-kernel s-840781cc1a6c,

Questions?

Slide 24 of 24: Questions?