Sub topic 1
Linear Classifiers and Separating Hyperplanes
Prerequisites
Assume you know features, labels, training and test sets, dot products, hyperplanes, and basic optimization.
Linear classifiers and separating hyperplanes
A linear classifier predicts a class from the sign of a linear score:
import numpy as np
w = np.array([2.0, -1.0])
b = 0.5
x = np.array([1.0, 2.0])
score = w @ x + b
prediction = 1 if score >= 0 else -1
print(score, prediction)The decision boundary is the hyperplane where the score is zero:
w · x + b = 0
In two dimensions, this is a line. The vector w is perpendicular to the line, and b moves the line. Many lines may separate two classes correctly.
Why the margin matters
The margin is the distance from the boundary to the nearest training points. A larger margin usually gives better generalization because small changes in the data are less likely to change predictions.
For labels yᵢ ∈ {-1, +1}, a convenient scaling imposes:
yᵢ(w · xᵢ + b) ≥ 1
The geometric margin on each side is 1 / ||w||, so maximizing the margin is equivalent to minimizing ½||w||².
The hard-margin SVM
A hard-margin support vector machine assumes perfect linear separation. Its optimization problem is:
minimize ½||w||²
subject to:
yᵢ(w · xᵢ + b) ≥ 1 for every training point.
The constraints enforce correct classification with at least the chosen margin. If even one point overlaps the other class, this problem has no feasible solution.
Soft margins and slack variables
A soft-margin SVM allows violations by adding a slack variable ξᵢ ≥ 0:
yᵢ(w · xᵢ + b) ≥ 1 - ξᵢ
Here, ξᵢ = 0 means the point satisfies the margin. Values between zero and one indicate a point inside the margin, while values above one indicate misclassification.
The objective becomes:
minimize ½||w||² + C Σξᵢ
Controlling the trade-off with C
C controls the penalty for margin violations.
| C value | Typical behavior |
|---|---|
| Small | Wider margin and more violations |
| Large | Fewer violations and a tighter fit |
A very large C can overfit noisy data. A small C can underfit. Select it using cross-validation rather than the test set.
> callout note > The scale of C depends on feature scaling and the loss convention used by the implementation. Compare values only within a consistent pipeline.
Exercise
Why does minimizing ½||w||² maximize the margin?
Show answer
The margin is proportional to 1 / ||w||, so minimizing the squared norm increases the distance between the boundary and the closest points.