- An SVM maximizes the margin around a separating hyperplane.
- Only the support vectors determine the boundary; moving other points does not change it.
- Kernels replace inner products, letting a linear method operate in a richer feature space.
This lesson follows Naive Bayes & Gaussian Discriminant Analysis and leads into Nearest Neighbours & Decision Trees. The geometry uses Linear Algebra and Multivariable Calculus.
1. Many separating lines
If two classes are linearly separable, infinitely many lines can separate them. Which one should we choose? A line that barely misses a point is fragile: a small change in the data could put a point on the wrong side.
An SVM chooses the line with the largest margin — the widest empty band around the boundary. The boundary is still linear, but the objective is geometric rather than likelihood-based.
Before reading the formula, look at the two classes and ask: which points would you expect to determine the boundary? In the animation, only two points sit exactly on the margin.
2. The margin
A linear classifier is defined by a weight vector and a bias:
The boundary is f(x)=0. The margin lines are f(x)=±1. The distance between them is:
Maximizing the margin means minimizing ||w||. The hard-margin problem requires every point to be on the correct side of its margin:
The constraint is y_i f(x_i) ≥ 1 for every training point, with labels encoded as +1 and −1. Points exactly on the margin are the support vectors.
3. Worked example
Take w=(1,0) and b=0. The boundary is the vertical line x=0. The margin lines are x=−1 and x=1, and the margin width is:
The support vectors are (−1,0) with label 0 and (1,0) with label 1. Their signed margins are exactly 1.
Worked example: margins and hinge loss
| point | label y | f(x)=x | y·f(x) | hinge max(0,1−yf) |
|---|---|---|---|---|
| (1, 0) | +1 | 1 | 1 | 0 |
| (0.5, 0) | +1 | 0.5 | 0.5 | 0.5 |
| (−1, 0) | −1 | −1 | 1 | 0 |
| (0, 0) | −1 | 0 | 0 | 1 |
The total hinge loss is 1.5. The support vectors have zero hinge loss because they sit exactly on the margin. The point at the boundary is inside the margin and pays a loss of 1.
A soft-margin SVM allows such violations but charges for them. The parameter C controls the price.
4. Soft margins
Real data are rarely perfectly separable. A soft-margin SVM adds a slack penalty for points that violate the margin:
The hinge loss is max(0, 1 - y f(x)):
A large C punishes violations strongly and produces a narrower margin; a small C allows more violations and a wider, smoother boundary. C is a regularization knob, not a universal “best” value.
What happens to the margin as C becomes very large on noisy data?
5. Kernels
A linear boundary is not always enough. A kernel computes an inner product in a richer feature space without explicitly constructing that space:
The linear kernel is x·z. A polynomial kernel adds powers and interactions. The radial basis function (RBF) kernel compares distances:
The RBF kernel can produce curved boundaries in the original space. A small gamma makes each point’s influence broad; a large gamma makes it local and can overfit. The kernel trick is powerful, but it still needs scaling, validation, and a sensible C.
The final classifier is a weighted sum of kernel values at the support vectors, not a stored copy of every training point. That is the same sparsity idea as the linear margin, expressed in feature space.
6. Where it fails
SVMs are sensitive to feature scaling. A feature measured in thousands can dominate one measured in fractions. Standardize before fitting unless the scale is meaningful.
Training scales poorly with the number of examples, especially for nonlinear kernels. Probability estimates are often added by a separate calibration step and should not be assumed to be well calibrated. C and gamma can overfit just as easily as any other hyperparameters.
The next lesson switches from a global boundary to local neighborhoods and axis-aligned splits: nearest neighbors and decision trees.
Next on the recommended path: Nearest Neighbours & Decision Trees
7. Going deeper
The SVM objective is a convex quadratic program with linear constraints. The dual formulation depends on pairwise inner products, which is exactly why kernels can replace the inner product with K(x,z). The support vectors are the points with nonzero dual coefficients.
Hinge loss and logistic loss are both convex margin losses. Logistic regression also outputs a probability; an SVM outputs a decision value. The choice between them is often about calibration and optimization, not just accuracy.
Modern large-scale systems often use linear SVMs or kernel approximations for speed. The geometric idea remains useful even when the optimizer is a GPU kernel rather than a quadratic-programming solver.
8. Practice
For w=(2,0) and b=0, what is the margin width?
Why can a point far from the boundary be moved without changing the SVM?
What does a very large RBF gamma do to the boundary?
9. Resources
The scikit-learn SVM chapter covers kernels, C, gamma, scaling, and the optimization problem. The example page plots a maximum-margin hyperplane and its support vectors. Both links were opened and checked for this lesson.
Continue the track
The mathematics behind this topic: Linear algebra · Multivariable calculus.