TExcelTAD
Home › Math for Machine Learning › Separating Hyperplanes and the Margin

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 yn∈{−1,+1}y_n \in \{-1, +1\}, and the classifier is

f(x)=wTx+b,y^=sign⁡(f(x))f(\mathbf{x}) = \mathbf{w}^T\mathbf{x} + b, \qquad \hat{y} = \operatorname{sign}(f(\mathbf{x}))

The decision boundary is the hyperplane f(x)=0f(\mathbf{x}) = 0 with normal vector w\mathbf{w}. A point is classified correctly when ynf(xn)>0y_nf(\mathbf{x}_n) > 0; the product ynf(xn)y_nf(\mathbf{x}_n) is the functional margin.

Distance to the hyperplane

The signed distance of x\mathbf{x} to the boundary is f(x)/∥w∥f(\mathbf{x})/\lVert \mathbf{w} \rVert (project x\mathbf{x} onto the unit normal). The geometric margin of a dataset is the smallest distance of any point:

r=min⁡nyn(wTxn+b)∥w∥r = \min_n \frac{y_n(\mathbf{w}^T\mathbf{x}_n + b)}{\lVert \mathbf{w} \rVert}

The hard-margin SVM

Scaling w\mathbf{w} and bb by the same factor does not change the hyperplane, so we may fix the scale by requiring yn(wTxn+b)≥1y_n(\mathbf{w}^T\mathbf{x}_n + b) \ge 1 for all nn, with equality for the closest points. Then the margin is 1/∥w∥1/\lVert \mathbf{w} \rVert, and maximizing it is minimizing ∥w∥2\lVert \mathbf{w} \rVert^2:

min⁡w,b 12∥w∥2subject toyn(wTxn+b)≥1 for all n\min_{\mathbf{w}, b}\ \frac{1}{2}\lVert \mathbf{w} \rVert^2 \quad \text{subject to} \quad y_n(\mathbf{w}^T\mathbf{x}_n + b) \ge 1 \ \text{for all } n

A quadratic program: convex, with a unique solution. The points with yn(wTxn+b)=1y_n(\mathbf{w}^T\mathbf{x}_n + b) = 1 are the support vectors. They alone determine the solution.

python · try it yourself
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") and LinearSVC; 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 w=(3,4)\mathbf{w} = (3, 4) and b=−10b = -10, what is the signed distance of (2,2)(2, 2) and of (0,0)(0, 0) to the boundary?

Answer

0.80.8 and −2-2.

Solution

∥w∥=5\lVert \mathbf{w} \rVert = 5. (6+8−10)/5=0.8(6 + 8 - 10)/5 = 0.8; (0−10)/5=−2(0 - 10)/5 = -2. The points lie on opposite sides.

Exercise 2: Margin from w\mathbf{w}

A hard-margin SVM ends with ∥w∥=0.25\lVert \mathbf{w} \rVert = 0.25. How wide is the corridor between the two margin hyperplanes f=±1f = \pm 1?

Answer
Solution

Each margin hyperplane is at distance 1/∥w∥=41/\lVert \mathbf{w} \rVert = 4 from the boundary, so the corridor is 2/∥w∥=82/\lVert \mathbf{w} \rVert = 8 wide.

Exercise 3: The 1-D SVM (code)

Points −3,−1-3, -1 (class −1-1) and 2,52, 5 (class +1+1) on a line. Find the maximum-margin threshold and check the constraints with w=2/3w = 2/3.

Answer

Threshold 0.50.5; f(x)=23x−13f(x) = \frac{2}{3}x - \frac{1}{3}.

Solution

The boundary sits halfway between the closest opposite points −1-1 and 22, at 0.50.5. The margin is 1.5=1/w1.5 = 1/w, so w=23w = \frac{2}{3} and b=−23⋅0.5=−13b = -\frac{2}{3} \cdot 0.5 = -\frac{1}{3}. The support vectors −1-1 and 22 have yf(x)=1y f(x) = 1.

python · try it yourself
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…

  1. maximizing ∥w∥\lVert\mathbf{w}\rVert
  2. minimizing 12∥w∥2\frac{1}{2}\lVert\mathbf{w}\rVert^2 subject to yn(wTxn+b)≥1y_n(\mathbf{w}^T\mathbf{x}_n + b) \ge 1
  3. minimizing the number of support vectors
  4. maximizing bb

Support vectors are…

  1. all training points
  2. the points that lie exactly on the margin hyperplanes and determine the solution
  3. the misclassified points only
  4. the cluster centres

The signed distance of x\mathbf{x} to the hyperplane wTx+b=0\mathbf{w}^T\mathbf{x} + b = 0 is…

  1. wTx+b\mathbf{w}^T\mathbf{x} + b
  2. (wTx+b)/∥w∥(\mathbf{w}^T\mathbf{x} + b)/\lVert\mathbf{w}\rVert
  3. ∥x∥\lVert\mathbf{x}\rVert
  4. b/∥w∥b/\lVert\mathbf{w}\rVert