Skip to content
BIASED

The singular value decomposition

The factorization A = UΣVᵀ for every matrix: building it from AᵀA, what singular values measure, rank and the four subspaces, the best low-rank approximation and LoRA, PCA via the SVD, the pseudoinverse and least squares.

When eigenvectors are not enough

The topic Symmetric matrices and quadratic forms gave symmetric matrices a perfect description: an orthonormal basis of eigenvectors along which the matrix only stretches. Real data matrices are neither square nor symmetric. A dataset of NN examples with dd features is a matrix X∈RN×dX \in \mathbb R^{N \times d}, and a linear layer maps Rdin\mathbb R^{d_{\text{in}}} to Rdout\mathbb R^{d_{\text{out}}}. For a rectangular matrix the equation Av=λvAv = \lambda v does not even make sense: AvAv and vv live in different spaces.

Even a square matrix can let us down. Take the running example of this topic:

A=(22−21)A = \begin{pmatrix} 2 & 2 \\ -2 & 1 \end{pmatrix}

Its trace is 33 and its determinant is 66, so its eigenvalues are 1.5±1.936i1.5 \pm 1.936i. No real direction is merely stretched. Yet the geometry is simple: AA sends the unit circle to an ellipse, and an ellipse has two perpendicular axes.

The fix is to allow two orthonormal bases, one for the inputs and one for the outputs. Every matrix A∈Rm×nA \in \mathbb R^{m \times n} of rank rr factors as

A=UΣV⊤=∑i=1rσi uivi⊤.A = U \Sigma V^\top = \sum_{i=1}^{r} \sigma_i\, u_i v_i^\top.

Here U∈Rm×mU \in \mathbb R^{m \times m} and V∈Rn×nV \in \mathbb R^{n \times n} are orthogonal. Their columns uiu_i and viv_i are the left and right singular vectors. The matrix Σ∈Rm×n\Sigma \in \mathbb R^{m \times n} has the same shape as AA and is zero except for its diagonal. The diagonal starts with the singular values σ1≥σ2≥⋯≥σr>0\sigma_1 \ge \sigma_2 \ge \dots \ge \sigma_r > 0 and continues with zeros. This is the singular value decomposition, or SVD. Unlike an eigendecomposition, it exists for every real matrix of any shape.

The sum on the right reads as a recipe. Each term takes the component of the input along viv_i, scales it by σi\sigma_i and writes the result along uiu_i. So AA is a sum of rr rank-one pieces, sorted from the most important to the least.

For a tall data matrix most columns of UU meet only the zero rows of Σ\Sigma. Libraries can skip them: np.linalg.svd(X, full_matrices=False) returns just the first min⁡(m,n)\min(m, n) columns.

Building the SVD from a symmetric matrix

Where do VV and the σi\sigma_i come from? From a symmetric matrix we already understand. The n×nn \times n matrix A⊤AA^\top A is symmetric, and it is positive semidefinite, because

x⊤A⊤A x=∥Ax∥2≥0.x^\top A^\top A\, x = \lVert Ax \rVert^2 \ge 0.

By the spectral theorem it has an orthonormal basis of eigenvectors v1,…,vnv_1, \dots, v_n with eigenvalues λ1≥⋯≥λn≥0\lambda_1 \ge \dots \ge \lambda_n \ge 0. Exactly rr of them are positive. Indeed, A⊤Ax=0A^\top A x = 0 forces ∥Ax∥2=x⊤A⊤Ax=0\lVert Ax \rVert^2 = x^\top A^\top A x = 0, so A⊤AA^\top A has the same null space as AA. By rank–nullity that null space has dimension n−rn - r, so exactly n−rn - r eigenvalues are zero. Now set

σi=λi,ui=Aviσi(i≤r).\sigma_i = \sqrt{\lambda_i}, \qquad u_i = \frac{A v_i}{\sigma_i} \quad (i \le r).

