Support Vector Machines

Find the separating boundary with the largest safety margin.

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  x

Do 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

Margin lab
B1B2B3B4N1N2N3N4
Current margin
27.8

Drag a far-away point and the boundary barely moves. Drag one of the outlined support vectors and the margin changes immediately.

Support vectors
b3, n2
Points inside tolerance
0

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:

wTx+b=0w^Tx+b=0

Everything on one side is predicted as class +1+1. Everything on the other side is predicted as class 1-1. Unlike logistic regression, a standard SVM does not output a probability. Only the sign matters.

wTx+b>0+1w^Tx+b>0\Rightarrow +1
wTx+b<01w^Tx+b<0\Rightarrow -1

Defining The Margin

Suppose a training example is (xi,yi)(x_i,y_i), where yi{1,+1}y_i\in\{-1,+1\}. For a hard-margin SVM, every point must be on the correct side of the boundary with enough clearance:

yi(wTxi+b)1y_i(w^Tx_i+b)\ge 1

The closest points satisfy equality:

yi(wTxi+b)=1y_i(w^Tx_i+b)=1

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:

2w\frac{2}{\|w\|}

Maximizing that margin is equivalent to minimizing w\|w\|. The standard hard-margin optimization is written as:

minw,b12w2\min_{w,b}\frac12\|w\|^2
subject to yi(wTxi+b)1 for every i\text{subject to }y_i(w^Tx_i+b)\ge1\text{ for every }i

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 ξi\xi_i that allow some examples to violate the margin or even be misclassified:

minw,b12w2+Ciξi\min_{w,b}\frac12\|w\|^2+C\sum_i \xi_i

The parameter CC controls the trade-off. Large CC prioritizes fitting the training data. Small CC allows a wider margin even if some examples violate it.

Trees Versus SVMs

IdeaDecision TreesSVM
Model shapePiecewise decision boundaryOne separating boundary
Main ideaLearn rules from recursive splitsMaximize the safety margin
Non-linearityNatural for treesNeeds transformed features or kernels
Feature scalingUsually not requiredVery important
InterpretabilityA small tree can be readableSupport 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 1-1 and +1+1.

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.