The idea
Many hyperplanes separate two separable classes; an SVM picks the one with the widest street between them. The intuition for why that generalizes: test points are noisy copies of training points, and the boundary with maximum clearance is the one a noisy point is least likely to stumble across. Scale so the street edges are ; the street (margin) width is then , and maximizing it is the hard-margin problem:
Soft margin and C. Real data overlaps, so allow violations (point may sit inside the street or on the wrong side) and charge per unit:
Small : violations are cheap → wider margin, more violations → higher bias, lower variance. Large : violations are expensive → narrow margin that contorts to fit every point → lower bias, higher variance. On separable data, approaches the hard-margin solution; on nonseparable data the hard-margin constraints are infeasible. Note the direction: works like an inverse regularization strength (sklearn agrees).
Hinge + L2 view. At the optimum with , so the whole thing is just regularized empirical risk:
Same recipe as L2-regularized logistic regression with hinge swapped for log loss. The hinge is exactly zero once — points comfortably outside the street contribute nothing, which is where sparsity comes from. Log loss is never exactly zero, so logistic regression listens to every point.
Support vectors. Solving via Lagrange duality gives one weight per training point:
Most points get . The support vectors — on the margin, inside it, or misclassified — are the only ones with , and depends only on them. Delete any other point and retrain: identical boundary.
Why kernels work. Look at the dual and the prediction : the data enters only through inner products. So replace every with a kernel and you have trained a linear separator in -space without ever computing . Concrete proof that this is real — in 2-D, the degree-2 polynomial kernel:
with — a 3-D inner product computed with one 2-D dot product and a square. For the RBF kernel the implicit space is infinite-dimensional; large means similarity dies off over tiny distances → wiggly, memorizing boundary (overfit); small → everything looks similar, boundary approaches linear (underfit). and interact, so tune them jointly on a log grid.
Scaling is mandatory. Kernels are built on distances and inner products; a feature measured in thousands drowns one measured in units, so standardize before fitting — this is not optional polish, it changes the geometry.
Against logistic regression: SVM optimizes a margin, logistic a likelihood; hinge gives no native probabilities (Platt scaling fits a sigmoid to SVM scores on held-out folds); logistic uses all points, SVM only support vectors. SVMs still win on small-to-medium with complex boundaries, and linear SVMs on high-dimensional sparse data like text, where and the margin story shines. Past a few hundred thousand rows, kernel training at to is off the table.
Worked example
Take , , so and . The street edges are and the margin width is
Score five points:
| Point | hinge | status | |||
|---|---|---|---|---|---|
| on margin edge — support vector | |||||
| on margin edge — support vector | |||||
| far outside street — ignorable | |||||
| inside street, correct side — support vector | |||||
| misclassified — support vector |
Check : , and its distance to the boundary is — exactly half the street width, as an on-margin point should be. is correctly classified yet still pays for standing inside the street; pays (crossing the boundary costs more than 1). Total hinge , and , so with the objective is . If this were the trained solution for these points, deleting would change nothing, while deleting would let the street rotate and widen.
Now drag it yourself: rotate until the classes separate and the hinge loss hits zero, then stretch and watch the street narrow while the loss stays zero — the tension between those two moves is the SVM objective.
Drag the pink tip of w to rotate the boundary until every ringed point escapes the band and hinge loss hits 0 — the green point slides the boundary itself (it sets b). Then stretch w longer: the loss stays 0 while the margin band collapses, which is exactly why SVMs minimize ‖w‖.
Cheat sheet
Primal (soft margin): s.t. , . Margin width .
Hinge form: — hinge + L2, the analogue of penalized logistic regression.
Dual / prediction: weights ; — data appears only via ; only support vectors have .
| Kernel | Notes | |
|---|---|---|
| Linear | text, , huge sparse data; fastest | |
| Polynomial | interactions up to degree ; scale-sensitive | |
| RBF | default nonlinear choice; infinite-dim feature space |
| Knob | Increase it | Decrease it |
|---|---|---|
| narrow margin, fewer violations — variance up | wide margin, more violations — bias up | |
| (RBF) | hyper-local similarity, wiggly boundary — variance up | near-linear boundary — bias up |
| Symptom | Likely cause | Fix |
|---|---|---|
| Train near-perfect, validation poor | and/or too large | shrink both; joint log-grid CV |
| Train and validation both poor | underfitting — , too small or kernel too simple | grow them; try RBF over linear |
| Bizarre boundary, one feature dominates | unscaled features | standardize (mandatory, distance-based) |
| Nearly all points are support vectors | memorization or heavy class overlap | shrink , shrink , revisit features |
| Training crawls at large | kernel SVM is to | linear SVM / SGD hinge, or different model |
| Need probabilities | hinge outputs scores only | Platt scaling or isotonic, fit on held-out folds |
| SVM | Logistic regression | |
|---|---|---|
| Loss | hinge — zero beyond margin | log loss — never zero |
| Optimizes | margin geometry | Bernoulli likelihood |
| Output | score , no native probability | probability (roughly calibrated) |
| Solution shaped by | support vectors only | every point |
| Nonlinearity | kernels built in | manual feature engineering |
| Very large | kernel: poor; linear: fine | fine |
- Sweet spot: small/medium + complex boundary (RBF), or high-dim sparse features like tf-idf text (linear).
- Platt-scaled probabilities are a second model — sklearn predict and the argmax of predict_proba can disagree.
- Multiclass is bolted on: one-vs-rest or one-vs-one under the hood.