Sub topic 1
Linear classifiers and separating hyperplanes
Prerequisites
This chapter assumes you already know the following:
- Basic supervised learning: features, labels, and the split into training and test sets.
- Vectors, dot products, and the equation of a line or plane.
- Python and NumPy basics.
You do not need calculus yet. The optimization material starts in later chapters. Here we build the geometric picture that everything else in the tutorial depends on.
What a linear classifier does
A linear classifier predicts a class from a weighted sum of the features. For a feature vector x with d entries, it computes a score:
f(x) = w · x + bw is the weight vector, with one weight per feature. b is a scalar called the bias or intercept. The predicted label is the sign of the score. In this tutorial the two classes are labeled -1 and +1.
prediction = +1 if f(x) >= 0, otherwise -1A positive weight means "a larger value of this feature pushes the score toward +1". A negative weight pushes toward -1. Training means choosing w and b so the signs match the labels on the training data.
The separating hyperplane
The set of points where the score is exactly zero is the decision boundary:
{ x : w · x + b = 0 }In two dimensions this set is a line. In three dimensions it is a plane. In d dimensions it is a hyperplane, a flat surface with d - 1 dimensions. The hyperplane splits the space into two half-spaces. One half has f(x) > 0 and the other has f(x) < 0.
Two geometric facts matter for the rest of the tutorial.
- The vector
wis perpendicular (normal) to the hyperplane. It points toward the positive side. - The signed distance from a point
xto the hyperplane isf(x) / ||w||, where||w||is the Euclidean length ofw.
The score itself is not a distance. It is a distance multiplied by ||w||. This is why the SVM later fixes the scale of w with a normalization convention.
| Quantity | Meaning |
|---|---|
| `w` | Normal vector, perpendicular to the hyperplane |
| `b` | Shifts the hyperplane along `w`. The origin's score is `b` |
| `f(x)` | Score. Its sign gives the class |
| `f(x) / ‖w‖` | Signed distance from `x` to the hyperplane |
| `y · f(x)` | Positive when the prediction is correct, negative when wrong |
The last row uses a useful trick. When labels are -1 or +1, the product y · f(x) is positive exactly when the point is on the correct side. Later chapters build the margin and the hinge loss from this product.
A worked example
Take w = (2, -1) and b = -1. The code below scores three points, converts the scores to labels, and computes distances.
import numpy as np
w = np.array([2.0, -1.0])
b = -1.0
X = np.array([[2.0, 1.0], [0.0, 2.0], [1.0, 1.0]])
scores = X @ w + b
labels = np.where(scores >= 0, 1, -1)
distances = scores / np.linalg.norm(w)
print("scores: ", scores)
print("labels: ", labels)
print("distances:", np.round(distances, 3))The scores are 2, -3, and 0. The labels are +1, -1, and +1. The signed distances are about 0.894, -1.342, and 0.
The third point lies exactly on the hyperplane. Its score is zero, so the >= 0 rule gives it +1. That choice is a convention. Points on the boundary are ambiguous, which is one reason the SVM prefers boundaries that keep points away from it.
Linear separability
A dataset is linearly separable if some w and b satisfy y_i (w · x_i + b) > 0 for every training point. Then a hyperplane classifies the whole training set correctly.
Separability depends on the representation. Points on a line labeled "inside a small interval" versus "outside it" are not separable in one dimension. Adding a second feature x² makes them separable in two dimensions. The kernel trick later in this tutorial exploits this idea without computing the extra features.
Real data often has noise or mislabeled points, so no perfectly separating hyperplane exists. Soft-margin SVMs handle this case. For now, assume separable data.
Many hyperplanes fit the same data
When data is separable, infinitely many hyperplanes classify the training set perfectly. Rotating or shifting a separating hyperplane slightly keeps every training point on the correct side. These hyperplanes can behave very differently on new points near the boundary.
The perceptron is the classic algorithm that finds one of them. It sweeps through the data. For each misclassified point, it nudges the hyperplane toward that point's correct side:
import numpy as np
def perceptron(X, y, max_epochs=100):
# y must contain labels in {-1, +1}
w = np.zeros(X.shape[1])
b = 0.0
for epoch in range(max_epochs):
errors = 0
for xi, yi in zip(X, y):
if yi * (xi @ w + b) <= 0:
w = w + yi * xi
b = b + yi
errors += 1
if errors == 0:
return w, b, epoch
return w, b, max_epochs
X = np.array([[2.0, 2.0], [3.0, 3.0], [3.0, 1.0],
[-1.0, -1.0], [-2.0, 0.0], [0.0, -2.0]])
y = np.array([1, 1, 1, -1, -1, -1])
w, b, epochs = perceptron(X, y)
pred = np.where(X @ w + b >= 0, 1, -1)
print("w:", w, "b:", b, "epochs:", epochs)
print("all correct:", bool((pred == y).all()))The perceptron convergence theorem guarantees that this loop stops after a finite number of updates when the data is separable. It prints all correct: True for this dataset.
The result depends on the order of the data and the starting point. A different ordering gives a different hyperplane. The perceptron has no preference among the valid hyperplanes. It stops at the first one it finds. If the data is not separable, it never stops on its own and only the max_epochs limit ends the loop.
Common errors
- Forgetting the bias. Without
b, the hyperplane must pass through the origin. Data that is separable by a shifted hyperplane may not be separable by one through the origin. - Reading the score as a distance.
f(x)equals the distance only when||w|| = 1. Divide by||w||for a real distance. - Mixing label conventions. Using
0/1labels in a formula that expects-1/+1makesy · f(x)meaningless, since0 · f(x)is always zero. - Assuming a unique solution. On separable data, many hyperplanes reach zero training error. Training error alone cannot tell them apart.
The last point leads to the next chapter. If every separating hyperplane has the same training error, we need another criterion to pick one. The SVM's answer is to choose the hyperplane that stays farthest from the nearest training points.
Exercise
A classifier has w = (3, 4) and b = -10. Compute the score and signed distance to the hyperplane for the point x = (4, 2). Which class is predicted?
Show answer
The score is 3·4 + 4·2 - 10 = 12 + 8 - 10 = 10. The norm of w is sqrt(9 + 16) = 5, so the signed distance is 10 / 5 = 2. The score is positive, so the predicted class is +1.
Exercise
The perceptron is run on a dataset that is not linearly separable, with max_epochs set to 100. What does it return, and why can no weight vector make every epoch error-free?
Show answer
It runs all 100 epochs and returns the weights from the last epoch, because the check errors == 0 is never satisfied. No hyperplane classifies every training point correctly, so every sweep contains at least one misclassified point and triggers an update.