Sub topic 1
Linear classifiers and separating hyperplanes
Prerequisites
This chapter assumes you know the basics of supervised learning: features, labels, and training and test sets. You should be comfortable with vectors, dot products, and NumPy. You do not need any optimization theory yet.
The linear decision rule
A linear classifier predicts a class from a weighted sum of the features. For a feature vector x, a weight vector w, and a bias b, it computes a score:
f(x) = w · x + b
The predicted label is the sign of the score. With labels in {-1, +1}, a positive score gives +1 and a negative score gives -1. The set of points where f(x) = 0 is the decision boundary.
In two dimensions the boundary is a line. In three it is a plane. In d dimensions it is a hyperplane, a flat subspace with d - 1 dimensions. The vector w is perpendicular to the hyperplane and points toward the +1 side. The bias b shifts the hyperplane away from the origin.
The size of the score carries information. The signed distance from a point x to the hyperplane is f(x) / ||w||. The sign tells you the side, and the magnitude tells you how far the point lies from the boundary. Later chapters build on this distance.
Computing a decision by hand
The code below applies a hand-picked hyperplane to three points. It prints the score, the signed distance, and the predicted label.
import numpy as np
w = np.array([2.0, 1.0])
b = -4.0
X = np.array([[3.0, 2.0], [1.0, 1.0], [2.0, 0.0]])
scores = X @ w + b
distances = scores / np.linalg.norm(w)
labels = np.where(scores >= 0, 1, -1)
for x, s, d, lab in zip(X, scores, distances, labels):
print(x, "score:", round(s, 2), "distance:", round(d, 2), "label:", lab)The point (2, 0) has score 0, so it sits exactly on the boundary. The code assigns it to +1 by convention.
Many hyperplanes can separate the same data
Suppose the two classes are linearly separable, meaning some hyperplane puts every +1 point on one side and every -1 point on the other. Then many such hyperplanes usually exist. You can rotate or shift a separating line slightly and it still classifies the training set perfectly.
Algorithms such as the perceptron stop at the first separating hyperplane they find. Which one they return depends on the starting weights and the order of the data. Those hyperplanes fit the training data equally well, but they can behave very differently on new points. A boundary that passes very close to a training point is fragile, because a small amount of noise can push a new point of the same class across it.
Common errors
- Forgetting the bias. Without b, the hyperplane must pass through the origin, which fits many datasets poorly.
- Mixing label conventions. The SVM math uses labels in {-1, +1}. Many datasets use {0, 1}, so convert them before applying formulas that multiply by y.
- Treating the score as a probability. The score is a signed distance scaled by ||w||. It is not bounded between 0 and 1.
- Assuming separability. Real data often overlaps, so no perfect hyperplane exists. Soft margins address this later in the tutorial.
The open question is how to choose one hyperplane out of the many that separate the data. The next chapter answers it with the margin.
Exercise
A hyperplane has w = (3, 4) and b = -5. For the point x = (3, 2), compute the score, the signed distance to the hyperplane, and the predicted label.
Show answer
The score is 33 + 42 - 5 = 12. The norm of w is sqrt(9 + 16) = 5, so the signed distance is 12 / 5 = 2.4. The score is positive, so the predicted label is +1.
Exercise
Why is a separating hyperplane that passes very close to a training point a poor choice, even though it has zero training error?
Show answer
A new point from the same class can lie slightly closer to the boundary than that training point. A small amount of noise can then put it on the wrong side, so the hyperplane is likely to generalize badly.