Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Curved boundaries from a linear method
Some classes cannot be split by a straight line, such as soft sand forming a ring around firm ground. You could invent new features (squares, products, distances) until a line works in the bigger space, but that space can be huge. The kernel trick gets the same result by only changing how similarity between two examples is measured. An SVM with an RBF kernel places a smooth bump of influence around each support vector, and the boundary follows wherever the bumps of the two classes balance.
Kernel SVMs were the leading non-linear classifiers for spectra, images and sensor data before deep learning. They still win on small and medium datasets with well-designed features, and they generalise better than neural networks when only hundreds of labelled examples exist, as is common in planetary science and fault diagnosis.
- 1. The kernel trick replaces…
- 2. An RBF kernel gives each support vector…
- 3. In the demo, very large γ makes the SVM…
Level 1: Beginner
★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.The trick by hand
Take the polynomial kernel k(x, z) = (x·z)² in 2-D. Expanding:
(x₁z₁ + x₂z₂)² = x₁²z₁² + 2x₁x₂z₁z₂ + x₂²z₂² = φ(x)·φ(z), with φ(x) = (x₁², √2·x₁x₂, x₂²).
So the kernel computes a dot product in a 3-D space of squares and products, without forming it.
XOR. The points (0, 0) and (1, 1) are class −1; (0, 1) and (1, 0) are class +1. No line separates them. In φ-space the product feature x₁x₂ is 1 only at (1, 1), which makes them separable. A quadratic-kernel SVM classifies all four correctly (exercise 1); a linear SVM cannot.
RBF kernel: k(x, z) = exp(−γ‖x − z‖²). It equals 1 for identical points and decays with distance.
- γ = 0.5 and ‖x − z‖² = 2 give e^(−1) = 0.368.
- Larger γ means faster decay: a more local, wigglier boundary.
The steps in plain English:
- Standardise the features. The RBF and polynomial kernels depend on scale.
- Pick a kernel: RBF by default, polynomial for known interaction orders, linear for many features.
- Compute the Gram matrix Kᵢⱼ = k(xᵢ, xⱼ) and solve the SVM dual with K in place of XXᵀ.
- Predict with f(x) = Σαᵢyᵢk(xᵢ, x) + b, a sum over support vectors only.
- Tune γ and C jointly by cross-validation on a log grid.
- 1. Dimension of φ for the 2-D kernel (x·z)²?
- 2. RBF value for identical points?
- 3. Larger γ in the RBF kernel makes the boundary…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.Kernels and the kernel SVM
Kernel SVM dual: maximise Σαᵢ − ½ΣᵢΣⱼ αᵢαⱼyᵢyⱼ k(xᵢ, xⱼ) subject to 0 ≤ αᵢ ≤ C and Σαᵢyᵢ = 0.
Decision function: f(x) = Σ over support vectors of αᵢyᵢ k(xᵢ, x) + b.
Common kernels:
| Kernel | k(x, z) | Feature space | Notes |
|---|---|---|---|
| Linear | x·z | the input space | fast; many features |
| Polynomial | (γ x·z + r)^d | all monomials up to degree d | interactions; can be unstable at high d |
| RBF / Gaussian | exp(−γ‖x − z‖²) | infinite-dimensional | universal; the default |
| Sigmoid | tanh(γ x·z + r) | (not always valid) | not PSD for many γ, r |
Mercer’s condition. k is a valid kernel (an inner product in some space) if and only if every Gram matrix it produces is symmetric positive semidefinite. Sums, products and positive scalings of kernels are kernels, which lets you build kernels for mixed data.
Bias handling. Adding 1 to the kernel (an extra constant feature) absorbs b and removes the equality constraint. Exercise 1 uses this, solving the box-constrained dual with L-BFGS-B.
from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
model = GridSearchCV(make_pipeline(StandardScaler(), SVC(kernel="rbf")),
{"svc__C": np.logspace(-1, 3, 5), "svc__gamma": np.logspace(-2, 2, 5)}, cv=5).fit(X, y)Use kernel SVMs for small to medium n (up to about 10⁴–10⁵) with non-linear structure. Prefer linear SVMs for very high-dimensional sparse data, and gradient boosting or neural networks for very large n.
- 1. A function is a valid kernel iff its Gram matrices are…
- 2. The sum of two valid kernels is…
- 3. Kernel SVM predictions sum over…
Level 3: Advanced
★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.Theory
Reproducing-kernel Hilbert spaces. Every valid kernel defines a Hilbert space of functions 𝓗 with ⟨f, k(·, x)⟩ = f(x). The SVM minimises hinge loss + λ‖f‖²_𝓗 over 𝓗. By the representer theorem, the solution is f = Σαᵢyᵢk(xᵢ, ·): a finite combination, even though 𝓗 may be infinite-dimensional.
The RBF kernel is universal. Its RKHS is dense in the continuous functions on compact sets, so an RBF SVM can approximate any decision boundary given enough data (Steinwart, 2001). The bandwidth γ acts as a smoothness prior:
- small γ gives nearly linear (very smooth) functions;
- large γ makes every training point an isolated bump, which means memorisation (exercise 2: at γ = 100, 97% of points are support vectors and accuracy drops to 0.72).
γ and C interact. Large γ with small C can still underfit. A common heuristic for γ is 1/(p·Var(X)) (scikit-learn’s ‘scale’), or the median heuristic 1/median‖xᵢ − xⱼ‖². The good region is usually a diagonal band in the (log γ, log C) grid.
Computational cost. The Gram matrix is O(n²), and training is roughly O(n²)–O(n³). Prediction is O(n_SV) kernel evaluations. Approximations: Nyström, random Fourier features (Rahimi and Recht, 2007).
Interview questions.
- What does the RBF γ control? The width of each support vector’s influence: small means smooth and global, large means local and wiggly. Tune it with C by cross-validation.
- Why isn’t the sigmoid kernel always valid? tanh(γx·z + r) can produce Gram matrices with negative eigenvalues. Then the dual is non-convex and the optimiser may misbehave.
- 1. The RBF kernel is 'universal' meaning…
- 2. Random Fourier features approximate…
- 3. The median heuristic sets γ from…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Tuning an RBF SVM for ring-shaped terrain
History.
- Aizerman, Braverman and Rozonoer (1964) used the kernel idea in potential-function methods.
- Mercer (1909) gave the PSD characterisation.
- Boser, Guyon and Vapnik (1992) combined kernels with maximum margins.
- Schölkopf and Smola (2002) systematised kernel methods.
- Rahimi and Recht (2007) introduced random features for scaling.
- Kernel SVMs held records on handwritten digits (MNIST, around 1.1% error with polynomial kernels and deskewing) before convolutional networks.
Exercise 1.
- (x·z)² equals the explicit 3-feature inner product.
- The RBF and quadratic kernels separate XOR; the linear kernel cannot.
- RBF and polynomial Gram matrices are PSD, while a sigmoid kernel with γ = 2, r = −1 produces negative eigenvalues.
Exercise 2: ring-shaped terrain. Setup: 200 training points with 8% label noise, clean test labels, 3 repetitions. The table shows test accuracy.
| γ \ C | 0.1 | 1 | 10 | 100 |
|---|---|---|---|---|
| 0.1 | 0.519 | 0.501 | 0.570 | 0.853 |
| 1 | 0.719 | 0.942 | 0.933 | 0.909 |
| 10 | 0.593 | 0.916 | 0.889 | 0.864 |
| 100 | 0.520 | 0.712 | 0.725 | 0.725 |
3-fold CV chose γ = 1 with C = 10 or 100, giving test accuracy 0.917.
The γ = 0.1 row is numerically fragile: with such wide bumps the 200 × 200 Gram matrix is nearly singular, and near-chance accuracies there depend on the solver’s round-off. In the browser the C = 10 cell comes out 0.62 instead of 0.57. The conclusion, that γ = 0.1 cannot draw the ring without a very large C, holds either way.
- γ = 1 (bumps about as wide as the ring) is best. With C = 1 it reaches 0.94, close to the clean-label optimum.
- γ = 100 memorises. About 97% of points become support vectors and test accuracy falls to 0.72.
- γ = 0.1 is nearly linear. It needs C = 100 to bend into a ring at all.
- CV picked a larger C than the best on clean test data (10–100 against 1). With noisy training labels, CV error is minimised by slightly over-fitting the noise. Its choice still scored within 3 points of the best.
Kernel SVMs in AI, robotics and aerospace
- Terrain and hazard classification from engineered visual and lidar features.
- Spectral classification of minerals and materials with few labelled samples.
- Fault diagnosis: non-linear boundaries between nominal and faulty telemetry.
- Kernel ideas everywhere: Gaussian processes, kernel PCA, attention as a kernel smoother.
References
- B. E. Boser, I. M. Guyon, V. N. Vapnik (1992). “A training algorithm for optimal margin classifiers.” COLT.
- B. Schölkopf, A. J. Smola (2002). Learning with Kernels. MIT Press.
- I. Steinwart (2001). “On the influence of the kernel on the consistency of support vector machines.” JMLR 2.
- A. Rahimi, B. Recht (2007). “Random features for large-scale kernel machines.” NeurIPS.
- C.-W. Hsu, C.-C. Chang, C.-J. Lin (2003). “A practical guide to support vector classification.” Technical report, National Taiwan University.