Sub topic 3
The hard-margin SVM
This chapter assumes you know vectors, dot products, the equation of a hyperplane, and basic constrained minimization. It also assumes you have seen why a wider margin generalizes better.
The setup
Training data is a set of pairs (xi, yi), where xi is a feature vector and yi is a label in {-1, +1}. A linear classifier predicts the sign of w·x + b. The vector w sets the orientation of the hyperplane, and b shifts it.
A point is classified correctly when yi (w·xi + b) > 0. The data is linearly separable if some (w, b) satisfies this for every point. The hard-margin SVM only exists for separable data.
Fixing the scale
Multiplying w and b by the same positive constant gives the same hyperplane. We remove this freedom by requiring the closest points to satisfy yi (w·xi + b) = 1. Every point then satisfies:
yi (w·xi + b) ≥ 1
The two hyperplanes w·x + b = +1 and w·x + b = -1 pass through the closest points of each class. The distance between them is 2 / ||w||. That distance is the margin.
The optimization problem
Maximizing 2 / ||w|| is the same as minimizing ||w||, and minimizing ½||w||² is more convenient because it is smooth and convex:
- Minimize ½ ||w||² over w and b
- Subject to yi (w·xi + b) ≥ 1 for every i
This is a convex quadratic program with linear constraints. It has a single global optimum, and the optimal w is unique.
Checking the solution in code
The next snippet approximates a hard margin by using a very large C. Scikit-learn's SVC always solves the soft-margin problem, and a large C penalizes violations so heavily that none occur on separable data.
import numpy as np
from sklearn.svm import SVC
X = np.array([[0.0, 0.0], [1.0, 0.0], [3.0, 3.0], [4.0, 3.0]])
y = np.array([-1, -1, 1, 1])
clf = SVC(kernel="linear", C=1e10).fit(X, y)
w, b = clf.coef_[0], clf.intercept_[0]
print("w:", w, "b:", b)
print("margin:", 2 / np.linalg.norm(w))
print("y(w.x+b):", y * (X @ w + b))The last line should print values that are at least 1, up to numerical error. The smallest ones, equal to 1, belong to the support vectors.
Common errors
- Non-separable data. If no hyperplane separates the classes, the constraints cannot all hold and the problem has no solution. The next chapter relaxes the constraints to fix this.
- Outlier sensitivity. One mislabeled or extreme point can move the boundary a lot, or make the problem infeasible.
- Labels in {0, 1}. The constraint form assumes labels of -1 and +1. Scikit-learn handles encoding internally, but hand-written objectives do not.
- Reading C=1e10 as exact. A huge C only approximates a hard margin and can cause slow convergence.
Exercise
A hard-margin SVM finds w = (3, 4). What is the margin width, and what is the value of y(w·x + b) at a support vector?
Show answer
The margin is 2 / ||w|| = 2 / 5 = 0.4. At a support vector, y(w·x + b) equals 1 exactly.
Exercise
Why do we minimize ½||w||² instead of maximizing the margin 2/||w|| directly?
Show answer
Maximizing 2/||w|| is equivalent to minimizing ||w||, and minimizing ½||w||² gives the same solution. The squared form is smooth and convex, so the problem is a quadratic program that standard solvers handle well.