Skip to content
Lambdian

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 CC each. CC trades margin width against training errors.

Geometry of the margin

The decision function is f(x)=w⊤x+bf(\mathbf{x}) = \mathbf{w}^\top\mathbf{x} + b. Scale w\mathbf{w} so the closest points satisfy ∣f(x)∣=1|f(\mathbf{x})| = 1; then the street's edges are f=±1f = \pm1 and its width is 2/∥w∥2/\lVert\mathbf{w}\rVert. Maximising the width is minimising ∥w∥2\lVert\mathbf{w}\rVert^2 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 ξi≥0\xi_i \ge 0 let point ii violate its edge by ξi\xi_i, and the objective adds C∑iξiC\sum_i\xi_i. Eliminating the slacks gives the hinge loss max⁡(0,1−yif(xi))\max(0, 1 - y_if(\mathbf{x}_i)) plus an L2 penalty: an SVM is regularised hinge-loss regression on labels. Small CC → wide street, more violations, lower variance. Large CC → narrow street, fewer training errors, risk of overfitting. Tune CC 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 w=∑iαiyixi\mathbf{w} = \sum_i\alpha_iy_i\mathbf{x}_i with 0≤αi≤C0\le\alpha_i\le C. Points strictly outside the street get αi=0\alpha_i = 0. The dual only ever uses dot products xi⊤xj\mathbf{x}_i^\top\mathbf{x}_j, which is exactly the opening for the kernel trick in the next lesson.

The algorithm, step by step

  1. 1
    Standardise features; encode labels as ±1.
  2. 2
    Solve the soft-margin problem (SMO for the dual, or subgradient / coordinate descent on the hinge loss for large linear problems).
  3. 3
    Support vectors: training points with αi>0\alpha_i > 0 (on or inside the margin).
  4. 4
    Predict sign⁡(w⊤x+b)\operatorname{sign}(\mathbf{w}^\top\mathbf{x} + b); tune C by cross-validation.

The maths

Primal (soft margin)

min⁡w,b 12∥w∥2+C∑imax⁡(0,1−yi(w⊤xi+b))\min_{\mathbf{w},b}\ \tfrac12\lVert\mathbf{w}\rVert^2 + C\sum_i\max(0, 1 - y_i(\mathbf{w}^\top\mathbf{x}_i + b))

Margin width

width=2∥w∥\text{width} = \frac{2}{\lVert\mathbf{w}\rVert}

Dual

max⁡α ∑iαi−12∑i,jαiαjyiyj xi⊤xj  s.t. 0≤αi≤C, ∑iαiyi=0\max_{\boldsymbol\alpha}\ \sum_i\alpha_i - \tfrac12\sum_{i,j}\alpha_i\alpha_jy_iy_j\,\mathbf{x}_i^\top\mathbf{x}_j\ \ \text{s.t.}\ 0\le\alpha_i\le C,\ \sum_i\alpha_iy_i = 0

Subgradient of the hinge loss

∂w=w−C∑i: yif(xi)<1yixi\partial_{\mathbf{w}} = \mathbf{w} - C\sum_{i:\,y_if(\mathbf{x}_i)<1} y_i\mathbf{x}_i

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…

Medium-sized datasets with many features (text, genomics), when a robust linear or kernel classifier is needed.

Check your understanding

Question 1

Q1Which training points determine the SVM boundary?

Question 2

Q2Increasing C in a soft-margin SVM…

Question 3

Q3The margin width of a linear SVM equals…

Lambdian Lab

A linear SVM by subgradient descent

Minimise λ2∥w∥2+1n∑imax⁡(0,1−yi(w⊤xi+b))\frac{\lambda}{2}\lVert w\rVert^2 + \frac1n\sum_i\max(0, 1 - y_i(w^\top x_i + b)) (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 ηt=\eta_t = lr / t\sqrt{t}; return (w, b). Do not regularise b.
  • support_vectors(X, y, w, b, tol): indices with yif(xi)≤1+y_if(x_i) \le 1 + tol.
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.