Week 2 · Kernel PCA: curved views of data
Read the words of the video
- Imagine a tiny dot drawn on paper, surrounded by a big ring. Can you draw a straight line to separate the dot from the ring? No matter where you draw, the line will always cut through the ring, right? That’s because simple lines can’t do curvy jobs.
- Now, gently push the paper's middle down, forming a bowl shape. See? The ring goes high, and the dot settles at the bottom. A simple, flat cut at the right height can now easily separate them. We just added a new dimension, 'height', allowing a straight line to work. This is the main idea of kernel PCA!
- Let's try this with numbers. Imagine points on a line: minus one, zero, one in the middle; and minus three, three on the outside. No single point can split them. But if we lift each point using its 'x squared' value, the middle points become low, and the outside points become high. A simple flat cut now separates them.
- For your exam, remember to always 'centre the Gram matrix' before finding its components. Also, the number of components is at most the number of data points, no matter how many 'lifted' dimensions there are. So, kernel PCA helps straight lines do curvy jobs by secretly adding new dimensions.
The one-minute story
Draw a small dot on a sheet of paper and a big ring around it. No straight line can separate "the dot" from "the ring": whichever way you cut, you slice the ring in two. PCA only knows straight lines, so it is stuck.
Now take the paper and push the middle down into a bowl. The ring is now high up, the dot is at the bottom, and one flat cut at the right height separates them perfectly. We did not change the data. We gave it a new dimension (height) where a straight line is enough.
That is kernel PCA. We secretly map each point into a bigger, richer space where PCA's straight lines can do curvy jobs. And here is the miracle, called the kernel trick: we never have to go to that bigger space. A simple formula, the kernel, tells us what the dot products would be there, and dot products are all PCA needs.
By the end of this week you can:
- explain why linear PCA fails on curved data and how a feature map helps;
- define a kernel and compute one from a feature map;
- decide whether a function is a valid kernel (Mercer's condition and closure rules);
- run kernel PCA on a small example, including centring the kernel matrix.
Words you need
- Feature map - a rule that sends each point to a longer list of numbers .
- Kernel - the dot product of two points after mapping.
- Gram (kernel) matrix - the table with .
- Positive semi-definite (PSD) - symmetric with all eigenvalues . Gram matrices must be PSD.
- Kernel trick - using in place of so we never compute .
- RBF / Gaussian kernel - , a similarity score that drops to 0 as points move apart.
Idea 1: Lifting data to where lines work
A tiny 1-D version: the points and (class A) and (class B) sit on a line, and class B is sandwiched inside class A, so no single threshold separates them. Map . Now class B has heights and class A has height : a flat cut at height separates them.
The cost of lifting: the new space can have hundreds or even infinitely many coordinates. Computing directly would be slow or impossible. Enter the kernel.
Idea 2: Kernels
A kernel returns the dot product of the lifted points, computed from the original points only:
Question. For take . Find a feature map , and check it on , .
Solution. Expand: . This is the dot product of
with . Check: , so . Via the map: and , dot product . ✓
So computing the 2-number dot product and squaring gives the same answer as building two 3-number vectors, and for degree 5 in 100 dimensions the saving is astronomical.
Popular kernels
| Kernel | Formula | Notes |
|---|---|---|
| Linear | no lifting at all | |
| Polynomial | , | features are all products up to degree |
| Gaussian / RBF | infinitely many features; ; small means "only very close points are similar" | |
| Laplacian | valid; sharper peak than the Gaussian |
Which functions are legal kernels? (Mercer's condition)
A function is a valid kernel exactly when, for every finite set of points, the Gram matrix is symmetric and positive semi-definite. That is equivalent to being a dot product in some feature space.
Closure rules. If are valid kernels, so are:
- for
- for any function
- and any polynomial of with non-negative coefficients
Not valid in general: , and with .
Quick disproof recipes: (i) not symmetric, like ; (ii) a Gram matrix with a negative eigenvalue or negative determinant; (iii) a negative number on the diagonal ( always).
Question. Decide for 1-D points: (a) ; (b) ; (c) .
Solution.
(a) For the points and : . The diagonal entry is negative (impossible for a dot product), and . Invalid.
(b) is valid (linear), is valid (constant), their sum is valid, and a power is a product of valid kernels. Valid.
(c) is valid; is valid; the sum is valid. Valid.
Idea 3: Kernel PCA
Ordinary PCA needs the eigenvectors of . In the lifted space the same idea gives , which may be gigantic or infinite. The trick:
- Every eigenvector of with non-zero eigenvalue is a combination of the lifted data points: .
- Substituting, the eigenproblem turns into one about the Gram matrix , which is only :
- Dot products are exactly the kernel values. We never touch .
Important: centre in the lifted space too. PCA needs centred data, but we cannot subtract a mean we cannot compute. The fix is to centre the kernel matrix:
(Each entry becomes minus its row mean, minus its column mean, plus the grand mean.)
Kernel PCA algorithm
- Compute ().
- Centre it to get .
- Find the eigenvalues and unit eigenvectors of .
- The coordinate of training point on component is . (The variance of the data along component is .)
Facts: there are at most components (not or the huge feature dimension); with the linear kernel you get ordinary PCA back; and the eigenvalues of are times the eigenvalues of the lifted covariance.
Question. Two points have Gram matrix . Centre it, find its eigenvalues, and give the coordinates of the two points on the first component.
Solution.
- Row means: . Column means: . Grand mean: .
- , so .
- Eigenvalues: trace , determinant : , . Eigenvector for : .
- Coordinates: .
Only one non-zero component, as expected: two points have only one direction between them once centred. (And is PSD; the uncentred has eigenvalues and .)
Your turn
For on 1-D points, find and check with , .
Need a hint?
Expand .
Show the full solution
. For : ; : . Dot: ✓.
and are valid. Which of these must be valid? (a) (b) (c) (d) (e) .
Need a hint?
Look at the list of rules.
Show the full solution
(a) valid, (b) not guaranteed (the difference may have a negative eigenvalue), (c) valid, (d) valid, (e) not valid (a negative multiple; its diagonal ).
. Compute .
Need a hint?
Row, column and grand means are all .
Show the full solution
, so (eigenvalues and ).
For the RBF kernel with : (a) What is ? (b) What happens to as ? (c) Compute for .
Need a hint?
.
Show the full solution
(a) . (b) it tends to . (c) .
You run kernel PCA on 50 data points with the RBF kernel (an infinite-dimensional feature space). At most how many components can have non-zero variance?
Need a hint?
The Gram matrix is .
Show the full solution
At most (and, because of centring, in fact at most non-zero eigenvalues).
Exam radar
MLT Quiz 1 summaries mention kernels in 15 papers, Gaussian kernel in 15, polynomial kernel in 13, kernel functions in 12, Mercer's condition in 8, valid kernels in 8, invalid kernel in 7, kernel trick in 7, and the transformation mapping in 10. Kernel PCA appears in 14 End Term papers.
The most common MSQ is "which of the following are valid kernels?" built from the closure rules (sums and products valid, differences and negative multiples not). Next: "write the feature map for this kernel" and "centre this Gram matrix".
- is not a valid kernel in general.
- Always centre before the eigen-analysis. Using the raw is a different (and wrong) problem.
- The number of kernel PCA components is limited by , the number of points, not the feature dimension.
- A kernel needs and symmetry. A single violation rules it out.
- Small in an RBF kernel very local, can overfit; large nearly constant, can underfit.
Quick quiz
A valid kernel must produce a Gram matrix that is...
- diagonal
- symmetric and positive semi-definite
- invertible
- all positive entries
k1 and k2 are valid kernels. Which is NOT guaranteed to be valid?
- k1 + k2
- k1 times k2
- k1 minus k2
- 5 times k1
The linear kernel in kernel PCA gives...
- nothing
- ordinary PCA
- k-means
- a random projection
Select all TRUE statements about kernel PCA. (select all that apply)
- The kernel matrix must be centred.
- The number of components is at most the number of data points.
- It needs the feature map phi to be computed explicitly.
- It is a supervised method.
Recap
- Linear PCA fails on curved data. Lift points with , then run PCA there.
- Kernel gives lifted dot products without computing .
- Valid Gram matrix symmetric PSD. Closure: , , positive multiples, OK; not.
- Kernel PCA: build , centre to , eigen-analyse; coordinate ; at most components.
- ;
- RBF: ; polynomial
- Valid ,
- Centring:
- Coordinates on component : ; variance along it
- MLF week 4 - The special directions of a matrix: Kernel PCA is eigen-analysis of a kernel matrix.
- MLF week 5 - Symmetric matrices: the well-behaved ones: A function is a valid kernel exactly when its matrix is positive semi-definite.
Practise with real exam questions
These come straight from past papers (with the real figures and the official answer key). Open 23 real questions on this week All Quiz 1 papers