The uiu_i are orthonormal for free: ui⊤uj=vi⊤A⊤Avj/(σiσj)=λjvi⊤vj/(σiσj)u_i^\top u_j = v_i^\top A^\top A v_j / (\sigma_i \sigma_j) = \lambda_j v_i^\top v_j / (\sigma_i \sigma_j), which is 11 for i=ji = j and 00 otherwise. For i>ri > r we get ∥Avi∥2=λi=0\lVert A v_i \rVert^2 = \lambda_i = 0, so Avi=0A v_i = 0. Complete u1,…,uru_1, \dots, u_r to an orthonormal basis of Rm\mathbb R^m. Then AV=UΣAV = U\Sigma column by column, and multiplying by V⊤V^\top on the right gives A=UΣV⊤A = U \Sigma V^\top.

Back to the running example. Here

A⊤A=(8225),A^\top A = \begin{pmatrix} 8 & 2 \\ 2 & 5 \end{pmatrix},

with eigenvalues 99 and 44 and eigenvectors v1=(2,1)/5v_1 = (2, 1)/\sqrt5, v2=(−1,2)/5v_2 = (-1, 2)/\sqrt5. So σ1=3\sigma_1 = 3 and σ2=2\sigma_2 = 2. Then u1=Av1/3=(2,−1)/5u_1 = A v_1 / 3 = (2, -1)/\sqrt5 and u2=Av2/2=(1,2)/5u_2 = A v_2 / 2 = (1, 2)/\sqrt5. A quick multiplication confirms 3 u1v1⊤+2 u2v2⊤=A3\, u_1 v_1^\top + 2\, u_2 v_2^\top = A.

Note what σi\sigma_i is: the length ∥Avi∥\lVert A v_i \rVert of the image of the unit vector viv_i. Singular values are square roots of eigenvalues of A⊤AA^\top A, not eigenvalues of AA itself, and they are never negative.

Rotate, stretch, rotate

Read Ax=U(Σ(V⊤x))Ax = U\big(\Sigma (V^\top x)\big) from right to left. V⊤V^\top rotates (possibly with a reflection) so that each viv_i lands on the axis eie_i. Σ\Sigma stretches axis ii by σi\sigma_i and adds or drops dimensions. UU rotates eie_i to uiu_i. Rotate, stretch, rotate.

So the unit circle goes to an ellipse with semi-axes σ1u1\sigma_1 u_1 and σ2u2\sigma_2 u_2. In the running example the input direction v1v_1 becomes an axis of length 33, and the perpendicular v2v_2 becomes an axis of length 22. Both rotations turn by about −26.6∘-26.6^\circ.

The singular values answer the natural questions about the size of a matrix.

The spectral norm is the largest stretch of a unit vector, and it equals σ1\sigma_1:

∥A∥2=max⁡∥x∥=1∥Ax∥=σ1.\lVert A \rVert_2 = \max_{\lVert x \rVert = 1} \lVert Ax \rVert = \sigma_1.

Indeed, ∥Ax∥2=x⊤A⊤A x\lVert Ax \rVert^2 = x^\top A^\top A\, x is the Rayleigh quotient of A⊤AA^\top A, and its maximum is λ1=σ12\lambda_1 = \sigma_1^2. For a square or tall AA the smallest stretch is likewise σn\sigma_n.

The Frobenius norm treats the matrix as one long vector of its entries:

∥A∥F2=∑i,jaij2=tr⁡(A⊤A)=∑iσi2.\lVert A \rVert_F^2 = \sum_{i,j} a_{ij}^2 = \operatorname{tr}(A^\top A) = \sum_i \sigma_i^2.

The middle step holds because the jj-th diagonal entry of A⊤AA^\top A is ∑iaij2\sum_i a_{ij}^2. The last one holds because a trace is the sum of the eigenvalues, here λi=σi2\lambda_i = \sigma_i^2. In the example, 4+4+4+1=13=9+44 + 4 + 4 + 1 = 13 = 9 + 4.

