Support vector machines
The maximum-margin classifier: hinge loss, soft margins, the C parameter and support vectors.
Animated explanation
The idea
When classes can be separated by a line, infinitely many lines do it. Which is best? The SVM's answer: the one that stays as far as possible from the nearest points of either class, like the centre line of the widest street you can build between them.
A wide margin is a buffer against noise: new points can wander a little and still land on the right side. Only the points on the edge of the street, the support vectors, determine the solution; every other point could move (outside the street) without changing anything.
Real data overlaps, so the soft-margin SVM lets some points violate the street at a cost each. trades margin width against training errors.
Geometry of the margin
The decision function is . Scale so the closest points satisfy ; then the street's edges are and its width is . Maximising the width is minimising subject to every point being on the correct side of its edge: a convex quadratic programme with a unique solution.
Soft margins and C
Slack variables let point violate its edge by , and the objective adds . Eliminating the slacks gives the hinge loss plus an L2 penalty: an SVM is regularised hinge-loss regression on labels. Small → wide street, more violations, lower variance. Large → narrow street, fewer training errors, risk of overfitting. Tune on a log scale by cross-validation, and standardise features: margins are measured in feature units.
The dual and support vectors
The Lagrangian dual expresses the solution as with . Points strictly outside the street get . The dual only ever uses dot products , which is exactly the opening for the kernel trick in the next lesson.
The algorithm, step by step
- 1Standardise features; encode labels as ±1.
- 2Solve the soft-margin problem (SMO for the dual, or subgradient / coordinate descent on the hinge loss for large linear problems).
- 3Support vectors: training points with (on or inside the margin).
- 4Predict ; tune C by cross-validation.
The maths
Primal (soft margin)
Margin width
Dual
Subgradient of the hinge loss
Strengths and weaknesses
Strengths
- Maximum margin generalises well, especially in high dimensions
- Convex: a unique global optimum
- Solution depends only on support vectors
- Kernels extend it to non-linear boundaries
Weaknesses
- No native probabilities
- Training the dual scales poorly beyond ~100k examples
- Needs feature scaling and tuning of C (and kernel parameters)
Reach for it when…
Check your understanding
Lambdian Lab
A linear SVM by subgradient descent
Minimise (labels ±1).
- hinge_loss(X, y, w, b, lam): the objective value.
- fit_svm(X, y, lam, epochs, lr): full-batch subgradient descent with a decaying step lr / ; return (w, b). Do not regularise b.
- support_vectors(X, y, w, b, tol): indices with tol.