~/blog
SVM Kernels
Imagine you're classifying images of handwritten digits. Some digits — like 0 and 8 — have shapes that aren't separable by any straight line in pixel space. Or consider a fraud detection system where transactions are flagged as suspicious when a specific combination of features is present (high amount AND unusual location), but neither feature alone is decisive. Patterns like these wrap around the feature space in ways no linear boundary can capture.
Linear SVMs fail here. No matter how wide you set the margin, a straight line through the original features cannot separate these classes. The solution is to project the data into a higher-dimensional space where a linear separator exists — without ever computing that projection explicitly. That's the kernel trick.
Let's see this failure in action on the simplest nonlinear pattern: XOR.
import numpy as np
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
X = np.array([
[ 1, 1], [ 2, 2], [-1, -1], [-2, -2], # positive class: same sign
[ 1, -1], [ 2, -2], [-1, 1], [-2, 2], # negative class: opposite sign
])
y = np.array([1, 1, 1, 1, -1, -1, -1, -1])
svm_linear = SVC(kernel='linear', C=1.0)
svm_linear.fit(X, y)
train_acc = svm_linear.score(X, y)
print(f"Linear SVM train accuracy on XOR: {train_acc:.2f}")Linear SVM train accuracy on XOR: 0.50Random guessing. The XOR pattern requires a nonlinear boundary.
What It Is
A kernel is a function that computes the dot product between two points in a transformed feature space — without ever constructing that space. The kernel trick replaces every in the dual SVM objective with , letting you train SVMs in spaces with millions or infinitely many dimensions at the cost of an computation in the original space.
Why Linear Fails on XOR
Positive class sits in Q1 and Q3 (same-sign quadrants); negative class in Q2 and Q4. Any line that separates Q1 from Q4 fails to separate Q3 from Q2 — the pattern wraps around.
The Plan — Four Steps to Kernel SVM
We'll walk through kernels in four steps: first see why linear SVM fails and how explicit feature maps solve it, then learn the kernel trick that avoids computing the map, explore the two most important kernels (polynomial and RBF), and finally understand how to choose among them.
Step 1 — The Feature Map Solution
Instead of drawing a line in 2D, let's map to a 3D space where the classes become linearly separable. The feature map :
The third component captures the sign interaction — positive when have the same sign, negative otherwise.
| Point | |||||
|---|---|---|---|---|---|
| (1,1) | +1 | +1 | 1 | 1 | +1 |
| (2,2) | +1 | +1 | 4 | 4 | +4 |
| (−1,−1) | +1 | +1 | 1 | 1 | +1 |
| (−2,−2) | +1 | +1 | 4 | 4 | +4 |
| (1,−1) | −1 | −1 | 1 | 1 | −1 |
| (2,−2) | −1 | −1 | 4 | 4 | −4 |
| (−1,1) | −1 | −1 | 1 | 1 | −1 |
| (−2,2) | −1 | −1 | 4 | 4 | −4 |
In the lifted space, all positive class points have and all negative class points have . The hyperplane (i.e., ) separates them perfectly.
✓ Step 1 complete. By mapping XOR data to 3D with , the classes become linearly separable.
Step 2 — The Kernel Trick
Computing explicitly then taking dot products is expensive — the mapped space can be millions of dimensions. The kernel trick shows that what appears in the dual objective is only the dot product , and this inner product can often be computed without ever computing explicitly.
A kernel function is:
computed directly in the original space. For the polynomial kernel of degree 2:
Expanding:
This equals for the feature map — without ever computing the 6-dimensional vector.
✓ Step 2 complete. The kernel computes dot products in the lifted space using only the original coordinates.
Step 3 — Polynomial Kernel Values on the Anchor
Computing for representative pairs:
| Pair | Same class? | ||
|---|---|---|---|
| (1,1) vs (2,2) | Yes (+1) | ||
| (1,1) vs (1,−1) | No | ||
| (1,1) vs (−1,−1) | Yes (+1) | ||
| (1,−1) vs (2,−2) | Yes (−1) | ||
| (1,1) vs (−2,2) | No |
Same-class pairs share high kernel values (25) when features have the same magnitude. Cross-class pairs get — the offset from the constant term only, no sign interaction.
✓ Step 3 complete. The polynomial kernel implicitly computes a 6-dimensional feature map.
Step 4 — The RBF Kernel
The Radial Basis Function (Gaussian) kernel is the most commonly used:
- controls how quickly similarity decays with distance
- As : all points are equally similar (underfitting)
- As : only identical points are similar (overfitting)
- RBF corresponds to an infinite-dimensional feature map — it can represent any smooth decision boundary
RBF kernel values for on the anchor:
| Pair | Same class? | ||
|---|---|---|---|
| (1,1) vs (2,2) | Yes (+1) | ||
| (1,1) vs (1,−1) | No | ||
| (1,1) vs (−1,−1) | Yes (+1) |
The (1,1) vs (−1,−1) pair gets — even though they're in the same class, they're far apart in distance. The kernel doesn't know class labels; it only measures similarity by proximity.
✓ Step 4 complete. The RBF kernel corresponds to an infinite-dimensional feature map, yet costs only to compute.
Hyperparameter Sensitivity — Effect on the Decision Boundary
Let's see what happens as we vary . We'll use C=1.0 and try values from 0.1 to 100. Notice we don't need StandardScaler here — XOR data is already centered and on the same scale.
import matplotlib
matplotlib.use('Agg')
import numpy as np
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
X = np.array([[1,1],[2,2],[-1,-1],[-2,-2],[1,-1],[2,-2],[-1,1],[-2,2]])
y = np.array([1,1,1,1,-1,-1,-1,-1])
gammas = [0.1, 1, 10, 100]
for gamma in gammas:
svm = SVC(kernel='rbf', C=1.0, gamma=gamma)
svm.fit(X, y)
train_acc = svm.score(X, y)
print(f"gamma={gamma:>5}: train_acc={train_acc:.2f}, n_SV={svm.support_vectors_.shape[0]}")gamma= 0.1: train_acc=0.50, n_SV=8
gamma= 1: train_acc=1.00, n_SV=4
gamma= 10: train_acc=1.00, n_SV=8
gamma= 100: train_acc=1.00, n_SV=8At : still fails — the RBF kernel is too smooth, each point's influence extends so far that the XOR structure averages out. At : optimal — four support vectors, a clean X-shaped boundary. At : still 100% train accuracy, but all 8 points become support vectors — the kernel is so local that each point only sees itself.
✓ Hyperparameter sensitivity complete. controls the RBF kernel's locality: too small underfits (all points look equally similar), too large overfits (each point only sees itself).
Choosing a Kernel
| Kernel | Formula | When to use |
|---|---|---|
| Linear | High-dimensional data, text, when | |
| Polynomial (deg ) | Image patches, polynomial relationships | |
| RBF (Gaussian) | Default choice; works when decision boundary is complex | |
| Sigmoid | Rarely used; not always a valid kernel |
RBF is the default for unknown structure. Linear wins when the data is already high-dimensional (text BOW, embeddings) because: (a) it's often linearly separable in high dimensions already, (b) the dual has variables but the primal has — using the primal (with LinearSVC) is faster.
Mercer's Theorem
A function is a valid kernel (corresponds to some feature map ) if and only if the kernel matrix is positive semi-definite for any set of points. This is Mercer's condition. The sigmoid kernel violates this for some parameter choices — SVM convergence is not guaranteed when using non-PSD kernels.
Related Concepts
Backward: The kernel trick builds entirely on the dual formulation from post 02. The dual SVM objective contains everywhere — not individually. This is what makes it possible to substitute for without changing the optimization. If the dual weren't available, kernels would require explicitly mapping to the lifted space (which could be infinite-dimensional).
Forward: Kernel SVM opens the door to spectral methods and graph-based learning. The kernel matrix is the core of kernel PCA, kernel ridge regression, and Gaussian processes. Understanding why must be positive semi-definite (Mercer's theorem) carries over directly to these methods.
Honest Limitations
-
and interact in ways you won't expect until you grid-search them together. I've spent hours tuning alone, only to find that changing by an order of magnitude made the search irrelevant. These two hyperparameters form a coupled system — controls the shape of the boundary and controls how tightly it fits the data. Always tune them jointly with
GridSearchCVover a logarithmic grid like{'C': [0.1, 1, 10], 'gamma': [0.1, 1, 10]}. -
Kernel SVM trades interpretability for flexibility — and you will feel this loss. With a linear kernel, I can inspect the weight vector and say "credit score contributes positively to predicting no default." With RBF, the decision function is a sum over support vectors weighted by their distance to the new point. You can't extract feature importance, you can't explain individual predictions by feature contributions, and you can't easily debug why a specific point was misclassified. If interpretability matters for your stakeholders, consider linear models or tree-based methods before reaching for RBF.
-
No single kernel works for all data. RBF assumes the decision boundary has the same complexity everywhere in the feature space. I've seen this fail on text data where word importance varies by topic — one global kernel can't capture topic-specific interactions. Polynomial kernels of degree 2 or 3 handle different interaction patterns but require understanding whether your problem has polynomial structure. Kernel selection is model selection; treat it as a hyperparameter search, not a free choice.
Test Your Understanding
-
The feature map separates XOR. What hyperplane in the 3D lifted space corresponds to the decision boundary ? Express it in terms of .
-
The polynomial kernel computes a 6-dimensional dot product without explicitly computing 6-dimensional vectors. What is the computational cost of computing in the original 2D space? What would it cost to explicitly compute in 6D?
-
At , the RBF kernel fails on XOR (50% accuracy). Explain geometrically: if the RBF kernel is very smooth (large influence radius), what does the learned decision function look like?
-
At , all 8 training samples become support vectors. This achieves 100% train accuracy but is likely to generalize poorly. Given the KKT conditions from post 02, what does it mean when every point is a support vector?
-
The linear kernel SVC fails on XOR but would succeed on the lifted XOR data (with as a third feature). If you add as a feature and use a linear kernel, would the result be identical to using an RBF kernel with on the original data? Why or why not?