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
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
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
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
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
Support Vector Classifier Problem 1: Non-separable data
Support Vector Classifier
Support Vector Classifier Problem 2: Noisy data > overfitting to training data
Support Vector Classifier
Support Vector Classifier The support vector classifier maximizes a soft margin.
Support Vector Classifier
Support Vector Classifier The support vector classifier maximizes a soft margin. ε- slack variable allowing misclassifications C- tuning parameter
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
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
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
Support Vector Machines Problem: Non-linear decision boundaries
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 ,
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
Support Vector Machines Polynomial kernel (degree = 3) Radial kernel
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 ?
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 ?
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
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
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
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?