For a square AA the orthogonal factors have determinant ±1\pm 1, so ∣det⁡A∣=∏iσi\lvert \det A \rvert = \prod_i \sigma_i. In the example, 3⋅2=63 \cdot 2 = 6.

The ratio σ1/σn\sigma_1 / \sigma_n says how strongly the map flattens the circle. It is the condition number κ(A)\kappa(A), and the topic Conditioning and numerical stability will show why it governs rounding errors.

Rank and the four subspaces

The rank of AA is the number of nonzero singular values. More than that, the SVD hands us orthonormal bases of all four subspaces from the topic Matrices and linear systems:

Subspace Dimension Basis
column space of AA rr u1,…,uru_1, \dots, u_r
null space of A⊤A^\top m−rm - r ur+1,…,umu_{r+1}, \dots, u_m
row space of AA rr v1,…,vrv_1, \dots, v_r
null space of AA n−rn - r vr+1,…,vnv_{r+1}, \dots, v_n

Why: Ax=∑i≤rσi(vi⊤x) uiAx = \sum_{i \le r} \sigma_i (v_i^\top x)\, u_i is always a combination of u1,…,uru_1, \dots, u_r, and x=vi/σix = v_i / \sigma_i reaches each uiu_i. The same sum vanishes exactly when xx is orthogonal to v1,…,vrv_1, \dots, v_r, that is, when xx lies in the span of vr+1,…,vnv_{r+1}, \dots, v_n. The two subspaces of A⊤A^\top follow in the same way from A⊤=VΣ⊤U⊤A^\top = V \Sigma^\top U^\top.

Rank–nullity is visible at a glance: the nn right singular vectors split into rr for the row space and n−rn - r for the null space.

Take a wide matrix:

B=(101011).B = \begin{pmatrix} 1 & 0 & 1 \\ 0 & 1 & 1 \end{pmatrix}.

For a wide matrix it is cheaper to diagonalize BB⊤=(2112)BB^\top = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}, which has the same nonzero eigenvalues as B⊤BB^\top B. They are 33 and 11, so σ1=3\sigma_1 = \sqrt3, σ2=1\sigma_2 = 1 and the rank is 22. The third right singular vector is v3=(1,1,−1)/3v_3 = (1, 1, -1)/\sqrt3, and indeed Bv3=0Bv_3 = 0: the null space is the line through (1,1,−1)(1, 1, -1).

In floating point an exact zero rarely survives. A singular value that should be zero comes out as 10−1510^{-15} or so. NumPy's matrix_rank therefore counts only the singular values above a tolerance:

σi>σ1⋅max⁡(m,n)⋅εmach,εmach≈2.2⋅10−16.\sigma_i > \sigma_1 \cdot \max(m, n) \cdot \varepsilon_{\text{mach}}, \qquad \varepsilon_{\text{mach}} \approx 2.2 \cdot 10^{-16}.

The result is the numerical rank. The SVD is the reliable way to compute it, because σk+1\sigma_{k+1} is exactly the spectral-norm distance from AA to the nearest matrix of rank kk, as the next block shows. The pivots of elimination give no such guarantee. The 30×3030 \times 30 upper triangular matrix with ones on the diagonal and −1-1 everywhere above it has every pivot equal to 11, yet its σ30≈2.8⋅10−9\sigma_{30} \approx 2.8 \cdot 10^{-9}: a change that small makes it singular.

An ML example: a feature table lists a price in rubles and the same price in dollars. One singular value of the data matrix is then zero up to rounding, far below the tolerance. Its right singular vector mixes the two price columns, and moving the weights along it does not change any prediction.

The best low-rank approximation

Cutting the sum after kk terms gives the truncated SVD

Ak=∑i=1kσi uivi⊤,A_k = \sum_{i=1}^{k} \sigma_i\, u_i v_i^\top,

