Support Vector Machine

Find the widest street between two classes. Turn it yourself, remove points to see which ones matter, soften the margin with C and bend it with polynomial and RBF kernels.

1Find the widest street

Many lines separate the two classes. An SVM picks the one with the widest empty street on either side. The points on the edge of the street, ringed, are the support vectors: they alone decide where it goes.

2C: how strict the street is

Real data overlaps. A soft margin lets a few points sit inside the street or even on the wrong side, at a cost. A small C makes them cheap (a wide, relaxed street); a big C makes them expensive (a narrow street that bends to please every point).

up: x² + y²

3The kernel trick

No straight line splits a disc from the ring around it. Add a feature, distance² from the centre, and the inner points sit low while the ring sits high: now a flat cut works. Kernels do this without ever building the new features.

Start here

Find the widest street

Two classes that a straight line can separate, in fact many straight lines. Which is best? A support vector machine's answer: the one with the widest empty street between the classes, so new points have the most room for error. Turn your own street first, then compare with the SVM's. Click any point to remove it (click again to bring it back).

02468100246810(1.6, 2.4) teal(2.4, 1.2) teal(2.8, 3.4) teal(1.2, 4.2) teal(3.6, 2.2) teal(2.2, 5.6) teal(4, 4) teal(3.4, 0.9) teal(1, 1.4) teal(4.8, 1.8) teal(6.4, 7.2) amber(7.6, 6) amber(5.6, 8.4) amber(8.4, 8) amber(6.8, 4.8) amber(8.6, 5.4) amber(7.2, 9) amber(5.8, 6.4) amber(9, 7) amber(4.8, 7.4) amber
teal classamber classsupport vectorcentre line; dashed: the street's edges
Street
160°
Your width
0.82
Widest possible
2.71

For each direction, the street is as wide as the gap allows. Keep turning to find the widest.

04590135012widest

Street width for every angle of the line (across, in degrees). Gaps: no street at that angle separates the classes.

How it learns

Training with the hinge loss

Fitting an SVM is an optimisation problem: make |w| small (a wide street) while keeping points out of it. One simple way is gradient descent on the hinge loss. Each step only the points inside the street or on the wrong side (circled in red) push on the line; every other point contributes exactly nothing.

02468100246810(1.6, 2.4) teal(2.4, 1.2) teal(2.8, 3.4) teal(1.2, 4.2) teal(3.6, 2.2) teal(2.2, 5.6) teal(4, 4) teal(3.4, 0.9) teal(1, 1.4) teal(4.8, 1.8) teal(6.4, 7.2) amber(7.6, 6) amber(5.6, 8.4) amber(8.4, 8) amber(6.8, 4.8) amber(8.6, 5.4) amber(7.2, 9) amber(5.8, 6.4) amber(9, 7) amber(4.8, 7.4) amber
pushing on the line this stepthe current street
step 017
Hinge loss + ½λ|w|² after each shown epoch: 1.000
Epoch
0
Pushing
20 of 20
Step 0 / 21
700 ms

Press Start to train a linear SVM from w = 0 by gradient descent on the hinge loss.

Hinge loss

max(0, 1 − y·f(x))

Zero once a point is safely outside the street (y·f ≥ 1). So points far from the boundary stop mattering completely.

Compare: logistic regression

log(1 + e^(−y·f(x)))

Never exactly zero, so every point pulls a little, even the easy ones. Logistic regression →

Real data overlaps

The soft margin and C

Here the groups overlap and one amber point (the large one) has strayed toward the teal side. No street is empty, so the SVM allows points inside it, or on the wrong side, at a price set by C. Small C: violations are cheap, the street is wide and calm. Big C: every violation is expensive, so the street narrows and tilts toward the stray.

02468100246810(4.02, 4.05) teal(2.97, 4.52) teal(3.23, 2.41) teal(4.39, 3.69) teal(2.59, 4.84) teal(1.58, 4.59) teal(2.81, 4.04) teal(2.9, 4.89) teal(3.03, 4.43) teal(3.15, 3.4) teal(4.88, 4.53) teal(3.02, 4.2) teal(2.56, 3.08) teal(4.13, 1.82) teal(2.18, 5.33) teal(5.92, 5.21) teal(2.61, 3.52) teal(4.23, 3.5) teal(8.29, 5.21) amber(5.34, 5.41) amber(7.44, 5.86) amber(5.83, 5.93) amber(6.87, 5.51) amber(7.68, 8.17) amber(5.8, 6.71) amber(7.05, 7.71) amber(7.38, 6.72) amber(6.63, 6.6) amber(6.76, 7) amber(7.37, 7.44) amber(4.44, 6.9) amber(8.57, 5.64) amber(4.32, 6.66) amber(4.77, 7.98) amber(6.51, 8.05) amber(7.87, 7.44) amber(4.2, 4.8) amber
support vector (on or inside the street, or misclassified)the stray (4.2, 4.8)
1.0
Street width
2.13
Support vectors
7
Training accuracy
95%
New points (200)
96%

