Method · Statistics Library

Support vector machine (SVM)

A support vector machine finds the boundary between classes that leaves the widest possible margin, supported only by the closest borderline points (support vectors), with a kernel trick allowing that boundary to bend non-linearly without ever building new columns.

Topic indexA reference reading outside the guided chapter sequence

Understand the idea

Imagine trying to draw a line separating two groups of dots on a page so that the line sits as far away as possible from both groups, the widest possible 'buffer zone' between them. That's exactly what a support vector machine does: it finds the boundary with the maximum margin, and the only points that actually matter for defining that boundary are the ones closest to it on either side, called support vectors, like the few tent poles that actually hold up a tent, while the rest of the fabric could be moved without changing the tent's shape at all.

Real data is rarely perfectly separable, so the method allows a limited 'budget' of points that fall inside the buffer zone or even on the wrong side, controlled by a dial called C. A small C keeps things calm and tolerant (a wide, forgiving margin that accepts some misclassified points, risking underfitting); a large C makes violations expensive, so the boundary contorts itself trying to get every single training point right, risking overfitting badly. You cannot pick the right C by looking at training accuracy: how well the model does on the rows it learned from does not tell you how well it will do on new rows, which is why this method's honest evaluation has to be cross-validated.

The 'kernel trick' is what lets the boundary bend into complex, non-linear shapes without literally constructing any new predictor columns: it works entirely by computing how SIMILAR pairs of points are to each other in a richer, implicit space. The default (RBF) kernel measures similarity by distance, letting each support vector cast a local 'sphere of influence' so the boundary can wrap around shapes a straight line never could.

An SVM, or support vector machine, grows out of the maximum margin classifier, which finds the boundary with the widest gap to the nearest rows; the support vector classifier relaxes this by letting a few rows sit inside the gap. For a numeric outcome the same idea is called SVR, support vector regression.

Practitioner explanation

KernelStats offers linear, polynomial, and RBF (default) kernels, with C the margin-violation cost and gamma the kernel reach (default 'scale' resolving to 1/(p*Var(X)), exactly 1/p on standardized data, with the numeric value used echoed on the card). Standardization is treated as NEARLY MANDATORY rather than a nice-to-have: both the margin and the RBF kernel are built from distances, so an unscaled predictor with large units (body mass in grams, SD ~800) would silently dominate a small-unit predictor (bill depth in mm, SD ~2); predictors are z-scored INSIDE each cross-validation fold (no leakage), and for regression (SVR) the OUTCOME is also z-scored internally with predictions back-transformed, so options like C=1 and epsilon=0.1 mean a consistent '1 unit' / '0.1 SD' regardless of whether the outcome happens to be measured in grams or dollars.

For SVR, epsilon defines a tube around the prediction in the internally standardized outcome units. Points strictly inside the tube have zero epsilon-insensitive loss. Points on its boundary may be support vectors while their loss is zero; points outside incur loss max(0, abs(y-f(x))-epsilon), penalized by C. Changing epsilon changes the tolerance, not a guarantee about prediction accuracy.

The product states plainly, as a specific limitation rather than an incidental detail, what an SVM will NOT give you: no probabilities by default (just a class; Platt scaling can bolt on probabilities, but they are explicitly described as a POST-HOC APPROXIMATION, not a native output), no coefficients/confidence intervals/p-values with a non-linear kernel (there is no equation to read at all), and no causal story (a wide margin means the classes ARE SEPARABLE in these predictors, not that the predictors CAUSE the class). The product also gives a specific, concrete example of when the simpler model wins: on near-separable, roughly linear problems such as penguin species, a linear classifier cross-validates within a whisker of the SVM and sometimes beats it (inside KernelStats the linear baseline for three species is LDA: the unpenalized multinomial logit does not converge on these measurements because the species are nearly separable); the RBF kernel is recommended specifically when the cross-validated numbers demonstrate the boundary genuinely is non-linear, not by default. A training-time budget (10 seconds by default) stops a run projected to exceed it with an explanation, rather than freezing the interface, since SVM training cost grows roughly quadratically with row count.

When to use it

Consider it when

Predicting a category (or, less commonly, a continuous outcome) when the boundary between classes may be non-linear, strong accuracy from a moderate number of rows matters more than interpretable coefficients, and the dataset is small to moderate in size (training cost grows roughly quadratically with rows).

Use caution when

You need interpretable coefficients, confidence intervals, or calibrated probabilities natively (an SVM with a non-linear kernel gives none of these), the dataset is large enough that quadratic training-time growth becomes impractical, or a simpler logistic/linear regression already achieves comparable cross-validated performance, in which case the simpler model should be preferred for its interpretability at no real accuracy cost.

Formulas and symbols

Binary soft-margin C-SVC primal

