The Problem with Linearity
In the previous lesson we saw that SVMs find the maximum-margin hyperplane — a powerful idea, but one that only works when the classes can be separated by a straight line (or flat hyperplane). Real-world data is rarely so cooperative. Think of two classes arranged in concentric rings, or a XOR pattern: no hyperplane can separate them.
The classical approach to nonlinear problems is feature engineering: manually construct new features that make the problem linearly separable. For example, if your data has feature x, you might add x2 as a new feature. In two dimensions, a circle separates two classes that a line cannot — and a circle in the original space corresponds to a line in the space (x, x2). But doing this manually is tedious, requires domain knowledge, and becomes intractable in high dimensions.
The kernel trick automates and generalizes this idea elegantly: it allows SVMs to operate in a very high-dimensional (potentially infinite-dimensional) feature space without ever computing the coordinates in that space.
Feature Maps
Formally, a feature map φ is a function that maps a data point x from the original input space to a higher-dimensional feature space:
Once we apply φ to all inputs, we train a linear SVM in the new space. The decision boundary is linear in feature space but corresponds to a nonlinear boundary in the original input space. The catch: if φ maps into a very high-dimensional space, computing φ(x) explicitly and then taking dot products becomes computationally prohibitive.
For instance, the polynomial feature map for two features (x1, x2) of degree 2 produces 6 features: (1, x1, x2, x12, x1x2, x22). For degree d with p original features, the feature space has O(pd) dimensions — quickly astronomical.
The Kernel Function
Here is the key observation: in the SVM optimization problem (in its dual form), data points appear only as dot products φ(x)Tφ(z). We never need the coordinates of φ(x) and φ(z) individually — only their inner product. A kernel function computes this inner product directly from the original inputs, without ever constructing φ:
This is profound: we can work in an extremely high-dimensional (or even infinite-dimensional) feature space at the cost of only a simple function evaluation in the original space. The computational complexity depends on the original dimension, not the feature space dimension.
When Is a Kernel Valid?
Not every function K(x, z) corresponds to a valid inner product in some feature space. A function K is a valid kernel (also called a Mercer kernel) if and only if the kernel matrix K (where Kij = K(xi, xj)) is positive semi-definite for all possible training sets. This is Mercer’s condition.
Intuitively, a positive semi-definite kernel matrix guarantees that we are computing inner products in some valid inner product space. If this condition fails, the SVM optimization may not have a unique solution or may not converge. In practice, the standard kernels (RBF, polynomial, linear) all satisfy Mercer’s condition, and we rarely need to verify it explicitly.
Common Kernel Functions
A small zoo of standard kernels covers the vast majority of practical problems:
| Kernel | Formula | Feature Space |
|---|---|---|
| Linear | K(x,z) = xTz | Original space (no mapping) |
| Polynomial | K(x,z) = (xTz + c)d | All monomials up to degree d |
| RBF / Gaussian | K(x,z) = exp(−γ‖x−z‖²) | Infinite-dimensional |
| Sigmoid | K(x,z) = tanh(κxTz + θ) | Approximates neural net layer |
The RBF Kernel in Depth
The Radial Basis Function (RBF) kernel, also called the Gaussian kernel, is by far the most widely used. It measures similarity as a Gaussian function of the Euclidean distance between two points:
The RBF kernel corresponds to an infinite-dimensional feature space — you can verify this by expanding the Taylor series of the exponential. Despite this, training and prediction are both tractable because we only ever compute K(xi, xj), never φ(x) itself. The RBF kernel is a universal approximator: given enough data and the right γ, it can represent any continuous decision boundary.
The γ parameter in the RBF kernel is often written as 1/(2σ2), connecting it to the variance of the Gaussian. A common default is γ = 1/n_features (scikit-learn’s default). Like C, it is tuned by cross-validation. Large γ risks overfitting (the boundary memorizes individual training points); small γ risks underfitting (the boundary is too smooth to capture the true structure).
Why the Dual Formulation Matters
The kernel trick requires that data appear only as dot products. This is guaranteed by working in the dual form of the SVM optimization. The dual problem reformulates the primal SVM (minimize ‖w‖ subject to constraints) into an equivalent problem over Lagrange multipliers αi:
After solving the dual, prediction for a new point x is:
The sum runs only over support vectors (where αi > 0), so prediction is efficient. The dual problem has O(n2) to O(n3) complexity in the number of training examples n — this is SVMs’ main scalability bottleneck for very large datasets.
Choosing the Right Kernel
Kernel selection is as much art as science. Some practical guidance:
Linear kernel: Start here. It is the fastest to train and easiest to interpret. If your data has many more features than examples (text classification, genomics), a linear SVM often outperforms nonlinear ones.
RBF kernel: The default for most problems when the linear kernel is insufficient. It is a safe choice when you do not have strong prior knowledge about the structure of the data. Tune C and γ jointly via grid search on a log scale.
Polynomial kernel: Useful when you believe the interaction of features matters at a specific degree. Degree 2 is common for computer vision tasks. Tends to be numerically less stable than RBF for high degrees.
Custom kernels: You can define your own K as long as it satisfies Mercer’s condition. Domain-specific kernels — string kernels for text, graph kernels for molecules — can encode domain knowledge directly into the similarity measure.
One elegant implication of the kernel trick: the entire model is encoded in the kernel matrix K. Two algorithms that use the same kernel on the same data will produce the same decision boundary, regardless of their internal implementations. This is why kernel methods are so modular — you can swap kernels to swap the notion of similarity, without changing any other part of the algorithm.
Strengths and Limitations
Kernel SVMs offer flexibility without explicit feature engineering. They are principled, have strong generalization guarantees, and work well even with limited data. The RBF kernel is a universal approximator and will, given enough examples, approximate any smooth decision boundary.
The main limitation is scalability. Storing and solving the kernel matrix is O(n2) in memory and O(n3) in training time. For datasets with more than ~100,000 examples, kernel SVMs become impractical without approximation methods (e.g., Nyström approximation, random features). This is why deep learning, which scales linearly in dataset size with stochastic gradient descent, has largely replaced SVMs for large-scale tasks — while kernel SVMs remain competitive on small to medium datasets.
- The kernel trick allows SVMs to operate in high-dimensional feature spaces without explicitly computing the feature map φ.
- A kernel function K(x, z) computes the inner product φ(x)·φ(z) directly from the original inputs.
- Mercer’s condition (positive semi-definite kernel matrix) guarantees a kernel corresponds to a valid inner product space.
- The three standard kernels are: linear (no mapping), polynomial (monomials up to degree d), and RBF (infinite-dimensional Gaussian).
- The RBF kernel is the most widely used; γ controls the width of the similarity bell — large γ means complex boundary, small γ means smooth boundary.
- In the dual formulation, prediction uses only kernel evaluations against support vectors, making it efficient even in infinite-dimensional spaces.
- Kernel SVMs scale as O(n²) in memory and O(n³) in training — impractical for very large datasets without approximation.