Kernel PCA

Kernel PCA runs PCA in a kernel's feature space using only the n×n kernel matrix: centre it, eigen-decompose it, rescale the eigenvectors, and read each point's coordinates off its row of the kernel matrix. The principal directions themselves are never built, but the projections onto them are all a downstream task needs.

Machine Learning Techniques

We now have every ingredient. PCA can be computed from an n×nn \times n matrix of dot products, and a kernel supplies dot products in a feature space we never have to construct. Putting the two together gives kernel PCA: non-linear dimensionality reduction at the cost of an n×nn \times n eigen-decomposition.

The algorithm, first draft

Input: data x1,…,xn∈Rdx_1, \dots, x_n \in \mathbb{R}^d and a valid kernel kk.

  1. Build the kernel matrix K∈Rn×nK \in \mathbb{R}^{n \times n} with Kij=k(xi,xj)K_{ij} = k(x_i, x_j).
  2. Eigen-decompose it: unit eigenvectors β1,β2,…\beta_1, \beta_2, \dots with eigenvalues nλ1≥nλ2≥…n\lambda_1 \ge n\lambda_2 \ge \dots
  3. Rescale: αk=βk/nλk\alpha_k = \beta_k / \sqrt{n\lambda_k}.

In ordinary PCA the next step was wk=Xαkw_k = X\alpha_k. Here the same step would read

wk=∑j=1nαk,j ϕ(xj),w_k = \sum_{j=1}^{n} \alpha_{k,j}\, \phi(x_j),

and that needs ϕ\phi, which we are not allowed to compute (for the RBF kernel it is infinite-dimensional). So we cannot write down the principal directions in the feature space.

We never needed the directions

What do we actually use PCA for? Usually not for the directions themselves, but for each point's coordinates along them: the compressed representation. The coordinate of point xix_i on the kk-th direction is

ϕ(xi)⊤wk=∑j=1nαk,j ϕ(xi)⊤ϕ(xj)=∑j=1nαk,j Kij.\phi(x_i)^\top w_k = \sum_{j=1}^{n} \alpha_{k,j}\, \phi(x_i)^\top \phi(x_j) = \sum_{j=1}^{n} \alpha_{k,j}\, K_{ij} .

Only kernel values appear. So each point's new representation is

xi  ⟼  (∑jα1,jKij,    ∑jα2,jKij,    …,    ∑jαℓ,jKij)∈Rℓ.x_i \;\longmapsto\; \Big( \textstyle\sum_j \alpha_{1,j} K_{ij},\;\; \sum_j \alpha_{2,j} K_{ij},\;\; \dots,\;\; \sum_j \alpha_{\ell,j} K_{ij} \Big) \in \mathbb{R}^{\ell} .

Read it this way: row ii of KK lists how similar xix_i is to every training point, and each component weights those similarities in its own way. A new point xx is handled the same way, using the similarities k(x,xj)k(x, x_j) to the training points.

What we give up is reconstruction. Turning coefficients back into a point needs the directions wkw_k, which live in the feature space. For most uses (visualisation, clustering, or feeding a classifier) the coordinates are what matter, and we have them.

Note that ℓ\ell can exceed the original dd. With two-dimensional data and an RBF kernel you might keep ten components. That is not a contradiction: the data has been lifted to a much larger space where its curved structure is flat, and ten coordinates in that space can describe it far better than the original two.

The missing step: centring in feature space

Ordinary PCA centred the data first. Kernel PCA needs the mapped points ϕ(xi)\phi(x_i) centred, but we cannot subtract their mean μϕ=1n∑iϕ(xi)\mu_\phi = \tfrac1n\sum_i \phi(x_i) because we cannot compute any of them. Fortunately the dot products of the centred points can be written using only kernel values:

(ϕ(xi)−μϕ)⊤(ϕ(xj)−μϕ)=Kij−1n∑mKim−1n∑mKmj+1n2∑m,lKml.\big(\phi(x_i) - \mu_\phi\big)^\top\big(\phi(x_j) - \mu_\phi\big) = K_{ij} - \frac1n\sum_{m} K_{im} - \frac1n\sum_{m} K_{mj} + \frac{1}{n^2}\sum_{m,l} K_{ml} .

Each term comes from expanding the product: the original kernel value, minus the average similarity of xix_i to all points, minus that of xjx_j, plus the overall average. In matrix form, with 1n\mathbf{1}_n the n×nn \times n matrix whose every entry is 1/n1/n,

Kc=K−1nK−K1n+1nK1n.K_c = K - \mathbf{1}_n K - K \mathbf{1}_n + \mathbf{1}_n K \mathbf{1}_n .

Use KcK_c in place of KK from step 2 onwards.

Kernel PCA, complete

  1. Compute Kij=k(xi,xj)K_{ij} = k(x_i, x_j).
  2. Centre it: Kc=K−1nK−K1n+1nK1nK_c = K - \mathbf{1}_n K - K\mathbf{1}_n + \mathbf{1}_n K \mathbf{1}_n.
  3. Eigen-decompose KcK_c: unit eigenvectors βk\beta_k, eigenvalues nλkn\lambda_k, largest first.
  4. Rescale: αk=βk/nλk\alpha_k = \beta_k / \sqrt{n\lambda_k}.
  5. Represent each training point by its ℓ\ell coordinates ∑jαk,j(Kc)ij\sum_j \alpha_{k,j} (K_c)_{ij}, for k=1,…,ℓk = 1, \dots, \ell.
Flow diagram: data points go into a kernel function to build an n by n kernel matrix, which is centred, eigen-decomposed and rescaled; each point's row of the kernel matrix times the weights gives its new coordinates. A dashed box labelled feature space is never computed
Kernel PCA works entirely through the kernel matrix. The feature space (dashed) is never built: each point's new coordinates come from its similarities to the training points.

Choosing the kernel

The kernel decides what structure kernel PCA can find. A polynomial kernel of degree 2 can flatten circles, ellipses and parabolas; higher degrees capture more intricate surfaces. The RBF kernel can follow almost any smooth shape, with σ\sigma controlling how local it is: a small σ\sigma makes every point similar only to its immediate neighbours (very flexible, prone to fitting noise), a large σ\sigma makes the kernel behave almost linearly. With a linear kernel, k(x,x′)=x⊤x′k(x, x') = x^\top x', kernel PCA is exactly ordinary PCA.

The bigger lesson

Look at how kernel PCA came about. We solved the problem for linear structure, noticed the solution needed only dot products between data points, and swapped those dot products for a kernel. That recipe, solve it linearly, check that only dot products appear, then kernelise, is general. We will apply it again to regression in Module 6 and to support vector machines in Module 10.

Try it yourself
Machine Learning Lab: feature maps →
The "All degree 2" map is the feature space of the quadratic kernel. Compare its first principal component with ordinary PCA on rings and moons.
MediumKernel PCA

Why can kernel PCA give each point's coordinates but not reconstruct the point?

EasyKernel PCAPCA

What does kernel PCA with the linear kernel k(x, x') = xᵀx' compute?

HardKernel PCAModel selection

How would you choose σ for an RBF kernel in kernel PCA used before a classifier?