Skip to content
AriadneTechnology

The Inner Ring · Chamber 9 of 9

SVD and PCA: The Shape of Data

Every matrix is a rotation, a stretch and a rotation. Compress images and find the principal components of data.

45 min 60 XP + 12 questions + 1 challengeMathVideoPapersProofsCodeLab

In this chamber you will

  • Read A = UΣVᵀ geometrically
  • Approximate a matrix with its top singular values
  • Run PCA from the covariance matrix and from the SVD
  • Read the Eckart–Young theorem and the eigenfaces paper
DiscoverLearnRead beyondPapers & lecturesYour turn

Discover: a few numbers for a face

In 1991, Matthew Turk and Alex Pentland at the MIT Media Lab built a system that could recognise a face in about 0.4 seconds. That was a hard problem. A 256×256256 \times 256 greyscale photo is a list of 65,536 numbers: one point in 65,536-dimensional space (Chamber 1's data as vectors). Comparing faces pixel by pixel is slow, and it's thrown off by small changes in lighting or position. Turk and Pentland described each face with a handful of numbers instead, computed like this:

Spotted in the wild

ωk=ukT(Γ−Ψ)\omega_k = \mathbf{u}_k^{T}(\Gamma - \Psi)
Turk & Pentland (1991), “Eigenfaces for Recognition”, Equation 7

Read it aloud: “omega k equals u k transpose, times gamma minus psi”. Γ\Gamma (capital gamma) is a new face image, unrolled into one long column of pixels. Ψ\Psi (capital psi) is the average face of the training set, so Γ−Ψ\Gamma - \Psi is “how this face differs from the average face”. Each uk\mathbf{u}_k is an eigenface, a ghostly face-shaped pattern, and the weight ωk\omega_k (omega k) says how much of pattern kk this face contains. In many of their tests, trained on M=16M = 16 images, they kept just seven eigenfaces (M′=7M' = 7, “M prime”): seven numbers per face instead of tens of thousands of pixels.

That raises three questions. Where do the patterns uk\mathbf{u}_k come from? Why those patterns and not others? And how can so few numbers carry so much? The answers are the singular value decomposition, which says that every matrix is built from a few strong layers and many weak ones, and principal component analysis, which is the same idea applied to a cloud of data.

Quick check +20 XP

In ωk=ukT(Γ−Ψ)\omega_k = \mathbf{u}_k^{T}(\Gamma - \Psi), the face Γ\Gamma, the average face Ψ\Psi and the eigenface uk\mathbf{u}_k are all columns of N2N^2 pixel values. What kind of object is the weight ωk\omega_k?

DiscoverLearnRead beyondPapers & lecturesYour turn

Learn: rotate, stretch, rotate

In Chamber 4 you watched matrices move the plane. Apply a 2×22 \times 2 matrix to the unit circle and, unless it flattens the plane, you get an ellipse. An ellipse has a longest axis and a shortest axis, at right angles to each other. Here's the surprise: there is always a pair of perpendicular unit vectors v1,v2\mathbf{v}_1, \mathbf{v}_2 that the matrix sends exactly onto those axes. It sends v1\mathbf{v}_1 to the long semi-axis, of length σ1\sigma_1 (“sigma one”) in the direction of a unit vector u1\mathbf{u}_1, and v2\mathbf{v}_2 to the short one, of length σ2\sigma_2 along u2\mathbf{u}_2:

Av1=σ1u1,Av2=σ2u2.A\mathbf{v}_1 = \sigma_1\mathbf{u}_1, \qquad A\mathbf{v}_2 = \sigma_2\mathbf{u}_2.

Put the v\mathbf{v}'s in the columns of a matrix VV, the u\mathbf{u}'s in the columns of UU, and the σ\sigma's on the diagonal of Σ\Sigma (capital sigma). The two equations become one, AV=UΣAV = U\Sigma. Since VV has orthonormal columns, V⊤V=IV^\top V = I and V−1=V⊤V^{-1} = V^\top (Chamber 7), so multiplying on the right by V⊤V^\top gives

A=UΣV⊤,A = U\Sigma V^\top,

read “A equals U sigma V transpose”. This is the singular value decomposition (SVD), and the remarkable fact is that every real matrix has one: square or rectangular, invertible or not.

Read the factorisation from right to left, as three steps applied to a vector x\mathbf{x}:

  1. Rotate. V⊤xV^\top\mathbf{x} lists the dot products vi⊤x\mathbf{v}_i^\top\mathbf{x}: the coordinates of x\mathbf{x} in the basis of v\mathbf{v}'s (Chamber 3). Because VV is orthogonal, this step is a rotation, possibly with a reflection. No length changes.
  2. Stretch. Σ\Sigma multiplies coordinate ii by σi\sigma_i. This is the only step that changes lengths. When m≠nm \ne n it also pads with zeros or drops coordinates, which is how you get from Rn\mathbb{R}^n to Rm\mathbb{R}^m.
  3. Rotate. UU turns the stretched axes to point along u1,u2,…\mathbf{u}_1, \mathbf{u}_2, \ldots in the output space.

So every linear map, however tangled it looks, is a rotation, a stretch along perpendicular axes, and another rotation. A sphere always becomes an ellipsoid (possibly flattened), with semi-axes σ1u1,σ2u2,…\sigma_1\mathbf{u}_1, \sigma_2\mathbf{u}_2, \ldots. In particular, σ1\sigma_1 is the biggest stretch AA applies to any unit vector. (Write a unit vector as x=∑icivi\mathbf{x} = \sum_i c_i\mathbf{v}_i with ∑ici2=1\sum_i c_i^2 = 1. Then ∥Ax∥2=∑iσi2ci2\|A\mathbf{x}\|^2 = \sum_i \sigma_i^2 c_i^2, which is at most σ12\sigma_1^2.)

The notation of the SVD
  • A=UΣV⊤A = U\Sigma V^\top“A equals U sigma V transpose”
    The singular value decomposition: rotate with V⊤V^\top, stretch with Σ\Sigma, rotate with UU. Every real matrix has one.
  • σi\sigma_i“sigma i”
    The ii-th singular value: how much AA stretches along its ii-th direction. Never negative, and sorted from largest to smallest.
    σ1≥σ2≥⋯≥0\sigma_1 \ge \sigma_2 \ge \cdots \ge 0
  • Σ\Sigma“capital sigma, the matrix”
    The m×nm \times n matrix with σ1,σ2,…\sigma_1, \sigma_2, \ldots on its diagonal and zeros everywhere else. In this chamber it is neither a sum nor a covariance.
    Σ=(σ100σ2)\Sigma = \begin{pmatrix} \sigma_1 & 0 \\ 0 & \sigma_2 \end{pmatrix}
  • ui\mathbf{u}_i“u i, a left singular vector”
    Column ii of UU: a unit vector in the output space Rm\mathbb{R}^m, the direction where the ii-th stretch lands.
    Avi=σiuiA\mathbf{v}_i = \sigma_i \mathbf{u}_i
  • vi\mathbf{v}_i“v i, a right singular vector”
    Column ii of VV: a unit vector in the input space Rn\mathbb{R}^n. It is an eigenvector of A⊤AA^\top A with eigenvalue σi2\sigma_i^2.
    A⊤Avi=σi2viA^\top A\mathbf{v}_i = \sigma_i^2 \mathbf{v}_i
  • AkA_k“A sub k”
    The truncated SVD: keep the first kk layers and drop the rest. It has rank kk, and it is the best rank-kk approximation of AA.
    Ak=∑i=1kσiuivi⊤A_k = \sum_{i=1}^{k} \sigma_i \mathbf{u}_i \mathbf{v}_i^\top
  • ∥A∥F\|A\|_F“the Frobenius norm of A”
    The length of AA unrolled into one long vector: the square root of the sum of all squared entries. It equals σ12+⋯+σr2\sqrt{\sigma_1^2 + \cdots + \sigma_r^2}.
    ∥A∥F=∑i,jAij2\|A\|_F = \sqrt{\textstyle\sum_{i,j} A_{ij}^2}
  • ∥A∥2\|A\|_2“the spectral norm of A”
    The biggest stretch AA applies to any unit vector. It equals the largest singular value.
    ∥A∥2=max⁡∥x∥=1∥Ax∥=σ1\|A\|_2 = \max_{\|\mathbf{x}\| = 1} \|A\mathbf{x}\| = \sigma_1

A worked example. Take A=(3045)A = \begin{pmatrix} 3 & 0 \\ 4 & 5 \end{pmatrix}. Its SVD is A=UΣV⊤A = U\Sigma V^\top with

U=110(1−331),Σ=(35005),V⊤=12(11−11).\begin{aligned} U &= \frac{1}{\sqrt{10}}\begin{pmatrix} 1 & -3 \\ 3 & 1 \end{pmatrix}, \qquad \Sigma = \begin{pmatrix} 3\sqrt{5} & 0 \\ 0 & \sqrt{5} \end{pmatrix}, \\[4pt] V^\top &= \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 1 \\ -1 & 1 \end{pmatrix}. \end{aligned}

Multiply the three out and you get AA back.

So v1=12(1,1)\mathbf{v}_1 = \tfrac{1}{\sqrt{2}}(1, 1), v2=12(−1,1)\mathbf{v}_2 = \tfrac{1}{\sqrt{2}}(-1, 1), u1=110(1,3)\mathbf{u}_1 = \tfrac{1}{\sqrt{10}}(1, 3) and u2=110(−3,1)\mathbf{u}_2 = \tfrac{1}{\sqrt{10}}(-3, 1). Check one column by hand: Av1=12(3,9)A\mathbf{v}_1 = \tfrac{1}{\sqrt{2}}(3, 9), and σ1u1=3510(1,3)=32(1,3)\sigma_1\mathbf{u}_1 = \tfrac{3\sqrt{5}}{\sqrt{10}}(1, 3) = \tfrac{3}{\sqrt{2}}(1, 3). The same vector.

This AA turns the unit circle into an ellipse with semi-axes 35≈6.713\sqrt{5} \approx 6.71 and 5≈2.24\sqrt{5} \approx 2.24. The circle's area π\pi becomes πσ1σ2=15π\pi\sigma_1\sigma_2 = 15\pi, and indeed det⁡A=15\det A = 15 (Chamber 5). For any square matrix, ∣det⁡A∣=σ1σ2⋯σn|\det A| = \sigma_1\sigma_2\cdots\sigma_n: the rotations don't change volume, only the stretch does.

Learn: where the SVD comes from

Why should every matrix have such perpendicular directions? The derivation is short, and it rests on the matrix A⊤AA^\top A (“A transpose A”), an n×nn \times n matrix that you met in the normal equations of Chamber 7.

Theorem. Every real m×nm \times n matrix AA has a singular value decomposition.

Proof. We build VV, then Σ\Sigma, then UU.

  1. 1

    A transpose A is symmetric

    (A⊤A)⊤=A⊤(A⊤)⊤=A⊤A(A^\top A)^\top = A^\top (A^\top)^\top = A^\top A. By the spectral theorem (the last section of this chamber), a symmetric matrix has real eigenvalues and an orthonormal basis of eigenvectors. Call the eigenvectors v1,…,vn\mathbf{v}_1, \ldots, \mathbf{v}_n, with eigenvalues ordered λ1≥⋯≥λn\lambda_1 \ge \cdots \ge \lambda_n.

  2. 2

    Its eigenvalues are never negative

    Since vi\mathbf{v}_i is a unit vector,

    λi=λi vi⊤vi=vi⊤A⊤A vi=(Avi)⊤(Avi)=∥Avi∥2≥0.\begin{aligned} \lambda_i = \lambda_i\,\mathbf{v}_i^\top\mathbf{v}_i &= \mathbf{v}_i^\top A^\top A\,\mathbf{v}_i \\ &= (A\mathbf{v}_i)^\top(A\mathbf{v}_i) \\ &= \|A\mathbf{v}_i\|^2 \ge 0. \end{aligned}

    So we can take square roots. Define σi=λi=∥Avi∥\sigma_i = \sqrt{\lambda_i} = \|A\mathbf{v}_i\|.

  3. 3

    Divide to get the u's

    Let rr be the number of non-zero σi\sigma_i. For i≤ri \le r, define ui=Avi/σi\mathbf{u}_i = A\mathbf{v}_i / \sigma_i. These are orthonormal:

    ui⊤uj=vi⊤A⊤A vjσiσj=λj vi⊤vjσiσj=δij,\mathbf{u}_i^\top\mathbf{u}_j = \frac{\mathbf{v}_i^\top A^\top A\,\mathbf{v}_j}{\sigma_i\sigma_j} = \frac{\lambda_j\,\mathbf{v}_i^\top\mathbf{v}_j}{\sigma_i\sigma_j} = \delta_{ij},

    the Kronecker delta (1 if i=ji = j, 0 otherwise), because vi⊤vj\mathbf{v}_i^\top\mathbf{v}_j is 0 when i≠ji \ne j, and when i=ji = j the fraction is λi/σi2=1\lambda_i / \sigma_i^2 = 1.

  4. 4

    Fill in the rest

    For i>ri > r, ∥Avi∥=σi=0\|A\mathbf{v}_i\| = \sigma_i = 0, so Avi=0A\mathbf{v}_i = \mathbf{0}. Extend u1,…,ur\mathbf{u}_1, \ldots, \mathbf{u}_r to an orthonormal basis u1,…,um\mathbf{u}_1, \ldots, \mathbf{u}_m of Rm\mathbb{R}^m (Gram–Schmidt, Chamber 7). Now Avi=σiuiA\mathbf{v}_i = \sigma_i\mathbf{u}_i for every ii up to min⁡(m,n)\min(m, n), and Avi=0A\mathbf{v}_i = \mathbf{0} for any ii beyond mm. Column by column, that is exactly AV=UΣAV = U\Sigma.

  5. 5

    Multiply by V transpose

    VV is orthogonal, so VV⊤=IVV^\top = I, and A=AVV⊤=UΣV⊤A = AVV^\top = U\Sigma V^\top. ■\blacksquare

The proof tells you more than existence:

  • The singular values are σi=λi(A⊤A)\sigma_i = \sqrt{\lambda_i(A^\top A)}, and the vi\mathbf{v}_i are eigenvectors of A⊤AA^\top A. By the same argument with AA⊤=UΣΣ⊤U⊤AA^\top = U\Sigma\Sigma^\top U^\top, the ui\mathbf{u}_i are eigenvectors of AA⊤AA^\top, which has the same non-zero eigenvalues.
  • The rank of AA is rr, the number of non-zero singular values. u1,…,ur\mathbf{u}_1, \ldots, \mathbf{u}_r are an orthonormal basis for the column space of AA, and vr+1,…,vn\mathbf{v}_{r+1}, \ldots, \mathbf{v}_n for its null space (Chamber 6). The SVD hands you orthonormal bases for the subspaces Chamber 6 introduced.

For the worked example, A⊤A=(25202025)A^\top A = \begin{pmatrix} 25 & 20 \\ 20 & 25 \end{pmatrix} has eigenvalues 45 and 5, with eigenvectors along (1,1)(1, 1) and (−1,1)(-1, 1). Their square roots, 45=35\sqrt{45} = 3\sqrt{5} and 5\sqrt{5}, are the singular values we found.

Quick check +20 XP

A 5×35 \times 3 matrix AA has A⊤AA^\top A with eigenvalues 1616, 99 and 00. What are the singular values of AA, and what is its rank?

Learn: a sum of layers, and the best low-rank copy

Chamber 4 gave four views of matrix multiplication. The fourth, columns times rows, turns UΣV⊤U\Sigma V^\top into a sum:

A=σ1u1v1⊤+σ2u2v2⊤+⋯+σrurvr⊤=∑i=1rσiuivi⊤.\begin{aligned} A &= \sigma_1\mathbf{u}_1\mathbf{v}_1^\top + \sigma_2\mathbf{u}_2\mathbf{v}_2^\top + \cdots + \sigma_r\mathbf{u}_r\mathbf{v}_r^\top \\ &= \sum_{i=1}^{r}\sigma_i\mathbf{u}_i\mathbf{v}_i^\top. \end{aligned}

Each uivi⊤\mathbf{u}_i\mathbf{v}_i^\top is an outer product, a rank-one matrix (Chamber 6): every row is a multiple of the same row vi⊤\mathbf{v}_i^\top. Drawn as an image it looks like a plaid, stripes in two directions. The SVD writes any matrix as a sum of plaids, sorted from strongest to weakest. Keep the first kk and drop the rest, and you get the truncated SVD, “A sub k”:

Ak=∑i=1kσiuivi⊤.A_k = \sum_{i=1}^{k}\sigma_i\mathbf{u}_i\mathbf{v}_i^\top.

AkA_k has rank kk. How close is it to AA? To answer, we need the size of a matrix. The Frobenius norm unrolls AA into one long vector and takes its length (Chamber 2):

∥A∥F=∑i=1m∑j=1nAij2,\|A\|_F = \sqrt{\sum_{i=1}^{m}\sum_{j=1}^{n} A_{ij}^2},

read “the Frobenius norm of A”. Then ∥A−B∥F\|A - B\|_F is the distance between two matrices, entry by entry. For images, it's the square root of the total squared pixel error.

Claim. ∥A∥F2=σ12+σ22+⋯+σr2\|A\|_F^2 = \sigma_1^2 + \sigma_2^2 + \cdots + \sigma_r^2.

Proof. First, multiplying by an orthogonal matrix QQ doesn't change the Frobenius norm. For any vector b\mathbf{b}, ∥Qb∥2=b⊤Q⊤Q b=b⊤b=∥b∥2\|Q\mathbf{b}\|^2 = \mathbf{b}^\top Q^\top Q\,\mathbf{b} = \mathbf{b}^\top\mathbf{b} = \|\mathbf{b}\|^2 (Chamber 7). The squared Frobenius norm of a matrix BB is the sum of its squared column lengths, so ∥QB∥F2=∑j∥Qbj∥2=∑j∥bj∥2=∥B∥F2\|QB\|_F^2 = \sum_j \|Q\mathbf{b}_j\|^2 = \sum_j \|\mathbf{b}_j\|^2 = \|B\|_F^2. Multiplying on the right is the same argument applied to rows: transposing only rearranges entries, so ∥BQ∥F=∥Q⊤B⊤∥F=∥B⊤∥F=∥B∥F\|BQ\|_F = \|Q^\top B^\top\|_F = \|B^\top\|_F = \|B\|_F, since Q⊤Q^\top is orthogonal too. Now peel the orthogonal factors off the SVD:

∥A∥F=∥UΣV⊤∥F=∥ΣV⊤∥F=∥Σ∥F=σ12+⋯+σr2,\begin{aligned} \|A\|_F = \|U\Sigma V^\top\|_F &= \|\Sigma V^\top\|_F \\ &= \|\Sigma\|_F = \sqrt{\sigma_1^2 + \cdots + \sigma_r^2}, \end{aligned}

the last step because the only non-zero entries of Σ\Sigma are the σi\sigma_i. ■\blacksquare

The same argument measures the truncation error. A−Ak=∑i>kσiuivi⊤A - A_k = \sum_{i > k}\sigma_i\mathbf{u}_i\mathbf{v}_i^\top is again of the form U(diagonal)V⊤U(\text{diagonal})V^\top, with σ1,…,σk\sigma_1, \ldots, \sigma_k replaced by zeros, so

∥A−Ak∥F=σk+12+⋯+σr2.\|A - A_k\|_F = \sqrt{\sigma_{k+1}^2 + \cdots + \sigma_r^2}.

The error is exactly the size of the singular values you threw away. You can read the quality of every truncation straight off the list of singular values, the spectrum.

We won't prove Eckart–Young here, since the proof takes a page. The readings below include a proof of the spectral-norm version and an intuitive argument for the Frobenius one. But notice what it means for machine learning. Whenever a method approximates a known matrix by a low-rank one, say to compress the weights of a trained layer, the truncated SVD is the benchmark: no rank-kk matrix gets closer in the Frobenius norm. (LoRA, from Chamber 6, is different: it learns a low-rank update instead of approximating a known one.)

Storage. To store AkA_k you keep kk columns of UU (mm numbers each), kk columns of VV (nn each) and kk singular values: k(m+n+1)k(m + n + 1) numbers instead of mnmn. A 1000×10001000 \times 1000 photo at rank 50 needs 50×2001=100,05050 \times 2001 = 100{,}050 numbers, about a tenth of the original million. Try it on two images, each a 64×6464 \times 64 matrix of brightness values between 0 (black) and 1 (white).

Interactive lab

The low-rank artist

The SVD writes an image as a sum of layers, A = σ₁u₁v₁ᵀ + σ₂u₂v₂ᵀ + …, each one a rank-one outer product, strongest first. Keep the first k and throw the rest away. How few layers still look like the picture?
Original A64×64 = 4,096 numbers
Rank 1: A₁129 numbers
Layer 1σ₁u₁v₁ᵀ, σ₁ = 30.13

Error 51.1%: still above 10%. Add more layers.

relative error

51.1%

energy kept

73.8%

numbers stored

129

of the 4,096

3.1%

Singular values σ₁ ≥ σ₂ ≥ … ≥ σ₆₄

0102030116324864

Gold bars are kept. The error is the length of what's thrown away: ‖A − A₁‖F = √(σ₂² + … + σ₆₄²).

Challenge: Low-rank artistFind the smallest rank that rebuilds the image with under 10% error.+50 XP

Things to try:

  1. At rank 1 the picture is a plaid: every row is a multiple of v1⊤\mathbf{v}_1^\top. Step up one layer at a time and watch the third panel, the newest layer σkukvk⊤\sigma_k\mathbf{u}_k\mathbf{v}_k^\top, gold where it adds brightness and blue where it removes it. Every layer is another plaid.
  2. Watch the spectrum while you go. The error readout is computed from the bars you haven't kept, exactly as the formula above says. The “energy kept” is 11 minus the relative error squared.
  3. Switch to the Greek key. At some rank the error drops to exactly zero and the spectrum falls off a cliff. Why? (Hint: how many different rows does the image have, and are any two of them multiples of each other? Think back to Chamber 6.)
  4. The challenge: for either image, find the smallest rank that rebuilds it with under 10% relative error, and leave the slider there.
Quick check +20 XP

A matrix has singular values 4,2,2,14, 2, 2, 1. What is ∥A−A1∥F\|A - A_1\|_F, the error of its best rank-one approximation?

Learn: principal component analysis

Now switch from one matrix to a dataset: nn points x1,…,xn∈Rd\mathbf{x}_1, \ldots, \mathbf{x}_n \in \mathbb{R}^d, stored as the rows of an n×dn \times d data matrix XX. Which directions matter?

In 1901 Karl Pearson asked for the line (and the plane) of closest fit to a cloud of points, measuring each point's distance perpendicular to the line rather than vertically, as regression does. Pythagoras turns this into a question about variance. Suppose the data are centred (their mean is 0\mathbf{0}) and the line runs along a unit vector w\mathbf{w}. Each point splits into a part along the line, of length w⊤xi\mathbf{w}^\top\mathbf{x}_i, and a perpendicular part:

∥xi∥2=(w⊤xi)2+dist⁡(xi,line)2.\|\mathbf{x}_i\|^2 = (\mathbf{w}^\top\mathbf{x}_i)^2 + \operatorname{dist}(\mathbf{x}_i, \text{line})^2.

The left side doesn't depend on w\mathbf{w}. So making the total squared distance small is the same as making ∑i(w⊤xi)2\sum_i (\mathbf{w}^\top\mathbf{x}_i)^2 large: the best-fitting line points in the direction along which the data vary the most. That direction is the first principal component.

  1. 1

    Centre

    Compute the mean xˉ=1n∑ixi\bar{\mathbf{x}} = \frac{1}{n}\sum_i \mathbf{x}_i (“x bar”) and subtract it from every row: Xc=X−1xˉ⊤X_c = X - \mathbf{1}\bar{\mathbf{x}}^\top (“X centred”). The best-fitting line passes through the mean, so this loses nothing.

  2. 2

    Form the covariance matrix

    C=1nXc⊤XcC = \frac{1}{n}X_c^\top X_c, a d×dd \times d matrix. Entry CjkC_{jk} is the covariance of features jj and kk, and the diagonal holds the variances. Statisticians often divide by n−1n - 1 instead of nn: the eigenvectors are the same and the eigenvalues scale.

  3. 3

    Find its eigenvectors

    CC is symmetric, so C=QΛQ⊤C = Q\Lambda Q^\top with orthonormal eigenvectors q1,…,qd\mathbf{q}_1, \ldots, \mathbf{q}_d (the principal directions) and eigenvalues λ1≥⋯≥λd≥0\lambda_1 \ge \cdots \ge \lambda_d \ge 0 (the variances along them).

  4. 4

    Measure the explained variance

    Component ii explains the fraction λi/(λ1+⋯+λd)\lambda_i / (\lambda_1 + \cdots + \lambda_d) of the total variance. Plotting these fractions (a “scree plot”) shows how many components are worth keeping.

  5. 5

    Project

    Keep the top kk directions, Qk=[q1⋯qk]Q_k = [\mathbf{q}_1 \cdots \mathbf{q}_k], and replace each point by its scores zi=Qk⊤(xi−xˉ)∈Rk\mathbf{z}_i = Q_k^\top(\mathbf{x}_i - \bar{\mathbf{x}}) \in \mathbb{R}^k: kk numbers instead of dd.

The notation of PCA
  • xˉ\bar{\mathbf{x}}“x bar, the mean vector”
    The average data point. PCA subtracts it from every point first.
    xˉ=1n∑i=1nxi\bar{\mathbf{x}} = \frac{1}{n}\sum_{i=1}^{n} \mathbf{x}_i
  • XcX_c“X centred”
    The n×dn \times d data matrix (one point per row) with the mean subtracted from every row.
    Xc=X−1xˉ⊤X_c = X - \mathbf{1}\bar{\mathbf{x}}^\top
  • CC“C, the covariance matrix”
    The d×dd \times d matrix of covariances between features, with the variances on its diagonal. Symmetric, and its eigenvalues are never negative.
    C=1nXc⊤XcC = \frac{1}{n} X_c^\top X_c
  • λi\lambda_i“lambda i”
    The ii-th eigenvalue of CC: the variance of the data along the ii-th principal direction.
    λi=σi2/n\lambda_i = \sigma_i^2 / n
  • λi∑jλj\frac{\lambda_i}{\sum_j \lambda_j}“the explained variance ratio”
    The fraction of the total variance that component ii captures.
  • zi\mathbf{z}_i“z i, the scores”
    The low-dimensional code for data point ii: its coordinates along the top kk principal directions.
    zi=Vk⊤(xi−xˉ)\mathbf{z}_i = V_k^\top(\mathbf{x}_i - \bar{\mathbf{x}})
  • S=QΛQ⊤S = Q\Lambda Q^\top“S equals Q lambda Q transpose”
    The spectral theorem: a symmetric matrix has orthonormal eigenvectors (the columns of QQ) and real eigenvalues (the diagonal of Λ\Lambda).

Why the top eigenvector? The variance of the data along a unit vector w\mathbf{w} is

1n∑i=1n(w⊤(xi−xˉ))2=w⊤(1n∑i=1n(xi−xˉ)(xi−xˉ)⊤)w=w⊤C w,\begin{aligned} &\frac{1}{n}\sum_{i=1}^{n}\big(\mathbf{w}^\top(\mathbf{x}_i - \bar{\mathbf{x}})\big)^2 \\ &\quad = \mathbf{w}^\top\Big(\frac{1}{n}\sum_{i=1}^{n}(\mathbf{x}_i - \bar{\mathbf{x}})(\mathbf{x}_i - \bar{\mathbf{x}})^\top\Big)\mathbf{w} \\ &\quad = \mathbf{w}^\top C\,\mathbf{w}, \end{aligned}

using (w⊤a)2=w⊤aa⊤w(\mathbf{w}^\top\mathbf{a})^2 = \mathbf{w}^\top\mathbf{a}\mathbf{a}^\top\mathbf{w}, and the fact that a sum of outer products of the rows is Xc⊤XcX_c^\top X_c (columns times rows again).

Claim. Over all unit vectors w\mathbf{w}, the variance w⊤Cw\mathbf{w}^\top C\mathbf{w} is largest at w=q1\mathbf{w} = \mathbf{q}_1, where it equals λ1\lambda_1.

Proof. Write w\mathbf{w} in the orthonormal eigenbasis: w=a1q1+⋯+adqd\mathbf{w} = a_1\mathbf{q}_1 + \cdots + a_d\mathbf{q}_d, with a12+⋯+ad2=∥w∥2=1a_1^2 + \cdots + a_d^2 = \|\mathbf{w}\|^2 = 1. Then Cw=∑jajλjqjC\mathbf{w} = \sum_j a_j\lambda_j\mathbf{q}_j, and because qi⊤qj=0\mathbf{q}_i^\top\mathbf{q}_j = 0 for i≠ji \ne j, all the cross terms vanish:

w⊤C w=∑j=1dλjaj2≤λ1∑j=1daj2=λ1.\mathbf{w}^\top C\,\mathbf{w} = \sum_{j=1}^{d}\lambda_j a_j^2 \le \lambda_1\sum_{j=1}^{d}a_j^2 = \lambda_1.

The inequality holds because every λj≤λ1\lambda_j \le \lambda_1, and w=q1\mathbf{w} = \mathbf{q}_1 reaches the bound, since q1⊤Cq1=λ1\mathbf{q}_1^\top C\mathbf{q}_1 = \lambda_1. ■\blacksquare

Repeat the argument among directions perpendicular to q1\mathbf{q}_1 and you get q2\mathbf{q}_2, and so on. This is word for word the definition in the eigenfaces paper, whose Equation 1 asks for the uk\mathbf{u}_k for which λk=1M∑n(ukTΦn)2\lambda_k = \frac{1}{M}\sum_n(\mathbf{u}_k^{T}\Phi_n)^2 is a maximum, subject to the u\mathbf{u}'s being orthonormal. It also tells you what PCA throws away. By Pythagoras again, the average squared distance from the points to the best kk-dimensional subspace is λk+1+⋯+λd\lambda_{k+1} + \cdots + \lambda_d: exactly the variance in the directions you dropped.

A worked example. Take four points in R2\mathbb{R}^2: (5,4)(5, 4), (−1,−2)(-1, -2), (3,0)(3, 0) and (1,2)(1, 2).

  1. The mean is xˉ=(2,1)\bar{\mathbf{x}} = (2, 1). The centred points are (3,3)(3, 3), (−3,−3)(-3, -3), (1,−1)(1, -1) and (−1,1)(-1, 1).
  2. The covariance matrix is C=14(9+9+1+19+9−1−19+9−1−19+9+1+1)=(5445)C = \frac{1}{4}\begin{pmatrix} 9 + 9 + 1 + 1 & 9 + 9 - 1 - 1 \\ 9 + 9 - 1 - 1 & 9 + 9 + 1 + 1 \end{pmatrix} = \begin{pmatrix} 5 & 4 \\ 4 & 5 \end{pmatrix}.
  3. det⁡(C−λI)=(5−λ)2−16=0\det(C - \lambda I) = (5 - \lambda)^2 - 16 = 0 gives λ1=9\lambda_1 = 9 and λ2=1\lambda_2 = 1, with eigenvectors q1=12(1,1)\mathbf{q}_1 = \tfrac{1}{\sqrt{2}}(1, 1) and q2=12(1,−1)\mathbf{q}_2 = \tfrac{1}{\sqrt{2}}(1, -1).
  4. The first component explains 9/(9+1)=90%9 / (9 + 1) = 90\% of the variance.
  5. The scores along q1\mathbf{q}_1 are 323\sqrt{2}, −32-3\sqrt{2}, 00 and 00. Their variance is (18+18+0+0)/4=9=λ1(18 + 18 + 0 + 0)/4 = 9 = \lambda_1, as promised. Keeping one component squashes the last two points onto the mean, and the average squared distance lost is (0+0+2+2)/4=1=λ2(0 + 0 + 2 + 2)/4 = 1 = \lambda_2.

PCA through the SVD. Take the SVD of the centred data, Xc=UΣV⊤X_c = U\Sigma V^\top. Then

C=1nXc⊤Xc=1nVΣ⊤U⊤UΣV⊤=V Σ⊤Σn V⊤.\begin{aligned} C = \frac{1}{n}X_c^\top X_c &= \frac{1}{n}V\Sigma^\top U^\top U\Sigma V^\top \\ &= V\,\frac{\Sigma^\top\Sigma}{n}\,V^\top. \end{aligned}

That is an eigendecomposition of CC. The principal directions are the right singular vectors of XcX_c, the variances are λi=σi2/n\lambda_i = \sigma_i^2/n, and the scores come for free: Z=XcVk=UkΣkZ = X_cV_k = U_k\Sigma_k. In the worked example, XcX_c has singular values 6 and 2, and indeed 62/4=96^2/4 = 9 and 22/4=12^2/4 = 1. This is how PCA is computed in practice (scikit-learn's PCA centres the data and takes an SVD), because it's more accurate than forming CC, and because it's cheap when there are far fewer points than features. That was Turk and Pentland's situation: they found eigenvectors vl\mathbf{v}_l of the small M×MM \times M matrix ATAA^{T}A and formed the eigenfaces as ul=Avl\mathbf{u}_l = A\mathbf{v}_l, which is step 3 of our derivation of the SVD without the division by σl\sigma_l.

Quick check +20 XP

A dataset's covariance matrix has eigenvalues 55, 2.52.5, 1.51.5 and 11. What percentage of the total variance do the first two principal components explain?

%

Learn: the spectral theorem

You've now leaned on one fact three times: A⊤AA^\top A and CC are symmetric, so they have real eigenvalues and perpendicular eigenvectors. Time to name it.

Read it “S equals Q lambda Q transpose”. Each qiqi⊤\mathbf{q}_i\mathbf{q}_i^\top projects onto the line through qi\mathbf{q}_i (Chamber 7), so a symmetric matrix simply stretches space by λi\lambda_i along nn perpendicular axes. That's the SVD's picture with the same axes going in and coming out. For a symmetric matrix with no negative eigenvalues, such as A⊤AA^\top A or CC, the eigendecomposition is an SVD: U=V=QU = V = Q and Σ=Λ\Sigma = \Lambda. When some eigenvalues are negative, the singular values are ∣λi∣|\lambda_i| and the signs move into UU.

The proof has three parts.

  1. The eigenvalues are real. The proof needs complex numbers, and it's in the box below.
  2. Eigenvectors for different eigenvalues are perpendicular. You proved this yourself in Chamber 8's Your turn: compute v1⊤Sv2\mathbf{v}_1^\top S\mathbf{v}_2 in two ways and use S⊤=SS^\top = S.
  3. Even when eigenvalues repeat, there are enough perpendicular eigenvectors to fill a basis. This needs an induction argument that we'll skip; Theorem 4.15 of Mathematics for Machine Learning states it, and linear algebra textbooks prove it.
Why a symmetric matrix has real eigenvalues

Suppose Sx=λxS\mathbf{x} = \lambda\mathbf{x} with x≠0\mathbf{x} \ne \mathbf{0}, allowing λ\lambda and x\mathbf{x} to be complex. Write xˉ\bar{\mathbf{x}} for the complex conjugate (flip the sign of every imaginary part; this bar is not a mean). Then xˉ⊤x=∑i∣xi∣2>0\bar{\mathbf{x}}^\top\mathbf{x} = \sum_i |x_i|^2 > 0.

Compute xˉ⊤Sx\bar{\mathbf{x}}^\top S\mathbf{x} in two ways. First, xˉ⊤(Sx)=λ xˉ⊤x\bar{\mathbf{x}}^\top(S\mathbf{x}) = \lambda\,\bar{\mathbf{x}}^\top\mathbf{x}. Second, conjugating Sx=λxS\mathbf{x} = \lambda\mathbf{x} gives Sxˉ=λˉxˉS\bar{\mathbf{x}} = \bar{\lambda}\bar{\mathbf{x}}, because SS is real. Then, using S⊤=SS^\top = S,

xˉ⊤Sx=(S⊤xˉ)⊤x=(Sxˉ)⊤x=λˉ xˉ⊤x.\bar{\mathbf{x}}^\top S\mathbf{x} = (S^\top\bar{\mathbf{x}})^\top\mathbf{x} = (S\bar{\mathbf{x}})^\top\mathbf{x} = \bar{\lambda}\,\bar{\mathbf{x}}^\top\mathbf{x}.

So λ xˉ⊤x=λˉ xˉ⊤x\lambda\,\bar{\mathbf{x}}^\top\mathbf{x} = \bar{\lambda}\,\bar{\mathbf{x}}^\top\mathbf{x}, and dividing by the positive number xˉ⊤x\bar{\mathbf{x}}^\top\mathbf{x} gives λ=λˉ\lambda = \bar{\lambda}: λ\lambda is real. ■\blacksquare

DiscoverLearnRead beyondPapers & lecturesYour turn

Read beyond the course

The SVD sits at a crossroads of numerical computing, statistics and machine learning, so every field has written about it. These are free, and each one shows the ideas from a different side.

Lecture notes · free online · ~40 min

CS168 Lecture #9: The Singular Value Decomposition (SVD) and Low-Rank Matrix Approximations

Tim Roughgarden & Gregory Valiant (Stanford) · Sections 4–6

Clear, example-led notes from Stanford's “Modern Algorithmic Toolbox” course. Section 5 states the Eckart–Young optimality in the Frobenius norm (their Fact 5.1), and Section 6 shows how PCA reduces to the SVD, then uses that to explain why the fact holds. Read it now to see this chamber's ideas from a computer scientist's side.

Lecture notes · free online · ~35 min

CS168 Lecture #7: Understanding and Using Principal Component Analysis (PCA)

Tim Roughgarden & Gregory Valiant (Stanford) · From “A Toy Example” to the end, including “Failure Cases”

PCA as a practitioner uses it: why you centre and scale, a genetics study in which the top two components of 1,387 Europeans' DNA draw a map of Europe, and the cases where PCA fails. The best antidote to thinking PCA always works.

Book · free online · ~90 min

Mathematics for Machine Learning

Marc Peter Deisenroth, A. Aldo Faisal & Cheng Soon Ong · Sections 4.5–4.6 (SVD, matrix approximation) and Chapter 10 (PCA)

The textbook treatment, in notation close to this course's. Section 4.6 proves the Eckart–Young theorem for the spectral norm (Theorem 4.25), and Chapter 10 derives PCA twice, as maximum variance and as best projection, then lists the key steps in practice (Section 10.6).

Article · free online · ~30 min

A Tutorial on Principal Component Analysis

Jonathon Shlens · Sections V–VI

A widely read tutorial whose stated goal is to take the magic out of PCA. Section V solves PCA with an eigendecomposition and Section VI with the SVD, side by side, so you can check your understanding of why they agree.

Book · free online · ~60 min

Data-Driven Science and Engineering

Steven L. Brunton & J. Nathan Kutz · Chapter 1: Sections 1.1–1.2, 1.5–1.6

The authors' free chapters open with the SVD, and the video below follows this chapter. Sections 1.5 and 1.6 run PCA and a full eigenfaces example with code, so read them right after the paper.

DiscoverLearnRead beyondPapers & lecturesYour turn

Papers and lectures

The eigenfaces paper is one of the most readable classics in machine learning, and you now know everything its central sections use. Read Calculating Eigenfaces (Equations 1–6) and then Using Eigenfaces to Classify a Face Image (Equations 7–9). One thing to watch: the paper stores each face Φn\Phi_n as a column, so its matrix A=[Φ1 Φ2⋯ΦM]A = [\Phi_1\ \Phi_2 \cdots \Phi_M] is the transpose of our XcX_c. On a first read, skip the later sections on locating faces in a scene and tracking moving heads.

Eckart and Young's paper is eight pages long and written in the notation of 1936. Its abstract is worth reading for one sentence: the normal equations of least squares (Chamber 7) “cannot be immediately written down”, because the entries of a low-rank matrix aren't independent of one another. They solve it by “first expressing the matrices in a canonic form”, which is the SVD. Pearson's paper is where PCA began: fitting a line or a plane by perpendicular distance, rather than by the vertical errors of regression.

Eigenfaces for RecognitionMatthew Turk, Alex Pentland · Journal of Cognitive Neuroscience 3(1), 71–86, 1991

PCA on photographs of faces: the covariance eigenproblem, the small-matrix trick and recognition by distance in “face space”. A landmark of computer vision.

The approximation of one matrix by another of lower rankCarl Eckart, Gale Young · Psychometrika 1(3), 211–218, 1936

The best rank-kk approximation is the truncated SVD. Written for psychologists, now used everywhere a matrix is compressed.

On lines and planes of closest fit to systems of points in spaceKarl Pearson · The London, Edinburgh, and Dublin Philosophical Magazine and Journal of Science 2(11), 559–572, 1901

The origin of principal component analysis: fitting a line or a plane by perpendicular distance.

Now take apart the equation at the heart of the eigenfaces paper.

Decode the paper · Eq. (3), section “Calculating Eigenfaces”

Eigenfaces for Recognition

Matthew Turk, Alex Pentland · Journal of Cognitive Neuroscience 3(1), 1991

+30 XP
C=1M∑n=1MΦnΦnT=AATC = \frac{1}{M}\sum_{n=1}^{M} \Phi_n \Phi_n^{T} = AA^{T}

Turk and Pentland's eigenfaces are the eigenvectors uk\mathbf{u}_k of this matrix: “the vectors uk\mathbf{u}_k and scalars λk\lambda_k are the eigenvectors and eigenvalues, respectively, of the covariance matrix”. Each face is an N×NN \times N image unrolled into a column of N2N^2 pixels. Match each symbol to its meaning.

CC
MM
Φn\Phi_n
ΦnΦnT\Phi_n \Phi_n^{T}
∑n=1M\sum_{n=1}^{M}
AA

Options

Watch

Singular Value Decomposition (SVD): OverviewSteve Brunton · 7 min
StatQuest: Principal Component Analysis (PCA), Step-by-StepStatQuest with Josh Starmer · 22 min

Brunton's overview follows Chapter 1 of his book, where each data point is a column (as in the eigenfaces paper), so his UU holds the patterns and VV the weights: the transpose of this chamber's rows convention. StatQuest works PCA out slowly on a small example, from finding PC1 to the percentage of variation per component and the scree plot. Watch how he scales things: he works with sums of squared distances, where this chamber divides by nn to get variances. The directions and the percentages come out the same.

DiscoverLearnRead beyondPapers & lecturesYour turn

Your turn

You've already found the smallest rank in the lab above. Now match the symbols, assemble one proof and write another, and then compress, decompose and recognise in code.

Match · Symbols ↔ Meaning

What each piece means

+25 XP

Match each piece of the SVD to what it tells you about the matrix AA.

σ1\sigma_1
v1\mathbf{v}_1
u1\mathbf{u}_1
AkA_k
∥A∥F\|A\|_F
σi2\sigma_i^2
V⊤xV^\top\mathbf{x}

Options

Match · Maths ↔ NumPy

From symbols to NumPy

+25 XP

Match each formula to the NumPy that computes it. Two details: np.linalg.svd returns the singular values as a 1D array S (not a matrix), and it returns V⊤V^\top (called Vt), not VV. The data matrix X has one point per row.

A=UΣV⊤A = U\Sigma V^\top
Ak=∑i=1kσiuivi⊤A_k = \sum_{i=1}^{k}\sigma_i\mathbf{u}_i\mathbf{v}_i^\top
∥A∥F\|A\|_F
Xc=X−1xˉ⊤X_c = X - \mathbf{1}\bar{\mathbf{x}}^\top
C=1nXc⊤XcC = \frac{1}{n}X_c^\top X_c
λi/∑jλj\lambda_i / \sum_j \lambda_j
Z=XcVkZ = X_c V_k

Options

Proofs

The puzzle runs the SVD backwards: start from A=UΣV⊤A = U\Sigma V^\top and show the v\mathbf{v}'s are eigenvectors of A⊤AA^\top A. The second shows why PCA's explained-variance fractions add up to exactly 1.

Proof puzzle

Right singular vectors are eigenvectors

+25 XP

Claim

If A=UΣV⊤A = U\Sigma V^\top is an SVD (UU and VV orthogonal, Σ\Sigma diagonal with entries σi\sigma_i), then every right singular vector vi\mathbf{v}_i is an eigenvector of A⊤AA^\top A, with eigenvalue σi2\sigma_i^2.

Tap lines in the order they should appear. Not every line belongs. Tap a line in your proof to send it back.

Your proof

  1. Pick the first line below.

Available lines

Prove it yourself

The eigenvalues share out the total variance

+40 XP

Claim

Let XcX_c be a centred n×dn \times d data matrix whose rows are xi−xˉ\mathbf{x}_i - \bar{\mathbf{x}}, and let λ1,…,λd\lambda_1, \ldots, \lambda_d be the eigenvalues of its covariance matrix C=1nXc⊤XcC = \frac{1}{n}X_c^\top X_c. Prove that

λ1+λ2+⋯+λd=1n∑i=1n∥xi−xˉ∥2.\lambda_1 + \lambda_2 + \cdots + \lambda_d = \frac{1}{n}\sum_{i=1}^{n} \|\mathbf{x}_i - \bar{\mathbf{x}}\|^2.

The right-hand side is the total variance: the average squared distance of the points from their mean. (This is why λi/∑jλj\lambda_i / \sum_j \lambda_j is the fraction of the variance that component ii explains.) You may use two results from this chamber: λi=σi2/n\lambda_i = \sigma_i^2 / n, where the σi\sigma_i are the singular values of XcX_c, and ∥A∥F2=σ12+⋯+σr2\|A\|_F^2 = \sigma_1^2 + \cdots + \sigma_r^2.

Preview

Your typeset proof appears here.

Code it up

Each problem has one answer. The first two use np.linalg.svd; the third applies the eigenfaces paper to a tiny dataset of 8×88 \times 8 “faces”.

Problem 25·Warm-up

One layer of a staircase

+20 XP

Let AA be the 8×88 \times 8 matrix with entries Aij=min⁡(i,j)A_{ij} = \min(i, j) for i,j=1,…,8i, j = 1, \ldots, 8. Its first row is 1,1,…,11, 1, \ldots, 1 and its last row is 1,2,…,81, 2, \ldots, 8.

The best rank-one approximation of AA is its first SVD layer, A1=σ1u1v1⊤A_1 = \sigma_1\mathbf{u}_1\mathbf{v}_1^\top. What fraction of the squared Frobenius norm does that single layer keep? That is, compute

σ12∥A∥F2,\frac{\sigma_1^2}{\|A\|_F^2},

and give it to 4 decimal places.

A number, rounded to 4 decimal places

Problem 26·Standard

Compressing a disc

+35 XP

A 40×4040 \times 40 black-and-white image shows a white disc of radius 15 in the middle of a black square:

Aij={1if (i−20.5)2+(j−20.5)2≤225,0otherwise,i,j=1,…,40.A_{ij} = \begin{cases} 1 & \text{if } (i - 20.5)^2 + (j - 20.5)^2 \le 225, \\ 0 & \text{otherwise,} \end{cases} \qquad i, j = 1, \ldots, 40.

Let AkA_k be its rank-kk truncated SVD. Find the smallest kk for which the relative error is strictly below 5%:

∥A−Ak∥F∥A∥F<0.05.\frac{\|A - A_k\|_F}{\|A\|_F} < 0.05.

Give kk as an integer.

An exact integer (or a fraction like 7/12)

Problem 27·Challenge

Distance from face space

+50 XP

Apply the eigenfaces paper to a tiny dataset. Turk and Pentland project a face onto a few eigenfaces and measure what's left over: a face that is far from “face space” may not be a face at all.

For n=1,…,12n = 1, \ldots, 12, training face Γn\Gamma_n is an 8×88 \times 8 image with pixel values

Γn(r,c)=5+((n mod 4)−1.5)cos⁡πr7+((3n mod 5)−2)cos⁡πc7+12((5n mod 7)−3)cos⁡π(r+c)7+0.3sin⁡(n(r+2c))\Gamma_n(r, c) = 5 + \big((n \bmod 4) - 1.5\big)\cos\tfrac{\pi r}{7} + \big((3n \bmod 5) - 2\big)\cos\tfrac{\pi c}{7} + \tfrac{1}{2}\big((5n \bmod 7) - 3\big)\cos\tfrac{\pi (r + c)}{7} + 0.3\sin\big(n(r + 2c)\big)

for r,c=0,1,…,7r, c = 0, 1, \ldots, 7 (angles in radians). Flatten each image row by row into a vector of length 64: pixel (r,c)(r, c) goes to position 8r+c8r + c.

1. Compute the average face Ψ=112∑n=112Γn\Psi = \frac{1}{12}\sum_{n=1}^{12}\Gamma_n and the differences Φn=Γn−Ψ\Phi_n = \Gamma_n - \Psi. 2. Find M′=3M' = 3 eigenfaces u1,u2,u3\mathbf{u}_1, \mathbf{u}_2, \mathbf{u}_3: unit eigenvectors of the covariance matrix C=112∑nΦnΦnTC = \frac{1}{12}\sum_{n}\Phi_n\Phi_n^{T} (the paper's Eq. 3) with the three largest eigenvalues. 3. A new image has pixels Γ(r,c)=5+1.2cos⁡πr7−0.8cos⁡πc7+0.5cos⁡πrc49\Gamma(r, c) = 5 + 1.2\cos\frac{\pi r}{7} - 0.8\cos\frac{\pi c}{7} + 0.5\cos\frac{\pi r c}{49}. Compute its weights ωk=ukT(Γ−Ψ)\omega_k = \mathbf{u}_k^{T}(\Gamma - \Psi) (Eq. 7), its projection onto face space Φf=∑k=13ωkuk\Phi_f = \sum_{k=1}^{3}\omega_k\mathbf{u}_k, and its squared distance from face space (Eq. 9)

ϵ2=∥Φ−Φf∥2,Φ=Γ−Ψ.\epsilon^2 = \|\Phi - \Phi_f\|^2, \qquad \Phi = \Gamma - \Psi.

Give ϵ2\epsilon^2 to 4 decimal places.

A number, rounded to 4 decimal places

Key takeaways

  • Every matrix is rotate, stretch, rotate: A=UΣV⊤A = U\Sigma V^\top, with Avi=σiuiA\mathbf{v}_i = \sigma_i\mathbf{u}_i. The singular values are the square roots of the eigenvalues of A⊤AA^\top A, and the rank is the number of non-zero ones.
  • The SVD is a sum of rank-one layers, strongest first. Keeping kk of them gives AkA_k, the best rank-kk approximation (Eckart–Young), with error σk+12+⋯+σr2\sqrt{\sigma_{k+1}^2 + \cdots + \sigma_r^2} in the Frobenius norm.
  • Storing AkA_k costs k(m+n+1)k(m + n + 1) numbers instead of mnmn. That's the whole idea behind compressing with low rank.
  • PCA: centre the data, find the eigenvectors of the covariance matrix C=1nXc⊤XcC = \frac{1}{n}X_c^\top X_c, keep the top few. They're the directions of greatest variance, and λi/∑jλj\lambda_i / \sum_j\lambda_j is the variance each explains.
  • PCA is the SVD of the centred data: the principal directions are the right singular vectors of XcX_c, and λi=σi2/n\lambda_i = \sigma_i^2/n.
  • The spectral theorem: a symmetric matrix has real eigenvalues and orthonormal eigenvectors, S=QΛQ⊤S = Q\Lambda Q^\top. It's why the SVD and PCA always exist.

Checkpoint

Prove it to the labyrinth

Answer every question to clear this chamber. First-try answers earn the most XP.

0/8
Question 1 of 8 +20 XP

A dataset of 100 examples with 20 features is stored as a 100×20100 \times 20 matrix AA. In its full SVD A=UΣV⊤A = U\Sigma V^\top, what are the shapes?

Question 2 of 8 +20 XP

A 2×22 \times 2 matrix AA turns the unit circle into an ellipse with semi-axes of length 55 and 0.40.4. What is ∣det⁡A∣|\det A|?

Question 3 of 8 +20 XP

A 6×46 \times 4 matrix A=UΣV⊤A = U\Sigma V^\top has singular values 7.27.2, 3.13.1, 00 and 00. Which statement is true?

Question 4 of 8 +20 XP

A greyscale photo is a 1000×8001000 \times 800 matrix. How many numbers do you need to store its rank-50 truncation A50A_{50} (the kept columns of UU and VV, plus the singular values)?

Question 5 of 8 +20 XP

What does the Eckart–Young theorem say about the truncated SVD AkA_k?

Question 6 of 8 +20 XP

Why does PCA subtract the mean from the data before finding directions?

Question 7 of 8 +20 XP

A centred data matrix XcX_c has n=50n = 50 rows, and its largest singular value is σ1=20\sigma_1 = 20. With C=1nXc⊤XcC = \frac{1}{n}X_c^\top X_c, what is the largest eigenvalue λ1\lambda_1 of CC?

Question 8 of 8 +20 XP

Which matrix is guaranteed by the spectral theorem to have an orthonormal basis of eigenvectors?

End of the chamber

Clear this chamber

+60 XPSingular Value DecompositionLow-Rank ApproximationPrincipal Component AnalysisThe Spectral Theorem