math.vinhnx.ca / machine learning / svm
Machine learning

Support vector machines

Of all the lines that separate two classes, the SVM picks the one with the widest empty street around it. The surprise is how few points decide where that street goes. Drag any point that isn’t circled and the boundary won’t move at all.

Decision function f(x)

click: add · shift-click: other class · drag · alt-click: delete

Sweeping C

drag to set C

Dual weights αᵢ

only support vectors are non-zero

The widest street

Label the classes $y_i = \pm 1$ and look for a line $\mathbf w^\top \mathbf x + b = 0$. Scale $\mathbf w$ so the nearest points satisfy $y_i(\mathbf w^\top \mathbf x_i + b) = 1$; then the street between the dashed lines has width $2/\|\mathbf w\|$. Making it widest means

$$\min_{\mathbf w, b}\ \tfrac12\|\mathbf w\|^2 \quad\text{subject to}\quad y_i(\mathbf w^\top \mathbf x_i + b) \ge 1 - \xi_i,\ \ \xi_i \ge 0,$$

plus a penalty $C\sum_i \xi_i$ for points allowed inside the street or on the wrong side. Small $C$ buys a wide, forgiving street; large $C$ insists on classifying every training point. The sweep plot shows that trade-off.

Why only a few points matter

The Lagrangian dual of this problem is

$$\max_{\boldsymbol\alpha}\ \sum_i \alpha_i - \tfrac12\sum_{i,j}\alpha_i\alpha_j y_i y_j\, K(\mathbf x_i, \mathbf x_j), \qquad 0 \le \alpha_i \le C,\ \ \sum_i \alpha_i y_i = 0,$$

with the classifier $f(\mathbf x) = \sum_i \alpha_i y_i K(\mathbf x_i, \mathbf x) + b$. The KKT conditions force $\alpha_i = 0$ for every point strictly outside the street. Those points drop out of $f$ entirely. Only the support vectors (on the street’s edge, or inside it with $\alpha_i = C$) have a say. Move a non-support vector anywhere on its own side and the solution is exactly unchanged. Logistic regression, by contrast, listens to every point a little.

Try it: . Drag an uncircled point around its own side, then tick “compare with logistic regression” and drag again.

The kernel trick

The data enter the dual only through dot products $K(\mathbf x_i, \mathbf x_j)$. Swap in another kernel and you get a linear classifier in a different feature space without ever computing the features:

Try it: , then push γ to the top.

How it is solved here

The page runs SMO (sequential minimal optimisation) in your browser: repeatedly pick the pair $(\alpha_i, \alpha_j)$ that most violates the KKT conditions and solve that two-variable problem exactly. Two is the smallest number you can move while keeping $\sum_i \alpha_i y_i = 0$. It is the same algorithm LIBSVM uses, and it finishes in milliseconds for a few dozen points.