In between: a balance of width and violations. In practice C is chosen by cross-validation.

Curved boundaries

The kernel trick

An amber disc inside a teal ring. No straight street can split them. But an SVM only ever uses the data through dot products xᵢ·xⱼ, so replace the dot product with a kernel K(xᵢ, xⱼ): a similarity that acts like a dot product in a bigger, invented feature space. The street is straight there and curved here.

-3-2-10123-3-2-10123(0.82, 0.42) amber(1.1, 1.77) teal(0.58, 0.34) amber(1.69, -1.59) teal(-0.27, -0.52) amber(1.71, 0.12) teal(0.15, -1.23) amber(-1.45, 1.59) teal(-0.98, 0.13) amber(0.52, -2.24) teal(-0.78, 0.8) amber(-1.71, 0.39) teal(0.52, -0.67) amber(-0.66, -2.04) teal(-0.28, -1.16) amber(2.13, 0.8) teal(-0.26, 0.38) amber(-1.83, -0.62) teal(0.41, 0.78) amber(0.31, -2.48) teal(-0.15, -0.37) amber(2.6, -0.16) teal(1.27, -0.41) amber(2.1, -0.77) teal(-0.61, -0.65) amber(2.11, 0.67) teal(0.76, -0.63) amber(-0.43, 1.77) teal(1.25, 0.04) amber(0.71, -1.33) teal(0.43, 1.13) amber(-1.37, -0.73) teal(0.95, 0.64) amber(-2.42, 0.04) teal(-0.34, -0.63) amber(-0.83, 1.37) teal(0.85, 0.46) amber(1.18, -1.29) teal(0.66, 0.28) amber(1.39, 1.43) teal(-0.84, -0.26) amber(1.74, -1.36) teal(-0.19, 1) amber(0.72, 1.97) teal(-0.45, -0.2) amber(1.8, 0.27) teal(1.28, 0.12) amber(-2.16, -0.57) teal(0.41, -0.43) amber(-2.12, -1.1) teal(0.6, 1.04) amber(1.28, -1.54) teal(-0.12, 0.27) amber(-1.64, 0.57) teal(-1.12, 0.36) amber(-0.79, -1.44) teal(-0.09, -0.21) amber(-2.21, 1.6) teal(-0.23, -0.5) amber(-0.43, -2.11) teal(-0.61, 0.13) amber(2.04, 0.99) teal(0.72, -0.92) amber(1.8, 0.84) teal(-0.29, -0.06) amber(-0.66, -2.12) teal(1.23, -0.19) amber(-2.68, 1.14) teal(-0.67, -0.38) amber(-2.24, -0.87) teal
support vectoramber sideteal side; faint shading: inside the street
Kernel
–
Training
63%
New points (300)
59%
Support vectors
68

Linear: K(u, v) = u·v. The best straight street still gets a third of the points wrong.

-2020369x² + y² = 2.6

The same points with one extra feature, x² + y², put on the up axis.

Why it works

Lift every point by its squared distance from the centre and the amber disc sits low while the teal ring sits high: a flat cut now separates them. The polynomial kernel gives the SVM those squared features without ever computing them; the RBF kernel does the same with infinitely many. That is the kernel trick.

Know its limits

The catch, and where SVMs shine

Scale your features

The street's width is a distance, so a feature in big units dominates, exactly as with KNN. Standardise first. KNN and scaling →

Slow on big data

Kernel SVMs compare every pair of points: training grows between n² and n³. Past about 100,000 rows, use a linear SVM or another model.

Two settings to tune

C and γ interact. The usual recipe is a grid search with cross-validation over powers of 10 for both. Cross-validation →

No probabilities, two classes

f(x) is a score, not a probability (Platt scaling fits a sigmoid on top). More than two classes means one-vs-rest or one-vs-one.

Where they shine: small to medium datasets with many features, such as text classification with word counts, bioinformatics and image features. Before deep learning, SVMs were the state of the art for many of these, and a linear SVM is still a strong, fast baseline.

No magic, just arithmetic

The maths, step by step

