Support vector machines

Support vector machines

How support vector machines find the widest-margin boundary between classes, and with kernels handle nonlinear problems, for classification and regression on small to medium data sets.

What it is

A support vector machine (SVM) is a supervised learning method that separates classes by finding the boundary with the largest possible margin, the gap between the boundary and the nearest training points of each class. Those nearest points are the support vectors, and they alone define the boundary. Introduced by Vladimir Vapnik and colleagues in the 1990s, SVMs rest on statistical learning theory, which links a wide margin to good generalization. With the kernel trick they draw nonlinear boundaries, and the same framework gives support vector regression (SVR) and one-class SVMs for novelty detection. SVMs are widely used for text and spectral classification, bioinformatics, image recognition (before deep learning), fault diagnosis and small data sets with many variables.

It answers questions such as: Which class does this sample belong to, given a modest number of labeled examples? How can classes that are not linearly separable be separated? What value is predicted by a smooth regression model that tolerates small errors? Is a new sample unlike the normal data? SVMs work well when the number of variables is large relative to the number of samples, but scale poorly to very large data sets.

Fundamentals

Maximum margin and the soft margin

For two classes labeled yi = ±1, a linear classifier is f(x) = w·x + b, and a point is classified by the sign of f(x). The distance from a point to the boundary is |f(x)|/‖w‖, and the margin between the two classes is 2/‖w‖. A hard-margin SVM maximizes it by solving: minimize ½‖w‖2 subject to yi(w·xi + b) ≥ 1. Real data overlap, so the soft-margin SVM allows violations with slack variables ξi ≥ 0: minimize ½‖w‖2 + C Σ ξi. This is equivalent to minimizing the hinge loss, max(0, 1 − y f(x)), plus an L2 penalty. The parameter C sets the trade-off: a large C punishes errors heavily (narrow margin, risk of overfitting), whereas a small C gives a wider, smoother margin with more training errors.

The dual form and the kernel trick

Solving through Lagrange multipliers αi gives the dual problem: maximize Σαi − ½ΣΣ αiαjyiyj (xi·xj) subject to 0 ≤ αi ≤ C and Σαiyi = 0. Only points with αi > 0, the support vectors, appear in the decision function f(x) = Σ αiyi (xi·x) + b. Because data enter only through dot products, they can be replaced by a kernel function K(x, x′) = φ(x)·φ(x′), the dot product in a high-dimensional feature space, computed without ever building the space. Common kernels: linear K = x·x′; polynomial K = (γ x·x′ + r)d; radial basis function (RBF), K = exp(−γ‖x − x′‖2), where γ controls how local the influence of each point is (large γ means a wiggly boundary, risk of overfitting); and sigmoid. The kernel must give a positive semi-definite Gram matrix (Mercer’s condition). Custom kernels exist for strings, graphs and spectra.

Regression, multiclass and probabilities

SVR uses an ε-insensitive loss: errors smaller than ε cost nothing, and larger ones are penalized linearly, so only points outside the ε-tube become support vectors. The three parameters are C, ε and the kernel parameters. SVMs are binary by nature, and multiclass problems are handled by one-vs-rest or one-vs-one schemes. The output f(x) is a score, not a probability, and probabilities can be obtained by Platt scaling, fitting a logistic function to the scores with cross-validation, at extra cost. Class imbalance is handled with class-specific C weights. The one-class SVM finds a boundary around normal data and flags outliers.

Preparing data and tuning

SVMs are sensitive to feature scales, because kernels use distances, so variables should be standardized (scaling fitted on training data only). C and γ (and ε for SVR) are tuned by cross-validation over a log-spaced grid, for example C from 10−2 to 103 and γ from 10−4 to 101, or by Bayesian optimization (see cross-validation). A linear kernel is a good first choice for high-dimensional sparse data such as text, whereas RBF is the usual default for other problems. Training cost grows roughly between O(n2) and O(n3) in the number of samples, which makes kernel SVMs slow beyond about 105 samples, where linear SVMs (LIBLINEAR, stochastic gradient descent) or approximate kernel maps are used. The model stores the support vectors, so prediction cost grows with their number.

Software, typical applications and limits

Tools: LIBSVM and LIBLINEAR, scikit-learn (SVC, SVR, LinearSVC, OneClassSVM), R (e1071, kernlab), and MATLAB (fitcsvm). Applications include cancer classification from gene expression, near-infrared spectra, handwriting, text categorization and quality control. Limits: poor scaling to very large data, no native probabilities, sensitivity to hyperparameters and scaling, limited interpretability with nonlinear kernels, and in many large problems tree ensembles or neural networks perform better.

Basic operation

  1. Prepare the data. Clean, encode categories and split into training and test sets.
  2. Standardize features. Fit the scaler on training data only.
  3. Choose a kernel. Start with linear for sparse high-dimensional data, RBF otherwise.
  4. Tune C and γ. Use cross-validation on a log-spaced grid.
  5. Train the final model. Fit on the full training set with the best parameters.
  6. Evaluate on the test set. Use metrics suited to class balance.
  7. Inspect support vectors. A very large fraction suggests overfitting or noisy data.
  8. Collect the output. You obtain a classifier or regression model; kernel choice and tuning are covered in the video.

Good practice

  • Always scale features. Unscaled variables dominate distances.
  • Tune on validation data. Never use the test set for tuning.
  • Start with a linear kernel. It is fast and a strong baseline.
  • Handle imbalance. Use class weights and suitable metrics.
  • Check support vector count. It reflects model complexity.
  • Be careful with sensitive decisions. Check performance across groups, and do not use an SVM score as the sole basis for medical or legal decisions.

Key terms

  • Margin — the distance between the decision boundary and the nearest training points.
  • Support vector — a training point that lies on or inside the margin and defines the boundary.
  • Kernel trick — computing dot products in a high-dimensional space without constructing it.
  • RBF kernel — a kernel based on distance, with width set by γ.
  • Hinge loss — a loss that penalizes points inside the margin or misclassified.
  • Slack variable — a measure of how far a point violates the margin.
  • C parameter — controls the trade-off between margin width and training errors.

Video coming soon

About five minutes on Support vector machines: how it works in detail, how the data is acquired, and how it is analyzed.

Shopping cart

0
image/svg+xml

No products in the cart.

Continue Shopping