SVMs & the Kernel Trick

core 12 minclassificationsvmkernelsmax-margin

Builds on:Logistic regressionDot product, norms, and similarityBias-variance tradeoff

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 w,bw, b so the street edges are w⊤x+b=±1w^{\top}x + b = \pm 1; the street (margin) width is then 2/∥w∥2/\|w\|, and maximizing it is the hard-margin problem:

min⁡w,b 12∥w∥2s.t.yi(w⊤xi+b)≥1,yi∈{−1,+1}\min_{w,b}\ \frac{1}{2}\|w\|^2 \quad \text{s.t.} \quad y_i(w^{\top}x_i + b) \ge 1,\qquad y_i \in \{-1,+1\}

Soft margin and C. Real data overlaps, so allow violations ξi≥0\xi_i \ge 0 (point ii may sit inside the street or on the wrong side) and charge CC per unit:

min⁡w,b,ξ 12∥w∥2+C∑iξis.t.yi(w⊤xi+b)≥1−ξi,  ξi≥0\min_{w,b,\xi}\ \frac{1}{2}\|w\|^2 + C\sum_i \xi_i \quad \text{s.t.} \quad y_i(w^{\top}x_i + b) \ge 1 - \xi_i,\ \ \xi_i \ge 0

Small CC: violations are cheap → wider margin, more violations → higher bias, lower variance. Large CC: violations are expensive → narrow margin that contorts to fit every point → lower bias, higher variance. On separable data, C→∞C \to \infty approaches the hard-margin solution; on nonseparable data the hard-margin constraints are infeasible. Note the direction: CC works like an inverse regularization strength (sklearn agrees).

Hinge + L2 view. At the optimum ξi=max⁡(0, 1−yif(xi))\xi_i = \max(0,\ 1 - y_i f(x_i)) with f(x)=w⊤x+bf(x) = w^{\top}x + b, so the whole thing is just regularized empirical risk:

min⁡w,b C∑imax⁡(0, 1−yif(xi))⏟hinge loss+12∥w∥2\min_{w,b}\ C\sum_i \underbrace{\max(0,\ 1 - y_i f(x_i))}_{\text{hinge loss}} + \frac{1}{2}\|w\|^2

Same recipe as L2-regularized logistic regression with hinge swapped for log loss. The hinge is exactly zero once yf(x)≥1y f(x) \ge 1 — 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 αi\alpha_i per training point:

max⁡α ∑iαi−12∑i,jαiαjyiyj⟨xi,xj⟩s.t.0≤αi≤C,  ∑iαiyi=0\max_{\alpha}\ \sum_i \alpha_i - \frac{1}{2}\sum_{i,j} \alpha_i \alpha_j y_i y_j \langle x_i, x_j \rangle \quad \text{s.t.} \quad 0 \le \alpha_i \le C,\ \ \sum_i \alpha_i y_i = 0

Most points get αi=0\alpha_i = 0. The support vectors — on the margin, inside it, or misclassified — are the only ones with αi>0\alpha_i > 0, and w=∑iαiyixiw = \sum_i \alpha_i y_i x_i depends only on them. Delete any other point and retrain: identical boundary.

Why kernels work. Look at the dual and the prediction f(x)=∑iαiyi⟨xi,x⟩+bf(x) = \sum_i \alpha_i y_i \langle x_i, x \rangle + b: the data enters only through inner products. So replace every ⟨x,z⟩\langle x, z \rangle with a kernel K(x,z)=⟨ϕ(x),ϕ(z)⟩K(x, z) = \langle \phi(x), \phi(z) \rangle and you have trained a linear separator in ϕ\phi-space without ever computing ϕ\phi. Concrete proof that this is real — in 2-D, the degree-2 polynomial kernel:

(x⊤z)2=(x1z1+x2z2)2=x12z12+2x1x2z1z2+x22z22=ϕ(x)⊤ϕ(z)(x^{\top}z)^2 = (x_1 z_1 + x_2 z_2)^2 = x_1^2 z_1^2 + 2 x_1 x_2 z_1 z_2 + x_2^2 z_2^2 = \phi(x)^{\top}\phi(z)

with ϕ(x)=(x12, 2 x1x2, x22)\phi(x) = (x_1^2,\ \sqrt{2}\,x_1 x_2,\ x_2^2) — a 3-D inner product computed with one 2-D dot product and a square. For the RBF kernel K(x,z)=exp⁡(−γ∥x−z∥2)K(x,z) = \exp(-\gamma \|x - z\|^2) the implicit space is infinite-dimensional; large γ\gamma means similarity dies off over tiny distances → wiggly, memorizing boundary (overfit); small γ\gamma → everything looks similar, boundary approaches linear (underfit). γ\gamma and CC 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 nn with complex boundaries, and linear SVMs on high-dimensional sparse data like text, where p≫np \gg n and the margin story shines. Past a few hundred thousand rows, kernel training at O(n2)O(n^2) to O(n3)O(n^3) is off the table.

