Support vector machine

A support vector machine solved live by SMO, one α-pair per step: margins, support vectors, linear, RBF and polynomial kernels, the KKT conditions and the explicit kernel feature map in 3D.

Updated

  • Maximum margin and soft margin
  • The dual problem and support vectors
  • SMO, step by step
  • Kernels and the kernel trick
  • The penalty C
SMO iter 1 · 2/‖w‖ = 2.77SMO iter 2 · 2/‖w‖ = 1.81SMO iter 3 · 2/‖w‖ = 2.41SMO iter 5 · 2/‖w‖ = 1.63SMO iter 8 · 2/‖w‖ = 1.65SMO iter 11 · 2/‖w‖ = 1.65SMO iter 14 · 2/‖w‖ = 1.65SMO iter 17 · 2/‖w‖ = 1.65max 2/‖w‖
Preparing the lab…

Intuition

The widest street

Many lines separate two classes. Which is best? The SVM's answer: the one with the widest empty "street" on both sides. A few points sit exactly on the street's edges — the support vectors; move any other point around and, as long as it stays off the street, nothing changes.

The three small labs below really run SMO. The background is the decision function f(x)f(x) (blue positive, orange negative), the solid line f=0f = 0 is the boundary and the dashed lines f=±1f = \pm1 are the street's edges; ringed points are support vectors, and a double ring marks a point held at the penalty limit (α=C\alpha = C).

Two clusters: linear kernelPreparing…
Circles: linear kernelPreparing…
Circles: RBF kernelPreparing…

In the first the two clusters are separable: SMO converges in 36 steps, only 7 of the 100 points are support vectors, and the street is 2/∥w∥≈0.962/\|w\| \approx 0.96 wide.

In the second one ring sits inside another. A linear kernel can only draw straight lines and none of them work: 76% training accuracy, with 88 points as support vectors — nearly every point is "protesting".

The third switches to an RBF kernel: the boundary curls into a ring, SMO converges in 233 steps, accuracy is 100%, and only 29 support vectors are needed.

Math

Hard and soft margins

Labels are yi=±1y_i = \pm1. A linear SVM looks for w,bw, b with yi(w⋅xi+b)≥1y_i(w\cdot x_i + b) \ge 1 (every point off the street) and the widest street 2/∥w∥2/\|w\|, i.e. the smallest 12∥w∥2\tfrac12\|w\|^2. When the data cannot be separated, slack variables ξi\xi_i are added:

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

The dual problem

With Lagrange multipliers αi\alpha_i this becomes the dual (the problem this page actually solves):

max⁡α W(α)=∑iαi−12∑i,jαiαjyiyjK(xi,xj)s.t.0≤αi≤C,  ∑iαiyi=0\max_\alpha\ W(\alpha) = \sum_i \alpha_i - \frac12\sum_{i,j}\alpha_i\alpha_j y_iy_j K(x_i, x_j) \quad\text{s.t.}\quad 0 \le \alpha_i \le C,\ \ \sum_i \alpha_i y_i = 0

The decision function is f(x)=∑iαiyiK(xi,x)−ρf(x) = \sum_i \alpha_i y_i K(x_i, x) - \rho; only points with αi>0\alpha_i > 0 (the support vectors) appear in the sum.

KKT conditions: three kinds of points

At the optimum every point is one of three kinds:

αi\alpha_iWhere it isyif(xi)y_i f(x_i)
00off the street, irrelevant≥1\ge 1
0<αi<C0 < \alpha_i < Cexactly on the edge (free support vector)=1= 1
CCon the street or misclassified≤1\le 1

The bias ρ\rho comes from the free support vectors: they all satisfy yif(xi)=1y_i f(x_i) = 1, and averaging them is the stable choice.

SMO: two at a time

