Separating Hyperplanes and the Margin
A linear classifier splits space with a hyperplane. When the classes can be separated at all, infinitely many hyperplanes do it. The support vector machine (SVM) picks the one with the widest empty corridor around it, the maximum margin, which tends to generalize best and leads to a clean convex optimization problem.
Linear classification
Labels are , and the classifier is
The decision boundary is the hyperplane with normal vector . A point is classified correctly when ; the product is the functional margin.
Distance to the hyperplane
The signed distance of to the boundary is (project onto the unit normal). The geometric margin of a dataset is the smallest distance of any point:
The hard-margin SVM
Scaling and by the same factor does not change the hyperplane, so we may fix the scale by requiring for all , with equality for the closest points. Then the margin is , and maximizing it is minimizing :
A quadratic program: convex, with a unique solution. The points with are the support vectors. They alone determine the solution.
import numpy as np
# two separable clusters; compare three separating lines by their geometric margin
X = np.array([[1.0, 1.0], [2.0, 1.5], [1.5, 3.0], [2.5, 2.5], # class +1
[4.0, 4.5], [5.0, 4.0], [4.5, 6.0], [6.0, 5.0]]) # class -1
y = np.array([1, 1, 1, 1, -1, -1, -1, -1])
candidates = {"steep": (np.array([-1.0, -0.3]), 4.0),
"shallow": (np.array([-0.3, -1.0]), 4.2),
"diagonal": (np.array([-1.0, -1.0]), 7.0)}
for name, (w, b) in candidates.items():
m = y * (X @ w + b) / np.linalg.norm(w)
print(f"{name:8s}: all correct {bool(np.all(m > 0))}, geometric margin {m.min():.3f}")steep : all correct True, geometric margin 0.718
shallow : all correct True, geometric margin 0.718
diagonal: all correct True, geometric margin 1.061
All three lines separate the data, but the diagonal one leaves the widest corridor. The SVM finds the best such line automatically (Soft Margin and the Hinge Loss trains one).
Why a large margin?
Intuitively, a wide margin leaves room for test points that differ slightly from training points. Formally, generalization bounds for linear classifiers depend on the margin relative to the data radius, not on the number of features, which is why SVMs work well even in very high dimensions (text classification with tens of thousands of features).
In software
sklearn.svm.SVC(kernel="linear")andLinearSVC;support_vectors_lists the support vectors (SVM).- Logistic regression also gives a linear boundary but uses all points, weighted by how uncertain they are.
- The perceptron finds a separating hyperplane, not the maximum-margin one (The Perceptron).
Exercises
Exercise 1: Distance to the boundary
For and , what is the signed distance of and of to the boundary?
Answer
and .
Solution
. ; . The points lie on opposite sides.
Exercise 2: Margin from
A hard-margin SVM ends with . How wide is the corridor between the two margin hyperplanes ?
Answer
Solution
Each margin hyperplane is at distance from the boundary, so the corridor is wide.
Exercise 3: The 1-D SVM (code)
Points (class ) and (class ) on a line. Find the maximum-margin threshold and check the constraints with .
Answer
Threshold ; .
Solution
The boundary sits halfway between the closest opposite points and , at . The margin is , so and . The support vectors and have .
import numpy as np
x = np.array([-3.0, -1, 2, 5]); y = np.array([-1, -1, 1, 1])
w, b = 2 / 3, -1 / 3
print(np.round(y * (w * x + b), 4))[2.3333 1. 1. 3. ]
Test yourself
The hard-margin SVM maximizes the margin by…
- maximizing
- minimizing subject to
- minimizing the number of support vectors
- maximizing
Support vectors are…
- all training points
- the points that lie exactly on the margin hyperplanes and determine the solution
- the misclassified points only
- the cluster centres
The signed distance of to the hyperplane is…