Kernel Methods

Make non-linear patterns look linear by changing the representation.

The Problem

In the previous chapter, a Support Vector Machine found the best linear decision boundary. That works beautifully when a straight line can separate the classes. But what if no straight line works?

Imagine predicting whether Jessie orders a burger using hunger level and burger price. The burger examples sit in the middle. The no-burger examples surround them.

        no  no  no
     no             no
  no      burger     no
     no             no
        no  no  no

No matter how we rotate a straight line, some examples will be on the wrong side. The linear SVM fails, not because margins are a bad idea, but because the original representation makes the pattern impossible to separate linearly.

Core Intuition

Kernel methods begin with a change in question. Instead of asking whether we can find a better line, ask whether we can represent the data differently so that a line becomes possible.

A one-dimensional example makes the idea clear:

x:     -2   -1    0    1    2
class: no  yes  yes  yes   no

No single threshold on xx separates the classes. The positive class is in the middle and the negative class is on both ends.

Now create a new feature, x2x^2:

xx squaredClass
-24no
-11yes
00yes
11yes
24no

A simple threshold on x2x^2 now works. The classifier did not become more complicated. The representation became more useful.

Interactive Demo

Feature map lab
Original spacex, y

A straight line misses the circular structure. The positive class lives near the center while the negative class surrounds it.

Mapped spacer squared

After mapping each point to its distance from the center, one threshold separates the same data.

Mapped separation
12/12

The classifier did not become curved. The representation changed so a simple boundary became useful.

Polynomial similarity
2.25
RBF similarity to center
0.94

Kernels compute similarities that behave like dot products in mapped feature spaces, without explicitly building those feature vectors.

The circular pattern is hard in the original space. After mapping each point to a radial feature, the same classes become separable by a simple threshold.

Mathematics

Kernel methods are easiest to understand in two stages. First, explicit feature mapping explains why adding dimensions can help. Then the kernel trick explains how to get the benefit without explicitly constructing those dimensions.

The Algorithm: Feature Mapping

Suppose the original input is x=(x1,x2)x=(x_1,x_2). Instead of giving the SVM this input directly, construct a new representation:

ϕ(x)=(x1,x2,x12,x22,x1x2)\phi(x)=(x_1,x_2,x_1^2,x_2^2,x_1x_2)

The SVM is trained on ϕ(x)\phi(x) rather than xx. The optimization problem is the same as before. Only the input representation changed.

This can make curved patterns look linear. A circular pattern in (x1,x2)(x_1,x_2) may become separable after adding x12+x22x_1^2+x_2^2.

The Problem With Explicit Features

Feature mapping works until the mapped representation becomes enormous. Polynomial features of degree twenty, degree one hundred, or infinitely many basis functions may be impossible to construct directly.

This seems to make the approach impractical. But SVMs reveal a loophole.

The Kernel Trick

In the dual view of an SVM, the optimization does not need transformed vectors individually. It needs dot products between transformed vectors:

ϕ(xi)Tϕ(xj)\phi(x_i)^T\phi(x_j)

A kernel function computes that dot product directly:

K(xi,xj)=ϕ(xi)Tϕ(xj)K(x_i,x_j)=\phi(x_i)^T\phi(x_j)

This is the kernel trick. We get the effect of working in a rich feature space without explicitly building the feature vectors in that space.

Common Kernels

KernelFormulaMeaning
LinearK(x,z)=x^TzNo transformation. Equivalent to a linear model.
PolynomialK(x,z)=(x^Tz+c)^dCreates polynomial interactions. Degree d controls complexity.
RBFK(x,z)=exp(-gamma ||x-z||^2)Measures local similarity. Nearby points have large similarity.

The RBF kernel is especially common because it defines local similarity. Nearby points have high similarity. Far-apart points have similarity close to zero.

K(x,z)=exp(γxz2)K(x,z)=\exp(-\gamma\|x-z\|^2)

The parameter γ\gamma controls how quickly similarity fades with distance. Large γ\gamma creates very local influence. Small γ\gamma creates smoother, broader influence.

Why It Works

A kernel is a similarity function with a special interpretation: it behaves like a dot product in some feature space. Instead of asking where a point is in the original coordinates, a kernel method asks how similar the point is to important training examples.

Different kernels define different geometries. The kernel determines which points count as similar, and therefore which decision boundaries are easy to express.

Complexity

Kernel methods are often more expensive than linear methods. Training usually requires computing similarities between many pairs of training examples. Prediction depends on the number of support vectors, so models with many support vectors can be slow at inference time.

Implementation

In practice, you usually choose a kernel through a library implementation. The point of this comparison is to train the same SVM with three different similarity functions and inspect how the boundary changes.

from sklearn.datasets import make_circles
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC

X, y = make_circles(n_samples=300, factor=0.35, noise=0.08)

models = {
    "linear": SVC(kernel="linear", C=1.0),
    "polynomial": SVC(kernel="poly", degree=3, C=1.0),
    "rbf": SVC(kernel="rbf", gamma=2.0, C=1.0),
}

for name, model in models.items():
    clf = make_pipeline(StandardScaler(), model)
    clf.fit(X, y)
    print(name, clf.score(X, y))

Always scale features before using distance-sensitive kernels. Otherwise, one large-scale feature can dominate the similarity calculation.

Interview Discussion

Why do kernels exist?

They let linear algorithms work with richer representations, often making non-linear patterns separable.

What problem does the kernel trick solve?

It avoids explicitly computing huge transformed feature vectors by computing their dot products directly.

What is the difference between feature engineering and kernel methods?

Feature engineering explicitly creates new features. Kernel methods implicitly work in a feature space through similarity computations.

Why is the RBF kernel popular?

It is flexible and local: nearby points are similar, far points are nearly unrelated, allowing complex smooth boundaries.

Why can kernel SVMs become slow on large datasets?

Training requires many pairwise kernel evaluations, and prediction can depend on many support vectors.

Active Recall

1. Why can a linear SVM not separate circular data in the original space?

2. What does a feature mapping accomplish?

3. Why do we not always explicitly compute transformed features?

4. What quantity does a kernel compute?

5. Explain the kernel trick without using equations.

Common Mistakes

  • Thinking the kernel is another classifier.
  • Believing transformed feature vectors are always explicitly computed.
  • Assuming kernels only work with SVMs.
  • Confusing kernels with ordinary feature engineering.
  • Forgetting that gamma and C control boundary flexibility.

Connection To Representation Learning

Support Vector Machines found the optimal linear decision boundary. Kernel methods keep the same optimization idea but change the representation. So far, we have designed better representations by hand. The next question is whether a model can discover useful representations automatically from data.