The constraint ∑iαiyi=0\sum_i\alpha_i y_i = 0 means no single α\alpha can change on its own. SMO picks a pair (i,j)(i, j): ii violates the KKT conditions most, and jj is chosen with second-order information to increase the objective the most (Fan, Chen and Lin 2005, libsvm's rule). The two-variable subproblem has an analytic solution, which is then clipped back into the box [0,C][0, C]. It stops when the violation m(α)−M(α)m(\alpha) - M(\alpha) falls below 10−1010^{-10}.

C and the margin

Overlapping clusters, linear kernel, various CC:

CCMargin 2/∥w∥2/\|w\|Support vectorsTraining accuracySMO steps
0.013.628087%41
0.11.764887%35
11.353787%86
101.263487%794

The larger CC, the less the SVM tolerates points on the street: the street narrows and the support vectors thin out, but convergence slows (C=100C = 100 needs 7942 steps). Here training accuracy does not change with CC — in the overlap someone is misclassified whatever you draw.

C = 0.01: a wide streetPreparing…
C = 10: a narrow streetPreparing…
RBF γ = 10: 91 support vectorsPreparing…

The RBF kernel's γ

K(x,x′)=exp⁡(−γ∥x−x′∥2)K(x, x') = \exp(-\gamma\|x - x'\|^2). With a large γ\gamma each point influences only a small neighbourhood and the boundary can bend sharply. On the circles, γ\gamma = 0.05, 0.5, 2 and 10 give 40, 29, 62 and 91 support vectors, all at 100% training accuracy — at γ=10\gamma = 10 nearly every point draws a little circle around itself, a sign of overfitting.

How it is computed, with real numbers

Once the lab above has loaded, this panel works through the same formulas with its current numbers.

Experiment

Follow the lesson

Course

Lesson 1: margins, support vectors and kernels

5 steps · each one: set up and run → look → answer

  1. 1. A few points decide

    The default circles with an RBF kernel, run to convergence. Open the "KKT view".

    Only 29 of 100 points are support vectors. What do the other 71 do?
  2. 2. A line cannot draw a circle

    Switch to the linear kernel.

    Why does accuracy fall to 76% with 88 support vectors?
  3. 3. Lift to 3D and cut

    Switch to the polynomial kernel (degree 2) and look at the 3D "Feature map".

    Why can a single plane separate the rings in 3D?
  4. 4. C sets the street's width

    Overlapping data, linear kernel, C = 0.01; then try C = 10.

    Raising C from 0.01 to 10 narrows the street. Why?
  5. 5. γ too large

    Back to the circles with RBF and γ = 10.

    Accuracy is still 100%. Why is this a bad sign?

Try these settings

Exercises

Exercises

Exercise 1: separate the circles with the fewest support vectors

100% training accuracy with at most 5 support vectors.

Hint

The default RBF needs 29. Switch to the quadratic polynomial kernel: 5 — in its feature space a single plane suffices.

  • Accuracy ≥ 100.0%
  • Support vectors ≤ 5

Exercise 2: narrow the street

Change only C. At most 35 support vectors, accuracy at least 87%.

Hint

C = 1 gives 37 and C = 10 gives 34, at the cost of 794 SMO steps.

  • Support vectors ≤ 35
  • Accuracy ≥ 87.0%

Exercise 3: a lean solution to XOR

The default RBF reaches 99% but uses 44 support vectors. Keep 99% with no more than 15.

Hint

The quadratic polynomial kernel: 12 support vectors. Its x₁x₂ term is exactly XOR's sign.

  • Accuracy ≥ 99.0%
  • Support vectors ≤ 15

Quiz

A point has α = 0. What happens if you move it (without crossing the street's edge)?
What usually happens as C increases?
What is the key to the kernel trick?

Further reading

Frequently asked questions

What are the “support vectors” in a support vector machine?

The training points that end with α > 0: those on the margin's edge, inside the margin, or misclassified. The decision function depends only on them; remove every other point and the result is unchanged.

What does the SVM parameter C do?

C is the penalty for crossing the margin. Small C allows more violations, giving a wider, smoother margin; large C tries hard to classify every training point, giving a narrow margin and possible overfitting. On this page's overlapping data, C from 0.01 to 10 shrinks the margin from 3.62 to 1.26.

How do I choose γ for the RBF kernel?

γ sets each point's reach (about 1/√γ). Too small and the boundary is nearly straight; too large and each point only covers itself, giving many support vectors and overfitting. Usually tune it together with C by cross-validated grid search; scikit-learn's default is γ = 1/(number of features × variance).

How does an SVM differ from logistic regression?

Both are linear classifiers (not limited to linear with kernels). Logistic regression minimises cross-entropy, outputs probabilities, and every point affects the result; the SVM minimises hinge loss plus ‖w‖², depends only on its support vectors, and gives no probabilities directly. SVMs often do better with few samples, many features or kernels; use logistic regression when you need probabilities.

What is the SMO algorithm?

Platt's 1998 method for solving the SVM dual: optimise just two α at a time (the equality constraint means at least two must change), solve the subproblem analytically, and avoid a general quadratic-programming solver. libsvm and scikit-learn's SVC use refined versions of it.

Further reading