Key ideas
- An SVM is a linear classifier that predicts and picks, among all separating hyperplanes, the one with the widest margin ("street") to the nearest training points1. In 2-D the hyperplane is a line.
- Hard margin: every point must satisfy . This only has a solution when the data are linearly separable1.
- Soft margin: slack variables let points enter the street or cross it, at a cost . Large punishes violations hard (close to hard margin, risk of overfitting); small allows more violations and gives a smoother, more regularised boundary1,3.
- Training is a convex quadratic program, so the optimum found is the global one; there are no bad local minima as in neural networks1.
- Support vectors are the training points with a non-zero dual weight : the points on the gutters, inside the street, or misclassified. All other points have and can be deleted without changing the solution1,2. Because a non-support vector left out is still classified correctly, the leave-one-out error is at most (number of support vectors)/.
- Kernel trick: the dual uses the data only through inner products, so can be replaced by . The SVM then fits a linear boundary in feature space, which is non-linear in input space, without ever computing 1,5. A valid kernel must give a positive semi-definite Gram matrix5.
- SVR (support vector regression) fits a function with an -tube around it: errors smaller than cost nothing, and only points outside the tube are penalised. So SVMs do both classification and regression1,2.
- An SVM outputs a score, not a probability. scikit-learn's
probability=Truefits a logistic model on the scores (Platt scaling) with an extra internal cross-validation. That step is expensive, and its probabilities can disagree withpredict4.
Formulas
Primal (soft margin) and dual1,2:
- Hard margin = no upper bound on (the limit ).
- and 2.
- Street width ; the distance from the boundary to the closest point (geometric margin) is 1.
- KKT conditions: ; (on a gutter); (inside the street, and misclassified only if ).
Kernels in scikit-learn3:
| Kernel | Notes | |
|---|---|---|
| linear | plain linear SVM | |
| polynomial | all monomials up to degree if (only degree if ) | |
| RBF (Gaussian) | infinite-dimensional ; | |
| sigmoid |
- is the dot product of with .
- A degree-2 kernel such as spans all monomials of degree , so it can reproduce the boundary of a Gaussian class-conditional model (quadratic in general, linear with a shared covariance).
- RBF sets how far one training point's influence reaches: large (small ) gives narrow bumps and a wiggly, overfit-prone boundary; small gives a smoother one. Tune and together by cross-validated grid search on a log scale3.
- Far from all training points every RBF term vanishes, so .
Worked example
Negatives ; positives . The closest opposite pair is and , so they are the support vectors and points along their difference, .
- Gutter conditions: and , so , , .
- Check the others: , so they lie outside the street and have .
- Duals: gives for both support vectors, and holds.
- Street width , the distance between the two support vectors.
- New point : , so it is classified but lies inside the street.
In code
import numpy as np
from sklearn.svm import SVC
X = np.array([[-1, 0], [0, -1], [0, 0], [2, 1], [3, 1], [2, 2]])
y = np.array([-1, -1, -1, 1, 1, 1])
clf = SVC(kernel="linear", C=1e6).fit(X, y) # huge C ~ hard margin
print(clf.coef_, clf.intercept_) # [[0.8 0.4]] [-1.]
print(clf.support_vectors_) # [[0. 0.] [2. 1.]]
print(clf.dual_coef_) # [[-0.4 0.4]] = y_i * alpha_i
print(clf.decision_function([[1, 2]])) # [0.6]
Common pitfalls
- Support vectors are the points closest to the boundary (or violating the margin), not the farthest ones; they alone determine .
- An SVM does not output a probability distribution; Platt scaling is an add-on fitted afterwards4.
- "" means the point lies on or inside the margin (), not necessarily that it is misclassified.
- An RBF SVM that fits the training set perfectly but generalises badly needs a larger (smaller ) and/or a smaller , chosen by cross-validation, not a higher .
- Margins measured in different kernels' feature spaces are not comparable: scaling by 2 doubles the margin without changing the boundary. A larger margin also does not guarantee the lowest test error among linear classifiers.
- Support vectors generally change when you switch kernels, since the feature space changes.
- SVMs are not scale invariant: standardise the features first, with the same scaling on test data3.
References
- Wikipedia: Support vector machine — Linear SVM (Hard-margin, Soft-margin); Nonlinear kernels; Computing the SVM classifier (Dual, Kernel trick, Sub-gradient descent); Properties — Issues; Extensions — Regression
- scikit-learn User Guide: Support Vector Machines — Sec. 1.4.7 Mathematical formulation (1.4.7.1 SVC, 1.4.7.2 SVR); Sec. 1.4.2 Regression
- scikit-learn User Guide: Support Vector Machines — Sec. 1.4.6 Kernel functions; Sec. 1.4.6.1 Parameters of the RBF Kernel; Sec. 1.4.5 Tips on Practical Use
- scikit-learn User Guide: Support Vector Machines — Sec. 1.4.1.2 Scores and probabilities
- Wikipedia: Kernel method — Mathematics: the kernel trick