Week 1 · PCA: finding the best view of your data
Read the words of the video
- Imagine taking a photo of a cool 3D statue for a flat postcard. Most pictures make it look squashed or boring. But there's one special angle where the statue looks its widest and clearest, so you can still easily tell what it is. PCA is like finding that perfect angle for your data.
- Imagine describing each student with 100 quiz marks. That's too many numbers! Many quizzes might even measure similar things. PCA helps us find just a few new numbers, like two or three, that capture most of the important information. It finds directions where our data changes the most, keeping only what truly matters.
- Let's see how much information we keep! If our data has 'spread' values of five, three, and two, the total spread is ten. Keeping the first direction explains 50% of the spread. Keeping the first two directions explains 80%. So, to keep at least 75% of the information, we need two directions!
- For exams, remember to always 'centre' your data first! And, reconstruction error means the information you *discarded*, not what you kept. PCA is unsupervised, finding hidden patterns. It picks directions with the most 'spread,' making complex data simple and clear, like finding the best view of a statue!
The one-minute story
A museum wants to put a 3D statue on a postcard, which is flat. Photograph it from the front, the top, the side... Most angles make the statue look squashed and boring. But one angle shows it spread out as widely as possible, so you can still tell what it is. PCA finds that best angle for any cloud of data.
Now imagine each student is described by 100 numbers (marks in 100 quizzes). You cannot picture a point in 100 dimensions, and many of the quizzes say nearly the same thing. PCA finds a handful of new numbers (maybe 2 or 3) that keep most of the information. Fewer numbers means faster models, cleaner pictures and less noise.
The idea in one line: look for the directions along which the data varies the most, and keep those.
Quick refresher from MLF (skip if fresh).
- A projection onto a direction (unit length) gives the coordinate ; the "shadow" is .
- A symmetric matrix has real eigenvalues and perpendicular eigenvectors.
- is a quadratic form; for the top eigenvector it equals the largest eigenvalue.
By the end of this week you can:
- explain what unsupervised learning and representation learning mean;
- run PCA step by step: centre, covariance, eigen-analysis, project;
- compute variance explained and reconstruction error;
- explain why "maximise variance" and "minimise reconstruction error" are the same.
Words you need
- Unsupervised learning - finding structure in data that has no labels.
- Representation learning - turning raw data into a better set of numbers (features) for later use.
- Data matrix - here : each column is one data point with features.
- Centring - subtracting the mean so the cloud is centred at the origin.
- Covariance matrix - how the features vary and move together.
- Principal component (PC) - a unit direction of high variance; PC1 has the most, PC2 the next, and PC2 is perpendicular to PC1.
- Reconstruction - rebuilding a data point from its few PCA coordinates.
Idea 1: The setup
You have data points . Stack them as the columns of .
Step 1: Centre. Compute the mean and replace every point by . From now on the data has mean zero.
Step 2: Covariance.
(If your paper stores points as rows, the same matrix is . Read the convention in the question.)
Step 3: The question PCA answers. Project onto a unit direction : each point becomes the number . The spread (variance) of those numbers is
Which unit makes this largest? Maximise subject to . The Lagrange method (MLF week 9) gives : is an eigenvector of , and the variance achieved is its eigenvalue . So the best direction is the eigenvector with the largest eigenvalue. That is PC1. The next best direction perpendicular to PC1 is the eigenvector with the second largest eigenvalue, and so on.
The PCA recipe
- Centre the data.
- Form .
- Find its eigenvalues and unit eigenvectors .
- Keep the top eigenvectors: .
- New coordinates of a point: ( numbers). Reconstruction: .
Idea 2: How good is the squashed version?
Variance explained. The total variance is . Keeping components retains
of it. People pick the smallest that keeps, say, 90% or 95%.
Reconstruction error. The average squared distance between points and their reconstructions is
Why are "most spread out" and "least squashing loss" the same question? Each point has a length from the centre. That length splits into the shadow on your axis and the leftover sticking out sideways, and Pythagoras says . The total length of the cloud is fixed. So whatever you gain in shadow you lose in leftover. Maximising shadow-spread is the same as minimising leftover.
Question. Three points in the plane are , , . Find the principal components, the variance explained by PC1, and the reconstruction error if we keep only PC1.
Solution.
- Mean: . Centred points: .
- Covariance: , , . With : .
- Eigenvalues: trace , determinant : , so , .
- Eigenvectors: for 9: gives . For 3: .
- Variance explained by PC1: .
- Reconstruction error with only PC1: the discarded eigenvalue, . Check it directly. Coordinates on PC1: (that is ). Reconstructions: , , . Errors: , , . Average . ✓
Question. A covariance matrix has eigenvalues . (a) Variance explained by PC1? (b) By the first two? (c) Minimum for at least 75%?
Solution. Total . (a) . (b) . (c) one component gives , two give , so .
Question. . What is the variance of the data along and along ?
Solution. Variance along a unit direction is . For : . For : (it is PC2, so its variance is the smaller eigenvalue, ).
Idea 3: Practical notes and limits
- Centre first. Without it the "first component" just points at the average and the covariance formula is wrong.
- Scale matters. PCA chases variance, so a feature measured in thousands drowns out one measured in fractions. Standardise (divide each feature by its standard deviation) when units differ.
- Components are uncorrelated. The new coordinates have zero covariance with each other, and .
- At most useful components (centred data of points has rank at most ).
- PCA is linear: it can only find straight directions. A curved structure (like two concentric rings) fools it. The fix is kernel PCA (next week).
- It is unsupervised: it never looks at labels, so the directions it keeps need not help classification.
- PCA is not regression. Regression minimises vertical errors to predict . PCA minimises perpendicular distances and treats every feature alike.
Link to SVD (MLF week 6). If is the SVD of the centred matrix, the principal directions are the columns of and .
Your turn
Points are already centred. Find , the eigenvalues, PC1 and the variance explained by PC1.
Need a hint?
Sum , , , divide by .
Show the full solution
, , . . Eigenvalues and . PC1 is the -axis . Variance explained .
Raw data: . Find the mean, the centred data and the covariance matrix. How many non-zero eigenvalues will there be?
Need a hint?
All three points lie on one line.
Show the full solution
Mean . Centred: . , , . , trace , determinant . Eigenvalues and : one non-zero eigenvalue, because the data lies exactly on a line. PC1 , explaining .
has eigenvalues . We keep 2 components. Give the variance explained and the average reconstruction error.
Need a hint?
Error = sum of the discarded eigenvalues.
Show the full solution
Total . Explained . Reconstruction error .
With as PC1, find the PC1 coordinate and reconstruction of the centred point , and the squared error.
Need a hint?
Coordinate .
Show the full solution
. Reconstruction . Error vector , squared length .
(a) PCA needs labels. (b) PC1 and PC2 are perpendicular. (c) PCA can be done without centring and still give the right answer. (d) Reconstruction error with components is the sum of the retained eigenvalues.
Need a hint?
Go back to the recipe.
Show the full solution
(a) False, it is unsupervised. (b) True, eigenvectors of a symmetric matrix. (c) False, you must centre. (d) False: it is the sum of the discarded eigenvalues.
Exam radar
In MLT Quiz 1, the concept summaries mention PCA in 25 of 60 papers, covariance matrix in 18, PCA variance in 11, principal components in 10, variance explained in 9, reconstruction error in 9. PCA also appears in 14 End Term papers (with kernel PCA).
Typical items: compute a covariance from a few points and find its eigenvalues; variance explained by components; reconstruct a point from PC1; "which statements about PCA are true"; the data is not centred, what changes?
- Reconstruction error = discarded eigenvalues, not the kept ones.
- Eigenvectors for PCA must be unit length before you project.
- Forgetting to centre is the most common blunder.
- "PCA minimises the sum of vertical distances" is false: it is perpendicular distances.
- Check whether the paper's uses or ; the question will say.
Quick quiz
Principal components of a data set are the eigenvectors of...
- the data matrix
- the covariance matrix of the centred data
- the identity matrix
- the label vector
Covariance eigenvalues are 6, 3, 1. Variance explained by the first component is...
- 30 percent
- 50 percent
- 60 percent
- 90 percent
Keeping 1 of 3 components with eigenvalues 6, 3, 1, the average reconstruction error is...
- 6
- 3
- 4
- 1
Select all TRUE statements about PCA. (select all that apply)
- It is an unsupervised method.
- The principal components are mutually perpendicular.
- Centring the data is optional.
- It can only be used for classification.
Recap
- PCA finds the directions of maximum variance: the eigenvectors of (after centring), ordered by eigenvalue.
- Coordinates ; reconstruction .
- Variance explained ; reconstruction error .
- Max variance min reconstruction error (Pythagoras). PCA is linear and unsupervised.
- Centre: ;
- Variance along unit : ; best : , top
- Coordinates ; reconstruction
- Variance explained ; error
- SVD link:
- MLF week 1 - What is machine learning, and which maths do we need?: MLT starts by treating every data point as a vector. Vectors, lengths and dot products are MLF week 1.
- MLF week 3 - Vectors, matrices and the best-fit line: PCA's 'reconstruction' is a projection - exactly the shadow idea from MLF week 3.
- MLF week 4 - The special directions of a matrix: Principal components ARE eigenvectors of the covariance matrix.
- MLF week 5 - Symmetric matrices: the well-behaved ones: Covariance matrices are symmetric, so the spectral theorem promises real eigenvalues and perpendicular principal components.
- MLF week 6 - SVD and PCA: squeezing data: SVD is the machinery under PCA.
- MLP week 12 - Unsupervised learning in sklearn: PCA in scikit-learn.
Practise with real exam questions
These come straight from past papers (with the real figures and the official answer key). Open 32 real questions on this week All Quiz 1 papers