Skip to content
Lambdian

Kernel methods and non-linear SVMs

The kernel trick: linear algorithms in huge feature spaces without ever computing the features.

Animated explanation

The idea

Some classes cannot be split by a line: a ring around a core, a checkerboard. But add the right new features and they can. For a ring, the squared distance from the centre x12+x22x_1^2 + x_2^2 does it: in that feature, one threshold separates the classes, which is a circle back in the original plane.

The SVM dual only ever uses dot products between points. A kernel K(x,z)K(\mathbf{x},\mathbf{z}) computes the dot product of two points in some feature space directly, often without that space being written down at all. Swap the dot product for a kernel and the linear SVM becomes a non-linear one, at no extra cost in the number of parameters.

Why the trick is cheap

The degree-2 polynomial kernel (x⊤z)2(\mathbf{x}^\top\mathbf{z})^2 equals the dot product of all pairwise products xixjx_ix_j: d2d^2 features, computed in O(d)O(d). The RBF kernel corresponds to an infinite-dimensional feature space, yet costs one exponential. The price moves elsewhere: kernel methods need the n×nn\times n Gram matrix, so training scales like O(n2)O(n^2) to O(n3)O(n^3).

Tuning C and γ

For the RBF kernel, γ\gamma sets how far one training point's influence reaches. Small γ\gamma: smooth, nearly linear boundaries (bias). Large γ\gamma: each support vector becomes its own little island (variance). CC interacts with it, so search both on a logarithmic grid, e.g. C∈{0.1,1,10,100}C\in\{0.1, 1, 10, 100\} and γ∈{10−3,…,10}\gamma\in\{10^{-3},\dots,10\}, with cross-validation, on standardised features.

Kernels beyond SVMs

Any algorithm written in terms of dot products can be kernelised: kernel ridge regression, kernel PCA, Gaussian processes, spectral clustering. For very large datasets, random Fourier features or the Nyström method approximate the kernel with explicit features so linear solvers can be used.

The algorithm, step by step

  1. 1
    Standardise features and choose a kernel (RBF by default).
  2. 2
    Build the Gram matrix Kij=K(xi,xj)K_{ij} = K(\mathbf{x}_i, \mathbf{x}_j) (or compute entries on demand).
  3. 3
    Solve the dual: maximise ∑αi−12∑αiαjyiyjKij\sum\alpha_i - \frac12\sum\alpha_i\alpha_jy_iy_jK_{ij} subject to 0≤αi≤C0\le\alpha_i\le C, ∑αiyi=0\sum\alpha_iy_i = 0.
  4. 4
    Predict sign⁡(∑iαiyiK(xi,x)+b)\operatorname{sign}\big(\sum_i\alpha_iy_iK(\mathbf{x}_i,\mathbf{x}) + b\big), summing over support vectors only.

The maths

Kernelised decision function

f(x)=∑i∈SVαiyiK(xi,x)+bf(\mathbf{x}) = \sum_{i\in SV}\alpha_iy_iK(\mathbf{x}_i, \mathbf{x}) + b

RBF kernel

K(x,z)=exp⁡(−γ∥x−z∥2)K(\mathbf{x},\mathbf{z}) = \exp(-\gamma\lVert\mathbf{x}-\mathbf{z}\rVert^2)

Polynomial kernel

K(x,z)=(x⊤z+c)dK(\mathbf{x},\mathbf{z}) = (\mathbf{x}^\top\mathbf{z} + c)^d

Explicit degree-2 map in 2-D

(x⊤z)2=ϕ(x)⊤ϕ(z),  ϕ(x)=(x12, 2 x1x2, x22)(\mathbf{x}^\top\mathbf{z})^2 = \phi(\mathbf{x})^\top\phi(\mathbf{z}),\ \ \phi(\mathbf{x}) = (x_1^2,\ \sqrt2\,x_1x_2,\ x_2^2)

Strengths and weaknesses

Strengths

  • Flexible non-linear boundaries with a convex problem
  • Works with any similarity, including strings and graphs
  • Strong accuracy on small and medium datasets

Weaknesses

  • Gram matrix: memory and time grow quadratically or worse with n
  • Two hyperparameters to tune jointly
  • Harder to interpret than linear models

Reach for it when…

Non-linear problems with up to tens of thousands of examples, especially with few features or a natural similarity measure.

Check your understanding

Question 1

Q1Why can the SVM be 'kernelised'?

Question 2

Q2An RBF SVM has 100% training accuracy and poor validation accuracy. What should you try?

Question 3

Q3What is the main cost of kernel methods on large datasets?

Lambdian Lab

Kernels and the Gram matrix

Build kernels and verify the kernel trick, then use scikit-learn's SVC with your own kernel.

  • rbf_kernel(A, B, gamma) → matrix exp⁡(−γ∥a−b∥2)\exp(-\gamma\lVert a-b\rVert^2).
  • poly2_features(X): the explicit degree-2 map (x12,2x1x2,x22)(x_1^2, \sqrt2x_1x_2, x_2^2) for 2-D inputs.
  • poly2_kernel(A, B) = (AB⊤)2(A B^\top)^2; it must equal poly2_features(A) @ poly2_features(B).T.
  • best_gamma(X_tr, y_tr, X_va, y_va, gammas): fit SVC(kernel="precomputed", C=1) with your RBF Gram matrices; return the γ with the best validation accuracy.
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.