a matrix of rank kk. The Eckart–Young theorem says no matrix of rank at most kk does better, in either norm:

min⁡rank⁡B≤k∥A−B∥2=σk+1,min⁡rank⁡B≤k∥A−B∥F=σk+12+⋯+σr2.\min_{\operatorname{rank} B \le k} \lVert A - B \rVert_2 = \sigma_{k+1}, \qquad \min_{\operatorname{rank} B \le k} \lVert A - B \rVert_F = \sqrt{\sigma_{k+1}^2 + \dots + \sigma_r^2}.

Both minima are reached at B=AkB = A_k. The error values are easy to see: A−Ak=∑i>kσiuivi⊤A - A_k = \sum_{i > k} \sigma_i u_i v_i^\top is itself an SVD with singular values σk+1,…,σr\sigma_{k+1}, \dots, \sigma_r. That nothing else beats AkA_k is the deep part of the theorem.

In the running example A1=3 u1v1⊤=(2.41.2−1.2−0.6)A_1 = 3\, u_1 v_1^\top = \begin{pmatrix} 2.4 & 1.2 \\ -1.2 & -0.6 \end{pmatrix}. The error has spectral and Frobenius norm 22, which is 2/13≈55%2/\sqrt{13} \approx 55\% of ∥A∥F\lVert A \rVert_F. With σ2\sigma_2 close to σ1\sigma_1, this matrix is far from rank one.

Real data are usually kinder. In a 64×6464 \times 64 grayscale image σ1\sigma_1 alone carries 92%92\% of ∥A∥F2\lVert A \rVert_F^2, and rank 1010 reproduces the image with an 8%8\% relative error. And AkA_k never has to be stored as an m×nm \times n table: keeping the vectors uiu_i, viv_i and the numbers σi\sigma_i is enough.

The same idea drives LoRA, a standard way to fine-tune large models. Instead of a full update of a weight matrix W∈Rdout×dinW \in \mathbb R^{d_{\text{out}} \times d_{\text{in}}}, it learns a rank-rr update

ΔW=BC,B∈Rdout×r,  C∈Rr×din.\Delta W = BC, \qquad B \in \mathbb R^{d_{\text{out}} \times r},\; C \in \mathbb R^{r \times d_{\text{in}}}.

For a 4096×40964096 \times 4096 projection with r=8r = 8 this is 65,53665{,}536 parameters instead of 16,777,21616{,}777{,}216, or 0.39%0.39\%. Eckart–Young prices the restriction: if the ideal update has singular values σi\sigma_i, any rank-rr update misses it by at least ∑i>rσi2\sqrt{\sum_{i > r} \sigma_i^2} in Frobenius norm.

PCA is the SVD of centred data

The topic Symmetric matrices and quadratic forms found the direction of largest variance as the top eigenvector of the covariance matrix. The SVD gets there without forming the covariance at all.

Let X∈RN×dX \in \mathbb R^{N \times d} be centred: every column has mean zero. Take its SVD, X=UΣV⊤X = U \Sigma V^\top. The covariance matrix becomes

S=1NX⊤X=V Σ⊤ΣN V⊤.S = \frac1N X^\top X = V\, \frac{\Sigma^\top \Sigma}{N}\, V^\top.

This is already a spectral decomposition of SS. So:

  • the principal directions, or principal components, are the right singular vectors vjv_j;
  • the variance of the data along vjv_j is σj2/N\sigma_j^2 / N;
  • the coordinates of the examples in the new basis are the columns of XVXV;
  • the best reconstruction of XX from kk components is the truncation XkX_k, by Eckart–Young.

The total variance is tr⁡S\operatorname{tr} S, the sum of the variances along all the directions vjv_j.

