Foundations to Frontier
Tests
B. Statistical Learning Theory › B3 Kernel methods

Feature Maps and the Kernel Trick

Linear methods are fast and well understood, but the world is curved. A feature map φ lifts the data into a space where linear methods work, and when an algorithm only uses inner products, a kernel k(x, z) = ⟨φ(x), φ(z)⟩ lets you work in that space without ever computing φ, even when it is huge or infinite-dimensional. This topic builds explicit polynomial feature maps, kernelizes the perceptron, introduces the polynomial and RBF kernels, and, in the research exercise, approximates the RBF kernel with random Fourier features, the trick that makes kernel methods scale.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Lift it, then cut it straight

Scatter red marbles in a ring around blue marbles on a table. No straight line separates them. Now push the middle of the tablecloth up: the blue marbles rise, the red ones stay low, and a flat sheet slid in between separates them perfectly. That’s a feature map: add new coordinates (here, height = distance from the centre squared) so that a straight cut works.

The kernel trick is the surprise: many learning algorithms only ever ask “how similar are these two points?”, meaning an inner product. If you can compute that similarity directly in the lifted space, you never need to build the lifted coordinates at all.

The kernel trick: linear methods, curved boundaries
data: kernel:
φ(x) = (x₁, x₂, x₁² + x₂²) with the plane z = 1.69
training accuracy 100.0% · converged in 3 epochs · 17 of 120 points carry weight (outlined)
The perceptron is a linear method, but written in terms of inner products it never needs the features themselves: replace x·z by a kernel k(x, z) = ⟨φ(x), φ(z)⟩ and it becomes linear in the feature space, with curved boundaries back in the plane. A linear kernel can't separate rings or XOR; (x·z + 1)² handles rings and XOR (it contains x₁², x₂² and x₁x₂); the RBF kernel (infinitely many features) fits anything, too wiggly when γ is large.
Why engineers care

Kernel methods (SVMs, Gaussian processes, kernel ridge regression) remain the method of choice for small, precious datasets: terrain classification from a few labelled patches, system identification for control, and surrogate models for expensive simulations. The same ideas explain wide neural networks (the neural tangent kernel).

📝 Quick check (Level 0)
0/3 answered
  1. 1. A feature map…
  2. 2. The kernel trick computes…
  3. 3. Rings in the plane become linearly separable when you add…

Level 1: Beginner

★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.

Feature maps

A feature map φ:Rd→F\varphi:\mathbb R^d\to\mathcal F turns a linear model in F\mathcal F into a nonlinear one in the input: f(x)=⟨w,φ(x)⟩f(x) = \langle w, \varphi(x)\rangle. For example, in 2-D,

φ(x)=(1,2x1,2x2,x12,x22,2x1x2)\varphi(x) = (1, \sqrt2x_1, \sqrt2x_2, x_1^2, x_2^2, \sqrt2x_1x_2)

gives all quadratic boundaries, and ⟨φ(x),φ(z)⟩=(x⋅z+1)2\langle\varphi(x), \varphi(z)\rangle = (x\cdot z + 1)^2.

Kernels

A kernel is a function k(x,z)=⟨φ(x),φ(z)⟩k(x, z) = \langle\varphi(x), \varphi(z)\rangle. Common choices:

  • linear: x⋅zx\cdot z
  • polynomial: (x⋅z+c)p(x\cdot z + c)^p, all monomials up to degree p
  • RBF (Gaussian): exp⁡(−γ∥x−z∥2)\exp(-\gamma\|x - z\|^2), an infinite-dimensional φ

The polynomial feature space has (d+pp)\binom{d + p}p dimensions: 5151 for d = 100 and p = 2, and about 97 million for p = 5. The kernel costs O(d) either way.

The Gram matrix

For data x1,…,xmx_1, \dots, x_m, the Gram matrix Kij=k(xi,xj)K_{ij} = k(x_i, x_j) holds everything a kernel method needs.

📝 Quick check (Level 1)
0/3 answered
  1. 1. (x·z + 1)² for x = (1, 2), z = (3, 4):
  2. 2. The RBF kernel's feature space is…
  3. 3. Dimension of all monomials of degree ≤ 2 in 100 variables:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

Kernelizing an algorithm

The perceptron’s weight vector is always a combination of the training points: w=∑iαiyiφ(xi)w = \sum_i\alpha_iy_i\varphi(x_i), with αᵢ the number of mistakes made on point i. So

f(x)=∑iαiyi k(xi,x).f(x) = \sum_i\alpha_iy_i\,k(x_i, x).

Training and prediction only use kernel values, so the kernel perceptron is exactly the explicit perceptron in feature space, without ever forming φ. The same rewrite works for ridge regression, SVMs, PCA, k-means and nearest neighbours (all depend only on inner products and distances, since ∥φ(x)−φ(z)∥2=k(x,x)+k(z,z)−2k(x,z)\|\varphi(x) - \varphi(z)\|^2 = k(x, x) + k(z, z) - 2k(x, z)).

