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
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 (blue positive, orange negative), the solid line is the boundary and the dashed lines are the street's edges; ringed points are support vectors, and a double ring marks a point held at the penalty limit ().
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 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 . A linear SVM looks for with (every point off the street) and the widest street , i.e. the smallest . When the data cannot be separated, slack variables are added:
The dual problem
With Lagrange multipliers this becomes the dual (the problem this page actually solves):
The decision function is ; only points with (the support vectors) appear in the sum.
KKT conditions: three kinds of points
At the optimum every point is one of three kinds:
| Where it is | ||
|---|---|---|
| off the street, irrelevant | ||
| exactly on the edge (free support vector) | ||
| on the street or misclassified |
The bias comes from the free support vectors: they all satisfy , and averaging them is the stable choice.
SMO: two at a time
The constraint means no single can change on its own. SMO picks a pair : violates the KKT conditions most, and 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 . It stops when the violation falls below .
C and the margin
Overlapping clusters, linear kernel, various :
| Margin | Support vectors | Training accuracy | SMO steps | |
|---|---|---|---|---|
| 0.01 | 3.62 | 80 | 87% | 41 |
| 0.1 | 1.76 | 48 | 87% | 35 |
| 1 | 1.35 | 37 | 87% | 86 |
| 10 | 1.26 | 34 | 87% | 794 |
The larger , the less the SVM tolerates points on the street: the street narrows and the support vectors thin out, but convergence slows ( needs 7942 steps). Here training accuracy does not change with — in the overlap someone is misclassified whatever you draw.
The RBF kernel's γ
. With a large each point influences only a small neighbourhood and the boundary can bend sharply. On the circles, = 0.05, 0.5, 2 and 10 give 40, 29, 62 and 91 support vectors, all at 100% training accuracy — at 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. A few points decide
The default circles with an RBF kernel, run to convergence. Open the "KKT view".
2. A line cannot draw a circle
Switch to the linear kernel.
3. Lift to 3D and cut
Switch to the polynomial kernel (degree 2) and look at the 3D "Feature map".
4. C sets the street's width
Overlapping data, linear kernel, C = 0.01; then try C = 10.
5. γ too large
Back to the circles with RBF and γ = 10.
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
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
- Cortes, Vapnik, Support-vector networks, 1995: the soft-margin SVM.
- Platt, Sequential Minimal Optimization: A Fast Algorithm for Training Support Vector Machines, 1998: SMO.
- Fan, Chen, Lin, Working Set Selection Using Second Order Information for Training SVM, JMLR 2005: the working-set rule used here.
- Chang, Lin, LIBSVM: A library for support vector machines: the library behind scikit-learn's SVC.
- scikit-learn, SVC: this page's reference implementation.