ML Study Hub

Home / MLF / Week 6

Week 6 · SVD and PCA: squeezing data

Official syllabus line: Linear Algebra - Singular value decomposition, Principal Component Analysis in Image Processing
About 90 minQuiz 2 material (see exam radar)BSCS2004 · Harish Guruprasad Ramaswamy, Arun Rajkumar, Prashanth LA
Before you start: Weeks 3, 4 and 5 (projection, eigenvectors, symmetric matrices).
Watch first: the animated story of this weekAbout 1-2 minutes of real 3D animation with a friendly voice. Pause any time.
Read the words of the video
  1. 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.
  2. 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.
  3. 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!
  4. 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

Think of it like this

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 A=UΣVTA=U\Sigma V^T and compute singular values from ATAA^TA;
  • 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

Words you need
  • Singular values σ1≥σ2≥⋯≥0\sigma_1\ge\sigma_2\ge\dots\ge0 - the stretch amounts of the SVD. Never negative.
  • Singular vectors - the columns of VV (input directions) and UU (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

The idea

Every matrix AA (any shape, any rank) can be written

A=U Σ VTA=U\,\Sigma\,V^T
  • VV is an n×nn\times n orthogonal matrix (the first turn),
  • Σ\Sigma is an m×nm\times n matrix that is zero except for the non-negative numbers σ1≥σ2≥…\sigma_1\ge\sigma_2\ge\dots on its diagonal (the stretch),
  • UU is an m×mm\times m orthogonal matrix (the final turn).

How to get them:

  • singular values σi=eigenvalues of ATA\sigma_i=\sqrt{\text{eigenvalues of }A^TA};
  • right singular vectors (columns of VV) are the eigenvectors of ATAA^TA;
  • left singular vectors (columns of UU) are the eigenvectors of AATAA^T, with ui=Avi/σi\mathbf u_i=A\mathbf v_i/\sigma_i.

ATAA^TA is symmetric and positive semi-definite (week 5), so its eigenvalues are real and ≥0\ge0, which is why the square roots make sense.

Rank: the rank of AA equals the number of non-zero singular values.

Writing it out term by term:

A=σ1u1v1T+σ2u2v2T+⋯+σrurvrTA=\sigma_1\mathbf u_1\mathbf v_1^T+\sigma_2\mathbf u_2\mathbf v_2^T+\cdots+\sigma_r\mathbf u_r\mathbf v_r^T

Each term is a rank-1 piece, and they are listed from biggest to smallest.

Solved example: 1. SVD of a rank-1 matrix

Question. Find the singular values of A=(1224)A=\begin{pmatrix}1&2\\2&4\end{pmatrix} and write AA as σ uvT\sigma\,\mathbf u\mathbf v^T.

Solution.

  1. ATA=(5101020)A^TA=\begin{pmatrix}5&10\\10&20\end{pmatrix}. Trace 2525, determinant 100−100=0100-100=0. Eigenvalues 2525 and 00.
  2. Singular values: σ1=25=5\sigma_1=\sqrt{25}=5 and σ2=0\sigma_2=0. Only one is non-zero, so rank⁡A=1\operatorname{rank}A=1 (indeed the second column is twice the first).
  3. Eigenvector of ATAA^TA for 2525: (1,2)/5(1,2)/\sqrt5, so v1=15(1,2)\mathbf v_1=\frac1{\sqrt5}(1,2).
  4. u1=Av1/σ1=15⋅15(5,10)=15(1,2)\mathbf u_1=A\mathbf v_1/\sigma_1=\frac{1}{5}\cdot\frac1{\sqrt5}(5,10)=\frac1{\sqrt5}(1,2).
  5. So A=5⋅15(12)⋅15(1  2)=(1224)A=5\cdot\frac1{\sqrt5}\binom12\cdot\frac1{\sqrt5}(1\;2)=\begin{pmatrix}1&2\\2&4\end{pmatrix}. ✓
Watch out - real exam trap

Singular values are not eigenvalues. For B=(300−2)B=\begin{pmatrix}3&0\\0&-2\end{pmatrix} the eigenvalues are 33 and −2-2, but the singular values are 33 and 22, because singular values can never be negative and come from BTB=diag⁡(9,4)B^TB=\operatorname{diag}(9,4). They coincide with eigenvalues only for symmetric positive semi-definite matrices. Exam options love "singular values are the eigenvalues of AA".

Facts that appear as true/false statements

  • SVD exists for every matrix, square or not.
  • σi≥0\sigma_i\ge0 always. The largest, σ1\sigma_1, is the biggest amount AA can stretch a unit vector: ∥A∥2=σ1\|A\|_2=\sigma_1.
  • AA is m×nm\times n of rank rr ⇒\Rightarrow exactly rr non-zero singular values (and min⁡(m,n)−r\min(m,n)-r zero ones).
  • A 10×810\times8 matrix of rank 88 has 88 non-zero singular values, so ATAA^TA (8×88\times8) 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 kk, the closest one to AA is obtained by keeping only the first kk terms of the sum above. The error is made only of the discarded singular values: ∥A−Ak∥F2=σk+12+σk+22+⋯\|A-A_k\|_F^2=\sigma_{k+1}^2+\sigma_{k+2}^2+\cdots.

Solved example: 2. How much does compression save?

Question. A grey-scale image is a 100×100100\times100 matrix. You keep only its top 55 singular values. How many numbers must you store, compared with the original?

Solution. The original needs 100×100=10,000100\times100=10{,}000 numbers. The rank-5 version ∑i=15σiuiviT\sum_{i=1}^5\sigma_i\mathbf u_i\mathbf v_i^T needs, for each ii, one σi\sigma_i, one 100100-long ui\mathbf u_i and one 100100-long vi\mathbf v_i: 5×(100+100+1)=10055\times(100+100+1)=1005 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 nn data points, each with dd features, stored as the columns of a d×nd\times n matrix XX.

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.

C=1n∑i=1nxixiT=1nXXTC=\frac1n\sum_{i=1}^n\mathbf x_i\mathbf x_i^T=\frac1nXX^T

The diagonal entries CjjC_{jj} are the variances of each feature, and CjkC_{jk} measures how features jj and kk rise and fall together. CC is symmetric and positive semi-definite.

Step 3: Eigen-analysis. The principal components are the eigenvectors of CC, ordered by eigenvalue. The eigenvalue λk\lambda_k is the variance of the data along component kk.

Why? The spread of the data along a unit direction u\mathbf u is 1n∑(uTxi)2=uTCu\frac1n\sum(\mathbf u^T\mathbf x_i)^2=\mathbf u^TC\mathbf u. We want the u\mathbf u with ∥u∥=1\|\mathbf u\|=1 that makes this as big as possible. Week 9's Lagrange multipliers will show the answer satisfies Cu=λuC\mathbf u=\lambda\mathbf u and the value reached is λ\lambda. The biggest spread is the top eigenvalue and its eigenvector.

Try it yourself

The black dots are your data, already centred. Turn the axis and watch the spread of the orange shadows. The red sticks show what you lose by squashing onto the axis.

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 ∥x∥2=∥shadow∥2+∥leftover∥2\|\mathbf x\|^2=\|\text{shadow}\|^2+\|\text{leftover}\|^2 (Pythagoras), these are the same problem. The animation shows it: the total never changes, so as shadow-spread rises, the loss falls.

The idea

With eigenvalues λ1≥λ2≥⋯≥λd\lambda_1\ge\lambda_2\ge\dots\ge\lambda_d of CC:

  • variance explained by the top kk components =λ1+⋯+λkλ1+⋯+λd=\dfrac{\lambda_1+\dots+\lambda_k}{\lambda_1+\dots+\lambda_d}
  • reconstruction error (average squared) when keeping kk components =λk+1+⋯+λd=\lambda_{k+1}+\dots+\lambda_d (the discarded eigenvalues)
  • projecting x\mathbf x onto the top kk components W=[w1⋯wk]W=[\mathbf w_1\cdots\mathbf w_k]: coordinates WTxW^T\mathbf x, reconstruction WWTxWW^T\mathbf x (a projection matrix, week 3)

The link to SVD. If X=UΣVTX=U\Sigma V^T is the SVD of the centred data matrix, then C=1nXXT=UΣ2nUTC=\frac1nXX^T=U\frac{\Sigma^2}{n}U^T. So the principal components are the left singular vectors ui\mathbf u_i, and λi=σi2/n\lambda_i=\sigma_i^2/n. PCA and SVD are two names for one computation.

Solved example: 3. Covariance and PCA on points that sit on a line

Question. The data points are (1,2), (3,4), (5,6)(1,2),\,(3,4),\,(5,6). Find CC, the principal components and the variance explained by the first.

Solution.

  1. Means: xˉ=3\bar x=3, yˉ=4\bar y=4. Centred points: (−2,−2), (0,0), (2,2)(-2,-2),\,(0,0),\,(2,2).
  2. ∑x2=4+0+4=8\sum x^2=4+0+4=8, ∑xy=4+0+4=8\sum xy=4+0+4=8, ∑y2=8\sum y^2=8. With n=3n=3: C=13(8888)=(83838383)C=\dfrac13\begin{pmatrix}8&8\\8&8\end{pmatrix}=\begin{pmatrix}\frac83&\frac83\\\frac83&\frac83\end{pmatrix}.
  3. Trace =163=\frac{16}3, determinant =0=0, so eigenvalues 163\frac{16}3 and 00.
  4. PC1 =12(1,1)=\frac1{\sqrt2}(1,1), the direction of the line. PC2 =12(1,−1)=\frac1{\sqrt2}(1,-1).
  5. Variance explained by PC1 =16/316/3=100%=\frac{16/3}{16/3}=100\%. Reconstruction error with one component =0=0: the points lie exactly on a line, so one number per point loses nothing.

The covariance entry 8/38/3 is a number you will see again and again in past papers.

Solved example: 4. Variance explained, projection and reconstruction

Question. A data set has covariance matrix C=(3113)C=\begin{pmatrix}3&1\\1&3\end{pmatrix}. (a) Find the principal components. (b) What fraction of the variance does PC1 explain? (c) Project the centred point x=(2,4)\mathbf x=(2,4) onto PC1 and reconstruct it. (d) What is the variance along the direction u=(1,0)\mathbf u=(1,0)?

Solution.

  1. Trace 6, determinant 9−1=89-1=8: λ2−6λ+8=0\lambda^2-6\lambda+8=0, so λ=4, 2\lambda=4,\,2. Eigenvectors: (1,1)/2(1,1)/\sqrt2 for 44 and (1,−1)/2(1,-1)/\sqrt2 for 22.
  2. (b) 44+2=23≈66.7%\dfrac{4}{4+2}=\dfrac23\approx66.7\%.
  3. (c) Coordinate on PC1: 12(2+4)=62≈4.24\frac{1}{\sqrt2}(2+4)=\frac6{\sqrt2}\approx4.24. Reconstruction: 4.24⋅12(1,1)=(3,3)4.24\cdot\frac1{\sqrt2}(1,1)=(3,3). Error vector (2,4)−(3,3)=(−1,1)(2,4)-(3,3)=(-1,1), squared length 22 (which equals the squared coordinate on PC2, (2−42)2=2(\frac{2-4}{\sqrt2})^2=2).
  4. (d) Variance along any unit direction u\mathbf u is uTCu\mathbf u^TC\mathbf u. For u=(1,0)\mathbf u=(1,0) that is just C11=3C_{11}=3. For u=12(1,1)\mathbf u=\frac1{\sqrt2}(1,1): 12(3+1+1+3)=4\frac12(3+1+1+3)=4, 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 nn points and dd features there are at most min⁡(n,d)\min(n,d) useful components (centering removes one more: rank of the centred data is at most n−1n-1).
  • PCA is not linear regression: regression measures vertical errors to predict yy; PCA measures perpendicular distances and treats all features alike.

Your turn

Your turn: 1. Singular values

MTM=(9004)M^TM=\begin{pmatrix}9&0\\0&4\end{pmatrix} for some matrix MM. What are the singular values of MM, and what is the sum of the singular values?

Need a hint?

σi=eigenvalue of MTM\sigma_i=\sqrt{\text{eigenvalue of }M^TM}.

Show the full solution

Eigenvalues 9, 49,\,4 give σ=3, 2\sigma=3,\,2; the sum is 55. (The trap option "singular values equal the eigenvalues of MM" is not generally true.)

Your turn: 2. Rank from singular values

AA is 10×810\times8 with singular values 7, 5, 5, 2, 1, 1, 0.5, 0.17,\,5,\,5,\,2,\,1,\,1,\,0.5,\,0.1. What is its rank, and is ATAA^TA invertible?

Need a hint?

Count the non-zero singular values.

Show the full solution

All 8 are non-zero, so rank =8=8 and ATAA^TA (8×88\times8) has rank 8, so it is invertible: least squares has a unique solution.

Your turn: 3. Compression ratio

A 200×300200\times300 image is approximated with its top 1010 singular values. How many numbers are stored, and what fraction of the original?

Need a hint?

Each rank-1 piece needs one σ\sigma, one u\mathbf u (length 200) and one v\mathbf v (length 300).

Show the full solution

10×(200+300+1)=501010\times(200+300+1)=5010 numbers. The original has 60,00060{,}000. So about 8.4%8.4\%.

Your turn: 4. Covariance by hand

Points (1,0), (−1,0), (0,2), (0,−2)(1,0),\,(-1,0),\,(0,2),\,(0,-2) (already centred). Find CC, its eigenvalues, and the variance explained by PC1.

Need a hint?

Sum x2x^2, xyxy, y2y^2 and divide by n=4n=4.

Show the full solution

∑x2=1+1+0+0=2\sum x^2=1+1+0+0=2, ∑xy=0\sum xy=0, ∑y2=0+0+4+4=8\sum y^2=0+0+4+4=8. C=(0.5002)C=\begin{pmatrix}0.5&0\\0&2\end{pmatrix}, already diagonal, so eigenvalues 22 and 0.50.5. PC1 =(0,1)=(0,1) (the yy-axis). Variance explained =22.5=80%=\frac{2}{2.5}=80\%.

Your turn: 5. Projected variance

For C=(5222)C=\begin{pmatrix}5&2\\2&2\end{pmatrix}, what is the variance of the data when projected on the unit vector u=15(2,1)\mathbf u=\frac1{\sqrt5}(2,1)?

Need a hint?

Compute uTCu\mathbf u^TC\mathbf u.

Show the full solution

Cu=15(10+2,  4+2)=15(12,6)C\mathbf u=\frac1{\sqrt5}(10+2,\;4+2)=\frac1{\sqrt5}(12,6). Then uTCu=15(2⋅12+1⋅6)=305=6\mathbf u^TC\mathbf u=\frac15(2\cdot12+1\cdot6)=\frac{30}5=\mathbf 6. (In fact (2,1)(2,1) is an eigenvector with eigenvalue 6: it is PC1.)

Exam radar

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 CC from 3 to 4 points (watch the 1/n1/n), eigenvalues of a 2×22\times2 covariance matrix, "σ\sigma from ATAA^TA", "rank of a 10×810\times8 matrix", and "which statements about PCA are true".

Watch out - real exam trap
  • Divide by nn (or n−1n-1) when forming CC, according to what the question says. In this course papers use 1nXXT\frac1nXX^T.
  • Reconstruction error = the sum of the discarded eigenvalues, not the kept ones.
  • Singular values ≠\ne eigenvalues of AA.
  • Forgetting to centre the data before computing CC.
  • Principal components are eigenvectors of the covariance matrix, not of the raw data matrix.

Quick quiz

Quick quiz

A matrix has singular values 6, 3 and 0. Its rank is...

  • 3
  • 2
  • 1
  • 9
The rank equals the number of non-zero singular values: two.

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
They are the eigenvectors of the covariance matrix, equivalently the left singular vectors of the centred data matrix.

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
5 / (5+3+2) = 0.5.

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.
Centering is needed, and the reconstruction error is the sum of the discarded eigenvalues.

Recap

Remember this
  • SVD: A=UΣVTA=U\Sigma V^T for any matrix. σi=eig(ATA)≥0\sigma_i=\sqrt{\text{eig}(A^TA)}\ge0. Rank = number of non-zero σi\sigma_i.
  • Best rank-kk approximation: keep the top kk terms; storage k(m+n+1)k(m+n+1).
  • PCA: centre →\to covariance C=1nXXTC=\frac1nXX^T →\to eigenvectors = components, eigenvalues = variances.
  • Variance explained =∑keptλ/∑λ=\sum_{\text{kept}}\lambda/\sum\lambda; reconstruction error =∑discardedλ=\sum_{\text{discarded}}\lambda.
  • PCA = SVD of the centred data, λi=σi2/n\lambda_i=\sigma_i^2/n.
Formulas to keep
  • A=UΣVT=∑iσiuiviTA=U\Sigma V^T=\sum_i\sigma_i\mathbf u_i\mathbf v_i^T; σi=λi(ATA)\sigma_i=\sqrt{\lambda_i(A^TA)}; rankA=#{σi≠0}\text{rank}A=\#\{\sigma_i\ne0\}
  • Rank-kk storage: k(m+n+1)k(m+n+1); error ∥A−Ak∥F2=∑i>kσi2\|A-A_k\|_F^2=\sum_{i>k}\sigma_i^2
  • Covariance (centred): C=1nXXTC=\frac1nXX^T; variance along unit u\mathbf u: uTCu\mathbf u^TC\mathbf u
  • Variance explained: ∑i≤kλi∑iλi\frac{\sum_{i\le k}\lambda_i}{\sum_i\lambda_i}; reconstruction error: ∑i>kλi\sum_{i>k}\lambda_i
  • λi(C)=σi2/n\lambda_i(C)=\sigma_i^2/n
Connected weeks (learn them together)

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