The cost trade-off

Explicit features cost O(mD); kernels cost O(m²) memory and up to O(m³) time. Kernels win when D is huge, and lose when m is huge.

🐍 Exercise 1: the kernel perceptron on rings
Check that the explicit quadratic feature map reproduces the polynomial kernel, then train a linear perceptron, a perceptron on explicit quadratic features, and a kernel perceptron on two concentric rings. Compare test accuracy and confirm the last two are identical.
📝 Quick check (Level 2)
0/3 answered
  1. 1. In the kernel perceptron, αᵢ is…
  2. 2. ‖φ(x) − φ(z)‖² equals…
  3. 3. Kernel methods scale badly in…

Level 3: Advanced

★★★★☆ Advanced⏱ 35 minGraduate level, with full derivations and proofs.

Why the RBF kernel is an inner product

Factor e−γ∥x−z∥2=e−γ∥x∥2e−γ∥z∥2e2γx⋅ze^{-\gamma\|x - z\|^2} = e^{-\gamma\|x\|^2}e^{-\gamma\|z\|^2}e^{2\gamma x\cdot z} and expand e2γx⋅z=∑n(2γ)n(x⋅z)nn!e^{2\gamma x\cdot z} = \sum_n\frac{(2\gamma)^n(x\cdot z)^n}{n!}. Each (x⋅z)n(x\cdot z)^n is a polynomial kernel with explicit features, so the RBF kernel is an inner product of infinitely many scaled monomials. ∎

Bochner’s theorem and random features

A continuous shift-invariant kernel k(x−z)k(x - z) with k(0) = 1 is positive definite exactly when it is the Fourier transform of a probability distribution p(ω). For the RBF kernel, p=N(0,2γI)p = \mathcal N(0, 2\gamma I). Then

k(x−z)=Eω,b[2cos⁡(ω⋅x+b)cos⁡(ω⋅z+b)],k(x - z) = \mathbb E_{\omega, b}\big[2\cos(\omega\cdot x + b)\cos(\omega\cdot z + b)\big],

so z(x)=2/D cos⁡(Wx+b)z(x) = \sqrt{2/D}\,\cos(Wx + b) with D random rows gives an unbiased approximation with error O(1/D)O(1/\sqrt D) (Rahimi and Recht, 2007).

📝 Quick check (Level 3)
0/3 answered
  1. 1. The RBF kernel's spectral distribution is…
  2. 2. Random Fourier features approximate the kernel with error…
  3. 3. Bochner's theorem concerns…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

The kernel trick, measured

Exercise 1 (rings, 200 training points). The quadratic feature map reproduces (x⋅z+1)2(x\cdot z + 1)^2 to machine precision. A linear perceptron scores 49.6% (chance), while the explicit quadratic perceptron and the kernel perceptron both score 99.95%, with identical predictions.

Exercise 2: random Fourier features (d = 5, γ = 0.2, 200 points). The worst-case kernel error falls from 1.15 at D = 10 to 0.39 (D = 100), 0.12 (D = 1000) and 0.039 (D = 10 000), a factor of about √10 per decade, as Monte Carlo predicts. For regression with m = 1000, exact kernel ridge has a test MSE of 0.027, while ridge on 20, 100 and 500 random features scores 0.31, 0.20 and 0.071. Random features trade accuracy for linear-time training.

🐍 Exercise 2 (research): random Fourier features
Approximate the RBF kernel with D random Fourier features (Bochner), measure the worst-case approximation error as D grows, and compare kernel ridge regression with ridge regression on random features.

Kernels in AI, robotics and aerospace

  • Gaussian processes for terrain mapping, learning dynamics models and Bayesian optimization of controller gains.
  • SVMs with RBF kernels for small-sample classification (fault detection, spectral classification of rocks).
  • Random features and Nyström to scale kernel methods to millions of points.
  • Neural tangent kernel: infinitely wide networks behave like kernel machines (topic B4.5).

References

  1. B. Schölkopf, A. J. Smola (2002). Learning with Kernels. MIT Press.
  2. J. Shawe-Taylor, N. Cristianini (2004). Kernel Methods for Pattern Analysis. Cambridge University Press.
  3. A. Rahimi, B. Recht (2007). “Random features for large-scale kernel machines.” NeurIPS.
  4. M. A. Aizerman, E. M. Braverman, L. I. Rozonoer (1964). “Theoretical foundations of the potential function method in pattern recognition learning.” Automation and Remote Control, 25.
  5. T. Hofmann, B. Schölkopf, A. J. Smola (2008). “Kernel methods in machine learning.” Annals of Statistics, 36(3).