min⁡β,β0,ξ  12∥β∥22+C∑i=1nξisubject toyi(β⊤ϕ(xi)+β0)≥1−ξi,ξi≥0\min_{\beta,\beta_0,\xi}\;\frac12\lVert\beta\rVert_2^2+C\sum_{i=1}^n\xi_i\quad\text{subject to}\quad y_i(\beta^\top\phi(x_i)+\beta_0)\ge1-\xi_i,\quad\xi_i\ge0

Binary classification with labels y_i in {-1,+1}: phi is the feature map, beta the separating weights, beta_0 the intercept, and nonnegative xi_i the margin slacks. C > 0 trades margin regularization against violation penalties. Kernel SVC uses the corresponding dual; multiclass classification combines binary problems. SVR has a different epsilon-insensitive objective.

RBF (radial) kernel

KRBF(x,x′)=e−γ∥x−x′∥2K_{\text{RBF}}(x, x') = e^{-\gamma \lVert x - x' \rVert^2}

gamma controls the reach of each support vector's influence; small gamma = smooth long-reach boundaries, large gamma = wiggly, potentially memorizing boundaries.

Support vector fraction

support_fraction=nsupport vectorsn\text{support\_fraction} = \dfrac{n_{\text{support vectors}}}{n}

The share of training rows that ended up as support vectors; a large fraction can indicate the boundary is nearly as complex as the data itself.

Assumptions and checks

  • SVM's kernel (including the default RBF) computes distances between rows; predictors on very different scales let the largest-scale variable dominate that distance, so unstandardized inputs make the margin reflect scale rather than genuine similarity.

    In KernelStats: Checked directly: the card reports whether predictors were standardized, or (if not) the ratio between the largest and smallest predictor spread, always flagging unscaled inputs as a warning regardless of the ratio.

  • With too few rows per encoded feature, the margin and support-vector selection become unstable, and predictions can change noticeably with a slightly different sample.

    In KernelStats: Checked directly: the card reports rows-per-encoded-feature and flags it as a warning below 10:1.

  • With a severely imbalanced classification target, the margin can be dominated by the majority class, and the minority class contributes few support vectors, understating how useful the model is for it.

    In KernelStats: Checked directly for classification: the card reports the smallest class's share of rows (flagged below 10%) and separately reports the fraction of rows that became support vectors, flagging a majority-support-vector solution (above 50%) as a sign the margin is not sparse.

Alternatives

  • Logistic Regression. The classes are close to linearly separable, or interpretable coefficients and calibrated probabilities are needed: logistic regression can match or beat an SVM on such problems while remaining fully interpretable.
  • Random Forest. You want a non-linear model with native feature importance and probability estimates, which an SVM does not provide by default.

Worked example

Scenario

Classifying penguin species from bill length, bill depth and flipper length with an RBF kernel (defaults: gamma = 'scale' = 1/3 on standardized data, C = 1), compared against linear discriminant analysis on the same predictors.

Result

SVM: in-sample 98.2%, 5-fold cross-validated 97.9%, with 15.5% of rows as support vectors; LDA: in-sample 98.2%, cross-validated 97.9%.

Interpretation

Because the species classes are close to linearly separable in these measurements, the simpler linear classifier ties the more flexible SVM exactly, the scenario the product's own guidance anticipates, so the linear model should be preferred here: it loses nothing in accuracy and gains interpretable discriminant coefficients and model-based probabilities.

Dataset: penguins.csv

Common misconceptions

Misconception

Increasing C to improve training accuracy is a reasonable way to tune an SVM.

Correction

C controls the tradeoff between margin regularization and the penalty on hinge-loss violations. The objective is not zero-one training accuracy: increasing C does not guarantee monotonically improving accuracy or a perfect fit, even at a very large value. Contradictory duplicate predictors with different labels cannot all be classified correctly by the same deterministic rule. Select C using suitable held-out or cross-validated performance, with preprocessing fitted inside each training split; retain a separate test set when needed for final evaluation.

Misconception

The probabilities an SVM reports (via Platt scaling) are as reliable and native as those from logistic regression.

Correction

An SVM produces no probabilities natively, only a class decision from which side of the margin a point falls on. Platt scaling can bolt probabilities on afterward, but the product describes this explicitly as a POST-HOC APPROXIMATION, not a genuinely modeled probability the way logistic regression's are.

References

  1. Support-Vector Networks, Cortes, C. and Vapnik, V. (1995), Machine Learning, 20(3), 273-297.
  2. An Introduction to Statistical Learning with Applications in R (2nd ed.), Chapter 9, James, G., Witten, D., Hastie, T. and Tibshirani, R. (2021).
  3. scikit-learn User Guide: Support Vector Machines, mathematical formulation, scikit-learn developers.