Worked example

Take w=(1,1)w = (1, 1), b=−3b = -3, so f(x)=x1+x2−3f(x) = x_1 + x_2 - 3 and ∥w∥=2≈1.414\|w\| = \sqrt{2} \approx 1.414. The street edges are f(x)=±1f(x) = \pm 1 and the margin width is

2∥w∥=22=2≈1.414\frac{2}{\|w\|} = \frac{2}{\sqrt{2}} = \sqrt{2} \approx 1.414

Score five points:

Pointyyf(x)f(x)yf(x)y f(x)hinge max⁡(0, 1−yf)\max(0,\ 1 - y f)status
A=(1,1)A = (1, 1)−1-1−1-11100on margin edge — support vector
B=(2,2)B = (2, 2)+1+1+1+11100on margin edge — support vector
F=(3,3)F = (3, 3)+1+1+3+33300far outside street — ignorable
D=(2.5,1)D = (2.5, 1)+1+1+0.5+0.50.50.50.50.5inside street, correct side — support vector
E=(2,0.5)E = (2, 0.5)+1+1−0.5-0.5−0.5-0.51.51.5misclassified — support vector

Check AA: f=1+1−3=−1f = 1 + 1 - 3 = -1, and its distance to the boundary is ∣f∣/∥w∥=1/2≈0.707|f|/\|w\| = 1/\sqrt{2} \approx 0.707 — exactly half the street width, as an on-margin point should be. DD is correctly classified yet still pays ξ=0.5\xi = 0.5 for standing inside the street; EE pays 1.51.5 (crossing the boundary costs more than 1). Total hinge =0.5+1.5=2= 0.5 + 1.5 = 2, and 12∥w∥2=1\frac{1}{2}\|w\|^2 = 1, so with C=1C = 1 the objective is 1+1⋅2=31 + 1 \cdot 2 = 3. If this (w,b)(w, b) were the trained solution for these points, deleting F=(3,3)F = (3,3) would change nothing, while deleting BB would let the street rotate and widen.

Now drag it yourself: rotate ww until the classes separate and the hinge loss hits zero, then stretch ww and watch the street narrow while the loss stays zero — the tension between those two moves is the SVM objective.

Margin width 2/‖w‖
1.41
Total hinge loss
0.20
On/inside margin
2

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): min⁡12∥w∥2+C∑iξi\min \frac{1}{2}\|w\|^2 + C\sum_i \xi_i s.t. yi(w⊤xi+b)≥1−ξiy_i(w^{\top}x_i + b) \ge 1 - \xi_i, ξi≥0\xi_i \ge 0. Margin width 2/∥w∥2/\|w\|.

Hinge form: min⁡ C∑imax⁡(0, 1−yif(xi))+12∥w∥2\min\ C\sum_i \max(0,\ 1 - y_i f(x_i)) + \frac{1}{2}\|w\|^2 — hinge + L2, the analogue of penalized logistic regression.

Dual / prediction: weights 0≤αi≤C0 \le \alpha_i \le C; f(x)=∑iαiyiK(xi,x)+bf(x) = \sum_i \alpha_i y_i K(x_i, x) + b — data appears only via KK; only support vectors have αi>0\alpha_i > 0.

KernelK(x,z)K(x, z)Notes
Linearx⊤zx^{\top}ztext, p≫np \gg n, huge sparse data; fastest
Polynomial(x⊤z+r)d(x^{\top}z + r)^dinteractions up to degree dd; scale-sensitive
RBFexp⁡(−γ∥x−z∥2)\exp(-\gamma \Vert x - z \Vert^2)default nonlinear choice; infinite-dim feature space
KnobIncrease itDecrease it
CCnarrow margin, fewer violations — variance upwide margin, more violations — bias up
γ\gamma (RBF)hyper-local similarity, wiggly boundary — variance upnear-linear boundary — bias up
SymptomLikely causeFix
Train near-perfect, validation poorγ\gamma and/or CC too largeshrink both; joint log-grid CV
Train and validation both poorunderfitting — γ\gamma, CC too small or kernel too simplegrow them; try RBF over linear
Bizarre boundary, one feature dominatesunscaled featuresstandardize (mandatory, distance-based)
Nearly all points are support vectorsmemorization or heavy class overlapshrink γ\gamma, shrink CC, revisit features
Training crawls at large nnkernel SVM is O(n2)O(n^2) to O(n3)O(n^3)linear SVM / SGD hinge, or different model
Need probabilitieshinge outputs scores onlyPlatt scaling or isotonic, fit on held-out folds
SVMLogistic regression
Losshinge — zero beyond marginlog loss — never zero
Optimizesmargin geometryBernoulli likelihood
Outputscore f(x)f(x), no native probabilityprobability (roughly calibrated)
Solution shaped bysupport vectors onlyevery point
Nonlinearitykernels built inmanual feature engineering
Very large nnkernel: poor; linear: finefine