Check it on the four centred points of that topic, (−3,−2)(-3, -2), (−1,0)(-1, 0), (1,2)(1, 2) and (3,0)(3, 0). Their matrix XX has σ1=24\sigma_1 = \sqrt{24} and σ2=2\sigma_2 = 2, so the variances are 24/4=624/4 = 6 and 4/4=14/4 = 1: exactly the eigenvalues of SS found there. And v1=(2,1)/5v_1 = (2, 1)/\sqrt5 is the same top eigenvector.

Why not simply diagonalize X⊤XX^\top X? Because forming it squares the singular values. Rounding the entries of X⊤XX^\top X moves its eigenvalues σj2\sigma_j^2 by about εmachσ12\varepsilon_{\text{mach}} \sigma_1^2. So a component with σj\sigma_j below about εmach σ1≈10−8σ1\sqrt{\varepsilon_{\text{mach}}}\, \sigma_1 \approx 10^{-8} \sigma_1 drowns in rounding errors, while the SVD of XX itself still resolves it. For singular values 11, 10−410^{-4} and 10−1010^{-10}, the SVD returns 10−1010^{-10}; the route through X⊤XX^\top X returns rounding noise of order 10−810^{-8}. This is why PCA in scikit-learn computes the SVD of XX directly.

The pseudoinverse and least squares

The topic Inner products, norms and orthogonality solved least squares with the normal equations, assuming independent columns. Often they are not independent: a duplicated feature, or more weights than examples. Then infinitely many xx minimize ∥Ax−b∥\lVert Ax - b \rVert. The SVD picks the shortest one. Define the pseudoinverse

A+=VΣ+U⊤=∑i=1r1σi viui⊤,x+=A+b,A^+ = V \Sigma^+ U^\top = \sum_{i=1}^{r} \frac{1}{\sigma_i}\, v_i u_i^\top, \qquad x^+ = A^+ b,

where Σ+\Sigma^+ is Σ\Sigma transposed, with each nonzero σi\sigma_i replaced by 1/σi1/\sigma_i and zeros left as zeros.

Why it works: change coordinates to c=V⊤xc = V^\top x and d=U⊤bd = U^\top b. Both changes are rotations, so lengths stay the same, and

∥Ax−b∥2=∑i≤r(σici−di)2+∑i>rdi2.\lVert Ax - b \rVert^2 = \sum_{i \le r} (\sigma_i c_i - d_i)^2 + \sum_{i > r} d_i^2.

The first sum vanishes at ci=di/σic_i = d_i / \sigma_i. The cic_i with i>ri > r do not affect the residual at all, so setting them to zero gives the shortest solution. For the wide matrix BB and b=(1,1)b = (1, 1) every solution is (1−t,1−t,t)(1 - t, 1 - t, t), and B+b=(1,1,2)/3B^+ b = (1, 1, 2)/3 is the shortest of them.

Two special cases connect to earlier topics. For a square invertible AA, A+=A−1A^+ = A^{-1}. For independent columns, A+=(A⊤A)−1A⊤A^+ = (A^\top A)^{-1} A^\top, the normal equations.

Small singular values are dangerous: 1/σi1/\sigma_i amplifies the noise in bb. Libraries cut off tiny σi\sigma_i (the rcond argument in NumPy), and ridge regression softens 1/σi1/\sigma_i into σi/(σi2+λ)\sigma_i / (\sigma_i^2 + \lambda).

One more fact links this to training. Gradient descent started from zero on an underdetermined least-squares problem converges to exactly A+bA^+ b: it never leaves the row space. To see why, and to train models that are not linear at all, we need derivatives. The next topic, Derivatives and Taylor series, starts the calculus part of the course.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • SVD rank-1 reconstructionoptional

    Build σ1 u1 v1ᵀ; compare Frobenius error to the full matrix.

    ~20 min · easier on a computer · The matching course topic has been read.

    Self-check in the notebook — not part of mastery or the certificate.

Full notes

Answers are saved on the server and tied to this browser until you sign in.