Two teal points, A (1, 2) and B (2, 1), and two amber points, P (3, 4) and Q (4, 3). The solver returns w = (0.5, 0.5) and b = -2.5. Check that it's right, use it, and then see what a kernel does.

  1. 1

    Every point is on the right side, at least 1 away

    The SVM's rule: f(x) = w·x + b, and every training point must have y·f(x) ≥ 1. Points with exactly 1 sit on the edge of the street.

    y·f(A) = −1 × (0.5·1 + 0.5·2 − 2.5) = 1.0000

    y·f(B) = −1 × (0.5·2 + 0.5·1 − 2.5) = 1.0000

    y·f(P) = +1 × (0.5·3 + 0.5·4 − 2.5) = 1.0000

    y·f(Q) = +1 × (0.5·4 + 0.5·3 − 2.5) = 1.0000

    All four are exactly 1: every point touches the street.

  2. 2

    How wide is the street?

    The edges are the lines f = −1 and f = +1. The distance between them is 2 ÷ |w|, so a smaller w means a wider street. Maximising the street means minimising |w|.

    |w| = √(0.5² + 0.5²) = 0.7071

    width = 2 ÷ 0.7071 = 2.8284

  3. 3

    Where w and b come from

    w is a weighted sum of the training points, w = Σ αᵢ yᵢ xᵢ, and the weights α are zero for every point except the support vectors. That is the sense in which only they matter.

    α = (A: 0.25, B: 0, P: 0.25, Q: 0)

    w = 0.25·(−1)·(1, 2) + 0.25·(+1)·(3, 4) = (0.5, 0.5)

    b = y − w·x at A = -1 − (0.5·1 + 0.5·2) = -2.5000

    (With four points all on the edge, the solver only needed two of them; B and Q get α = 0 and could move outward without changing anything.)

  4. 4

    Classify a new point

    Take (2, 4). The sign of f gives the class; the size says how far from the boundary it is.

    f(2, 4) = 0.5·2 + 0.5·4 − 2.5 = 0.5000

    Positive, so amber. But it's below 1, so it lies inside the street: the model isn't confident.

  5. 5

    The hinge loss

    For training with a soft margin, each point costs max(0, 1 − y·f(x)). Points safely outside the street cost nothing; points inside or on the wrong side cost more the further in they are.

    loss at teal (3, 2) = max(0, 1 − (−1)·0) = 1.0000

    loss at teal (0, 0) = max(0, 1 − (−1)·-2.5) = 0.0000

    Soft-margin SVM minimises ½|w|² + C × (sum of hinge losses): a wide street, traded against points that break it.

  6. 6

    The kernel trick, by hand

    The polynomial kernel K(u, v) = (u·v + 1)². For u = (1, 2) and v = (2, 1) it equals an ordinary dot product of six made-up features, [1, √2·a, √2·b, a², b², √2·a·b], without ever building them.

    (u·v + 1)² = (1·2 + 2·1 + 1)² = 25.0000

    φ(u)·φ(v) = 1·1 + 1.4142·2.8284 + 2.8284·1.4142 + 1·4 + 4·1 + 2.8284·2.8284 = 25.0000

    RBF, γ = 0.5 = exp(−0.5 × ((1 − 2)² + (2 − 1)²)) = 0.3679

    The RBF kernel is the same trick with infinitely many features: it scores how close two points are, from 1 (the same point) down toward 0.

Keep going

Resources

Hand-picked places to go deeper. They're all free to read or watch, and they open in a new tab.

How it works: Support Vector Machine

Step by step

  • Teal class (−1)
  • Amber class (+1)
  • Points pushing on the line

Press Start or Step: the step the animation is on lights up below.

  1. 1

    Start

    w = 0 and b = 0: every point is inside the (infinitely wide) street, so every point has a hinge loss of 1.

    w = 0; b = 0
  2. 2

    Find the violators

    Score each point with y·f(x). Those below 1 are inside the street or on the wrong side; only they contribute to the gradient.

    V = { i : yᵢ (w·xᵢ + b) < 1 }
  3. 3

    Step down the loss

    Shrink w a little (widening the street) and move w, b toward pushing the violators out.

    w -= lr (λ w − mean over V of yᵢ xᵢ); b += lr · mean over V of yᵢ
  4. 4

    Support vectors decide

    At the optimum, the points on or inside the street are the support vectors. Exact solvers such as SMO solve the dual problem, which also allows kernels.

    predict(x) = sign(w·x + b)

A support vector machine (SVM) separates two classes with the line (or plane) that leaves the widest empty street, the margin, between them. The decision is the sign of f(x) = w·x + b; the street's edges are f = ±1 and its width is 2 / |w|, so maximising the margin means minimising |w|.

Only the points on the edge of the street, the support vectors, decide where it goes: w is a weighted sum of them, and every other point has weight zero. Real data overlaps, so a soft margin allows violations at a cost: minimise ½|w|² + C·Σ max(0, 1 − y·f(x)), the hinge loss. Small C gives a wide, tolerant street; big C a narrow, strict one.

The kernel trick: the solution only uses dot products between points, so replacing them with a kernel K(u, v), such as the polynomial (u·v + 1)ᵈ or the RBF exp(−γ|u − v|²), fits a straight street in a bigger feature space, which is a curved boundary in the original one.

Where you'll meet it

  • Small to medium datasets with many features, such as text classification, where a clean margin generalises well.
  • Non-linear boundaries with an RBF kernel when you have thousands, not millions, of rows.
  • Interview favourites: “what is a support vector?”, “what do C and γ do?”, “explain the kernel trick”, “hinge loss vs logistic loss”, “why scale the features?”.
time O(n · d) per epoch (linear); O(n² to n³) for kernel SVMsspace O(d) linear; O(n²) kernel matrix

A linear SVM trained by (sub-)gradient descent touches each point once per epoch. A kernel SVM compares pairs of points, so training grows between quadratically and cubically with n; prediction costs one kernel evaluation per support vector.