Week 6 · SVD and PCA: squeezing data
Read the words of the video
- Imagine kneading dough. You turn it, stretch it long in one direction, then turn it again. This simple three-step recipe is exactly how complex data transformations work! We call this amazing process Singular Value Decomposition, or SVD. It helps us understand how any matrix changes space.
- SVD helps us find the most important 'stretches' in data. Like a photo: most information is in the biggest stretches. Discarding smaller stretches makes a smaller file, still looking great! PCA uses this for data points: finding the view that shows maximum spread, using SVD's powerful idea.
- Let’s look at three data points like beads on a string. First, we center them around the middle. Then, using PCA, we find the best line through them. This line shows 100 percent of their spread, meaning all their movement is perfectly captured by just one direction!
- For exams, remember to center data for PCA and carefully calculate covariance. SVD helps you understand matrix ranks and compression. PCA finds the best data views. Master these concepts to unlock the secrets of data and matrices. You've got this! Keep practicing!
The one-minute story
Knead a ball of dough: you turn it, stretch it along one direction, then turn it again. Any matrix, however messy, acts on space exactly like that: a turn, a stretch along the coordinate axes, and another turn. This three-step recipe is the singular value decomposition (SVD). The stretch amounts are called singular values, and the big ones tell you where the "action" of the matrix lives.
Now think of a photograph, which is just a big table of brightness numbers (a matrix). Most of its information sits in the first few stretch amounts; the rest is fine detail you would hardly miss. Throw the small ones away and you get a smaller file that still looks almost identical. That is image compression.
PCA (principal component analysis) uses the same idea on a cloud of data points: "Which flat view of this cloud shows the most spread?" You will find the answer is the SVD (or the eigenvectors of the covariance matrix) of the data.
By the end of this week you can:
- state the SVD and compute singular values from ;
- connect rank with the number of non-zero singular values;
- build the best low-rank approximation and count how much storage it saves;
- compute a covariance matrix and run PCA by hand on small data;
- compute variance explained and reconstruction error.
Words you need
- Singular values - the stretch amounts of the SVD. Never negative.
- Singular vectors - the columns of (input directions) and (output directions).
- Low-rank approximation - replacing a matrix by a simpler one that uses only its biggest singular values.
- Centering - subtracting the average from each feature so the data cloud is centred at the origin.
- Covariance matrix - a table of how every pair of features moves together; the diagonal holds the variances.
- Principal component - a direction along which the data is spread out the most.
- Variance explained - the share of the total spread captured by the chosen directions.
Idea 1: The singular value decomposition
Every matrix (any shape, any rank) can be written
- is an orthogonal matrix (the first turn),
- is an matrix that is zero except for the non-negative numbers on its diagonal (the stretch),
- is an orthogonal matrix (the final turn).
How to get them:
- singular values ;
- right singular vectors (columns of ) are the eigenvectors of ;
- left singular vectors (columns of ) are the eigenvectors of , with .
is symmetric and positive semi-definite (week 5), so its eigenvalues are real and , which is why the square roots make sense.
Rank: the rank of equals the number of non-zero singular values.
Writing it out term by term:
Each term is a rank-1 piece, and they are listed from biggest to smallest.
Question. Find the singular values of and write as .
Solution.
- . Trace , determinant . Eigenvalues and .
- Singular values: and . Only one is non-zero, so (indeed the second column is twice the first).
- Eigenvector of for : , so .
- .
- So . ✓
Singular values are not eigenvalues. For the eigenvalues are and , but the singular values are and , because singular values can never be negative and come from . They coincide with eigenvalues only for symmetric positive semi-definite matrices. Exam options love "singular values are the eigenvalues of ".
Facts that appear as true/false statements
- SVD exists for every matrix, square or not.
- always. The largest, , is the biggest amount can stretch a unit vector: .
- is of rank exactly non-zero singular values (and zero ones).
- A matrix of rank has non-zero singular values, so () is invertible. That is exactly the condition for a unique least-squares solution (week 3).
Low-rank approximation and image compression
Eckart-Young theorem: among all matrices of rank , the closest one to is obtained by keeping only the first terms of the sum above. The error is made only of the discarded singular values: .
Question. A grey-scale image is a matrix. You keep only its top singular values. How many numbers must you store, compared with the original?
Solution. The original needs numbers. The rank-5 version needs, for each , one , one -long and one -long : numbers. About 10 % of the storage, and for a typical photo the picture is still recognisable.
Idea 2: PCA, finding the best view of a data cloud
Say you have data points, each with features, stored as the columns of a matrix .
Step 1: Centre. Subtract the mean of each feature, so the cloud is centred at the origin. (Skip this and PCA is wrong: the first "component" would just point at the average.)
Step 2: Covariance matrix.
The diagonal entries are the variances of each feature, and measures how features and rise and fall together. is symmetric and positive semi-definite.
Step 3: Eigen-analysis. The principal components are the eigenvectors of , ordered by eigenvalue. The eigenvalue is the variance of the data along component .
Why? The spread of the data along a unit direction is . We want the with that makes this as big as possible. Week 9's Lagrange multipliers will show the answer satisfies and the value reached is . The biggest spread is the top eigenvalue and its eigenvector.
Two ways to say what PCA does
- Maximise the spread (variance) of the shadows on the chosen axes.
- Minimise the reconstruction error: the squared distance between each point and its shadow.
Since each point satisfies (Pythagoras), these are the same problem. The animation shows it: the total never changes, so as shadow-spread rises, the loss falls.
With eigenvalues of :
- variance explained by the top components
- reconstruction error (average squared) when keeping components (the discarded eigenvalues)
- projecting onto the top components : coordinates , reconstruction (a projection matrix, week 3)
The link to SVD. If is the SVD of the centred data matrix, then . So the principal components are the left singular vectors , and . PCA and SVD are two names for one computation.
Question. The data points are . Find , the principal components and the variance explained by the first.
Solution.
- Means: , . Centred points: .
- , , . With : .
- Trace , determinant , so eigenvalues and .
- PC1 , the direction of the line. PC2 .
- Variance explained by PC1 . Reconstruction error with one component : the points lie exactly on a line, so one number per point loses nothing.
The covariance entry is a number you will see again and again in past papers.
Question. A data set has covariance matrix . (a) Find the principal components. (b) What fraction of the variance does PC1 explain? (c) Project the centred point onto PC1 and reconstruct it. (d) What is the variance along the direction ?
Solution.
- Trace 6, determinant : , so . Eigenvectors: for and for .
- (b) .
- (c) Coordinate on PC1: . Reconstruction: . Error vector , squared length (which equals the squared coordinate on PC2, ).
- (d) Variance along any unit direction is . For that is just . For : , the maximum, as expected.
True statements about PCA (the exam's favourite MSQ)
- It is unsupervised (no labels used).
- Components are orthogonal (eigenvectors of a symmetric matrix) and the projected features are uncorrelated.
- Variance explained by a component is its eigenvalue divided by the sum of all eigenvalues.
- You must centre the data; if features have different units you should also scale them.
- With points and features there are at most useful components (centering removes one more: rank of the centred data is at most ).
- PCA is not linear regression: regression measures vertical errors to predict ; PCA measures perpendicular distances and treats all features alike.
Your turn
for some matrix . What are the singular values of , and what is the sum of the singular values?
Need a hint?
.
Show the full solution
Eigenvalues give ; the sum is . (The trap option "singular values equal the eigenvalues of " is not generally true.)
is with singular values . What is its rank, and is invertible?
Need a hint?
Count the non-zero singular values.
Show the full solution
All 8 are non-zero, so rank and () has rank 8, so it is invertible: least squares has a unique solution.
A image is approximated with its top singular values. How many numbers are stored, and what fraction of the original?
Need a hint?
Each rank-1 piece needs one , one (length 200) and one (length 300).
Show the full solution
numbers. The original has . So about .
Points (already centred). Find , its eigenvalues, and the variance explained by PC1.
Need a hint?
Sum , , and divide by .
Show the full solution
, , . , already diagonal, so eigenvalues and . PC1 (the -axis). Variance explained .
For , what is the variance of the data when projected on the unit vector ?
Need a hint?
Compute .
Show the full solution
. Then . (In fact is an eigenvector with eigenvalue 6: it is PC1.)
Exam radar
MLF Quiz 2: (of the 30 Quiz 2 papers that have a concept summary) SVD in all 30, PCA in 29, covariance matrix in 23, eigenvalues of the covariance matrix in 7, projection onto principal components in 3. In the End Term, "which is true about PCA" (an MSQ) appears about 22 times, covariance of given points about 11 times, and projected-variance computations about 9 times; SVD true/false MSQs another 6.
Reliable question types: compute from 3 to 4 points (watch the ), eigenvalues of a covariance matrix, " from ", "rank of a matrix", and "which statements about PCA are true".
- Divide by (or ) when forming , according to what the question says. In this course papers use .
- Reconstruction error = the sum of the discarded eigenvalues, not the kept ones.
- Singular values eigenvalues of .
- Forgetting to centre the data before computing .
- Principal components are eigenvectors of the covariance matrix, not of the raw data matrix.
Quick quiz
A matrix has singular values 6, 3 and 0. Its rank is...
- 3
- 2
- 1
- 9
The principal components of centred data are...
- the rows of the data matrix
- the eigenvectors of the covariance matrix
- the singular values
- the mean vector
The covariance matrix has eigenvalues 5, 3 and 2. Keeping the first component, the variance explained is...
- 50 percent
- 30 percent
- 20 percent
- 100 percent
Select all that are TRUE about PCA. (select all that apply)
- It is an unsupervised method.
- The principal components are mutually orthogonal.
- The data does not need to be centred.
- Reconstruction error equals the sum of the kept eigenvalues.
Recap
- SVD: for any matrix. . Rank = number of non-zero .
- Best rank- approximation: keep the top terms; storage .
- PCA: centre covariance eigenvectors = components, eigenvalues = variances.
- Variance explained ; reconstruction error .
- PCA = SVD of the centred data, .
- ; ;
- Rank- storage: ; error
- Covariance (centred): ; variance along unit :
- Variance explained: ; reconstruction error:
- MLT week 1 - PCA: finding the best view of your data: SVD is the machinery under PCA.
- MLP week 12 - Unsupervised learning in sklearn: PCA in sklearn is an SVD behind the scenes.
Practise with real exam questions
These come straight from past papers (with the real figures and the official answer key). Open 28 real questions on this week All Quiz 2 papers