Logistic regression ended with a structured failure: a linear boundary cannot cut a moon or a circle, no matter the threshold. This lesson keeps the linear machinery and fixes the problem twice over — first by asking for the best possible line (maximum margin), then by the kernel trick, which lets that same linear machinery draw boundaries of nearly arbitrary shape without ever materializing the nonlinear features it is implicitly using.
Maximum margin: which separating line?
On separable data, infinitely many hyperplanes achieve zero training error, and lesson 3 showed the MLE runs off to infinity trying to pick one. The SVM’s answer is geometric: choose the hyperplane that maximizes the margin — the distance to the nearest point of either class. A point’s signed distance to the plane is , so fixing the scale by makes the margin , and the problem becomes
— a convex quadratic program. The margin is not aesthetics: fat-margin classifiers have generalization bounds that depend on the margin rather than the dimension, which is the theoretical license for what the kernel trick will shortly do (work in enormous, even infinite-dimensional, feature spaces without automatically overfitting).
Soft margins and the hinge loss
Real data overlap, so allow violations with slack , penalized at rate :
Eliminate and the constrained program collapses into an unconstrained one that slots straight into this track’s standard template — average loss plus penalty:
The hinge is the third member of a family you now know well: 0–1 loss (what we care about, non-convex, unoptimizable), log loss (smooth convex surrogate, cares about probabilities everywhere), hinge (piecewise-linear convex surrogate, cares only until the margin is met, then exactly zero). That flat region is the SVM’s signature: points beyond the margin contribute nothing — remove them and the solution is unchanged. The solution is carried entirely by the support vectors: points on or inside the margin. Sparsity in samples, where lasso gave sparsity in features. The price of the flat region: hinge scores are not probabilities (Platt scaling from lesson 3 exists precisely to fix this). And is inverse regularization: large = violations expensive = narrow, hard margin (low bias, high variance); small = wide, tolerant margin.
The kernel trick
Here is the pivot. The dual form of the QP (and equally the representer theorem for the penalized form) shows the optimal is a combination of training points, , so both training and prediction touch the data only through inner products . Replace every inner product with a kernel function
and you are running the identical linear algorithm in the feature space — without ever computing . The decision function becomes
Mercer’s condition (the kernel matrix is PSD for every data set) is what guarantees some exists. The working vocabulary:
| Kernel | Implicit feature space | |
|---|---|---|
| Linear | the original one | |
| Polynomial | all monomials up to degree | |
| RBF (Gaussian) | infinite-dimensional |
The RBF kernel deserves its own paragraph, because its one parameter is the most instructive dial in classical ML. is a sum of Gaussian bumps of width centred on support vectors. Small : bumps overlap broadly, the boundary is nearly linear — high bias. Large : each bump shrinks toward its own point, the boundary becomes islands around individual training samples — memorization, in the most literal visual sense. is the bias–variance trade-off from the regularization lesson, rendered as geometry, and together form the grid every practical SVM tuning sweeps.
Start on moons with the linear kernel: the best line fails exactly as logistic regression did. Switch to RBF: the boundary bends through the gap. Now the two experiments that teach the lesson: (1) sweep γ from 0.1 to 12 and watch the boundary morph from almost-straight → smooth curve → jagged islands hugging single points, with test accuracy rising then falling — bias–variance as shape; (2) on circles, note the RBF solves concentric classes effortlessly (distance IS the feature), and on XOR no linear kernel can ever beat 50% by symmetry while RBF carves the four quadrants. Honesty note: this trainer is stochastic-subgradient on the hinge (Pegasos-style) — fast and faithful in shape; production SVMs solve the dual QP exactly (SMO/libsvm) and would give cleaner margins at extreme C.
Where SVMs sit today
A fair status report for 2026: on tabular problems of moderate size, gradient-boosted trees (two lessons from now) usually win; on raw perception, deep nets ate the kernel lunch — a modern reading is that deep learning learns instead of fixing it by choosing . SVMs remain the right tool when: data are medium-sized ( — kernel matrices are ), features are already meaningful, margins matter conceptually, or you need convexity guarantees. And the ideas outlive the tool: max-margin thinking reappears in modern interpolation/implicit-bias research, kernels return as Gaussian processes and as analysis tools for neural networks (NTK), and the hinge → surrogate-loss framework is how classification theory is organized, period.
from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
# ALWAYS scale before an RBF kernel: γ multiplies squared distances,
# so one big-range feature silently owns the kernel.
clf = make_pipeline(StandardScaler(), SVC(kernel="rbf", C=1.0, gamma=1.0))
clf.fit(X_tr, y_tr)
print(clf[-1].n_support_) # the sample-sparsity: how many SVs carry the model
Exercises
Work these before the next lesson
- Show the distance from a point to the plane is , and that the canonical scaling makes the margin — hence maximizing margin = minimizing .
Solution
Worked solutions are part of Premium — unlock all of them for £5/month →
- 4 more exercises — each with a worked solution — are part of Premium. Unlock everything for £5/month →
References
- C. Cortes & V. Vapnik, “Support-Vector Networks”, Machine Learning 1995 — the soft-margin SVM.
- B. Boser, I. Guyon, V. Vapnik, “A Training Algorithm for Optimal Margin Classifiers”, COLT 1992 — the kernel trick enters.
- S. Shalev-Shwartz et al., “Pegasos: Primal Estimated sub-GrAdient SOlver for SVM”, ICML 2007 — the trainer family the widget uses.
- J. Platt, “Sequential Minimal Optimization”, 1998 — how the dual QP is actually solved.
- ESL ch. 12 — margins, kernels, and the statistical view.