The Problem
Decision trees divide the world into a series of yes-or-no questions. Random Forests average many trees to reduce variance. Gradient Boosting adds trees sequentially to correct previous mistakes.
Those methods are powerful, but they can produce irregular decision boundaries. Sometimes the data is simpler than that.
Imagine predicting whether Jessie orders a burger using only two features: hours since last meal and burger price. The burger examples sit mostly above the no-burger examples. A straight line separates them almost perfectly.
Burger o o o o
o o o
----------------------------
No Burger x x x x
x xDo we need an entire forest of trees? Probably not. We can ask for the best possible separating line. That is the motivation behind Support Vector Machines.
Core Intuition
Suppose several different lines classify every training example correctly. At first, they seem equally good. SVMs argue they are not.
Instead of asking which line separates the training data, an SVM asks which line leaves the largest safety margin.
The margin is the empty buffer zone around the decision boundary. If Jessie is close to the boundary, a tiny change in hunger or price could flip the prediction. If the boundary is far away from the closest examples, small changes are less likely to matter.
Any separator
Classifies the training data, but may pass close to examples.
Large margin
Leaves more room before predictions flip.
Support vectors
The closest examples that determine the boundary.
Most training examples do not determine the final classifier. Move a point far away from the boundary and nothing changes. Move one of the closest points and the optimal boundary changes immediately. Those closest points are the support vectors.
Interactive Demo
Drag a far-away point and the boundary barely moves. Drag one of the outlined support vectors and the margin changes immediately.
In a real SVM, C controls the penalty for margin violations. This demo uses it as a visual tolerance so you can see which points are becoming troublesome.
The outlined points are the current support vectors. They matter most because they sit closest to the boundary and define the margin.
Mathematics
The SVM chapter has one job: understand the best linear decision boundary. Kernels come next, after the linear idea is clear.
The Decision Boundary
A linear classifier uses a boundary:
Everything on one side is predicted as class . Everything on the other side is predicted as class . Unlike logistic regression, a standard SVM does not output a probability. Only the sign matters.
Defining The Margin
Suppose a training example is , where . For a hard-margin SVM, every point must be on the correct side of the boundary with enough clearance:
The closest points satisfy equality:
Those equality points are the support vectors. They lie on the two margin lines that run parallel to the decision boundary.
Maximizing The Margin
The margin width is:
Maximizing that margin is equivalent to minimizing . The standard hard-margin optimization is written as:
This is a constrained optimization problem. Among all boundaries that classify the training data correctly, it chooses the one with the widest margin.
Why It Works
A larger margin creates a more robust classifier. If a boundary passes extremely close to the data, a small change in a feature can flip the prediction. If the boundary leaves a wide empty gap, small perturbations are less likely to matter.
This is why SVMs are not satisfied with any separating line. They prefer the line that separates the classes while staying as far as possible from the nearest training examples.
Soft Margin
Real datasets are rarely perfectly separable. Jessie might sometimes buy a burger while dieting. If the classes overlap, no line can classify every example correctly.
A soft-margin SVM introduces slack variables that allow some examples to violate the margin or even be misclassified:
The parameter controls the trade-off. Large prioritizes fitting the training data. Small allows a wider margin even if some examples violate it.
Trees Versus SVMs
| Idea | Decision Trees | SVM |
|---|---|---|
| Model shape | Piecewise decision boundary | One separating boundary |
| Main idea | Learn rules from recursive splits | Maximize the safety margin |
| Non-linearity | Natural for trees | Needs transformed features or kernels |
| Feature scaling | Usually not required | Very important |
| Interpretability | A small tree can be readable | Support vectors and weights are less direct |
Complexity
Traditional SVM training solves a quadratic optimization problem, so it can become expensive on very large datasets. Prediction can be efficient because only the support vectors determine the final decision in the usual formulation.
Implementation
This implementation trains a linear soft-margin SVM with hinge loss and gradient descent. Labels must be encoded as and .
import numpy as np
class LinearSVM:
def __init__(self, learning_rate=0.001, C=1.0, epochs=1000):
self.learning_rate = learning_rate
self.C = C
self.epochs = epochs
self.w = None
self.b = 0.0
def fit(self, X, y):
n_samples, n_features = X.shape
self.w = np.zeros(n_features)
self.b = 0.0
for _ in range(self.epochs):
for x_i, y_i in zip(X, y):
margin = y_i * (np.dot(self.w, x_i) + self.b)
if margin >= 1:
grad_w = self.w
grad_b = 0
else:
grad_w = self.w - self.C * y_i * x_i
grad_b = -self.C * y_i
self.w -= self.learning_rate * grad_w
self.b -= self.learning_rate * grad_b
return self
def decision_function(self, X):
return X @ self.w + self.b
def predict(self, X):
return np.where(self.decision_function(X) >= 0, 1, -1)In practice, scale features before training an SVM. Distances and margins are central to the model, so a feature measured in thousands can dominate a feature measured between zero and one.
Interview Discussion
What is the margin?
The margin is the distance between the separating boundary and the nearest training examples on either side.
Why maximize the margin?
A larger margin makes the classifier less sensitive to small changes in the input features, which often improves generalization.
What are support vectors?
They are the closest training examples to the boundary. They determine the position of the maximum-margin classifier.
What happens when the data is not linearly separable?
A soft-margin SVM allows margin violations using slack variables and controls the penalty with C.
Why is feature scaling important for SVMs?
SVMs depend on distances and margins. Features with larger numeric scales can dominate the geometry if the data is not scaled.
Active Recall
1. Why is any separating line not good enough?
2. Which training examples actually determine the classifier?
3. Why does maximizing the margin improve generalization?
4. Explain the purpose of the parameter C.
5. Why does a standard SVM not produce probabilities?
Common Mistakes
- Thinking SVM outputs probabilities.
- Assuming every training example determines the boundary.
- Confusing support vectors with all observations.
- Believing SVM always requires kernels.
- Forgetting to scale features.
Connection To Kernel Methods
Decision trees learn flexible boundaries by recursively partitioning the feature space. Support Vector Machines take the opposite approach: they search for the single linear boundary that separates the classes as robustly as possible. The next question is what to do when no linear boundary exists. That leads to Kernel Methods.