TExcelTAD
Home › Math for Machine Learning › Singular Value Decomposition

Singular Value Decomposition

Eigendecomposition needs a square matrix, and often a symmetric one. Data matrices are rectangular. The singular value decomposition works for every matrix: A=UΣVTA = U\Sigma V^T, a rotation, an axis-aligned stretch and another rotation. It is the most useful factorization in data science, behind PCA, least squares, low-rank compression and recommender systems.

The statement

Every A∈Rm×nA \in \mathbb{R}^{m \times n} of rank rr factors as

A=UΣVT=∑i=1rσi uiviTA = U\Sigma V^T = \sum_{i=1}^{r} \sigma_i\,\mathbf{u}_i\mathbf{v}_i^T

with U∈Rm×mU \in \mathbb{R}^{m \times m} and V∈Rn×nV \in \mathbb{R}^{n \times n} orthogonal, and Σ∈Rm×n\Sigma \in \mathbb{R}^{m \times n} diagonal with singular values σ1≥σ2≥⋯≥σr>0\sigma_1 \ge \sigma_2 \ge \cdots \ge \sigma_r > 0 (the rest are 0). The columns vi\mathbf{v}_i are the right singular vectors, the ui\mathbf{u}_i the left singular vectors, and Avi=σiuiA\mathbf{v}_i = \sigma_i\mathbf{u}_i.

The link to eigendecomposition

ATA=V(ΣTΣ)VT,AAT=U(ΣΣT)UTA^TA = V(\Sigma^T\Sigma)V^T, \qquad AA^T = U(\Sigma\Sigma^T)U^T

So the vi\mathbf{v}_i are eigenvectors of ATAA^TA, the ui\mathbf{u}_i eigenvectors of AATAA^T, and σi2\sigma_i^2 are the nonzero eigenvalues of both. For centred data XX, XTX/(N−1)X^TX/(N-1) is the covariance matrix, so the right singular vectors of XX are the principal directions and σi2/(N−1)\sigma_i^2/(N-1) the variances along them.

python · try it yourself
import numpy as np

rng = np.random.default_rng(0)
A = rng.normal(size=(5, 3))
U, s, Vt = np.linalg.svd(A)                    # full: U 5x5, Vt 3x3
print("shapes:", U.shape, s.shape, Vt.shape)
Sigma = np.zeros((5, 3)); np.fill_diagonal(Sigma, s)
print("A = U Sigma V^T:", np.allclose(U @ Sigma @ Vt, A))
eig = np.linalg.eigvalsh(A.T @ A)[::-1]
print("sigma^2:", np.round(s**2, 6))
print("eig(A^T A):", np.round(eig, 6))
print("A v1 = sigma1 u1:", np.allclose(A @ Vt[0], s[0] * U[:, 0]))
shapes: (5, 5) (3,) (3, 3)
A = U Sigma V^T: True
sigma^2: [9.928184 2.921811 0.112128]
eig(A^T A): [9.928184 2.921811 0.112128]
A v1 = sigma1 u1: True

Thin SVD and the pseudo-inverse

For tall data matrices, the last m−nm - n columns of UU multiply zeros in Σ\Sigma. The thin SVD (full_matrices=False) drops them: UU becomes m×nm \times n. The pseudo-inverse is

A+=VΣ+UT,Σ+=diag⁡(1/σ1,…,1/σr,0,…)A^+ = V\Sigma^+U^T, \qquad \Sigma^+ = \operatorname{diag}(1/\sigma_1, \ldots, 1/\sigma_r, 0, \ldots)

and x=A+b\mathbf{x} = A^+\mathbf{b} is the minimum-norm least-squares solution of Ax≈bA\mathbf{x} \approx \mathbf{b}, valid even when AA is rank-deficient.

python · try it yourself
import numpy as np

A = np.array([[1.0, 1.0], [1.0, 1.0], [1.0, 1.0]])   # rank 1: columns identical
b = np.array([1.0, 2.0, 6.0])
U, s, Vt = np.linalg.svd(A, full_matrices=False)
s_inv = np.array([1 / x if x > 1e-10 else 0.0 for x in s])
x = Vt.T @ np.diag(s_inv) @ U.T @ b
print("singular values:", np.round(s, 6))
print("pinv solution:", np.round(x, 6), " numpy pinv:", np.round(np.linalg.pinv(A) @ b, 6))
print("fitted values:", np.round(A @ x, 6))
singular values: [2.44949 0.     ]
pinv solution: [1.5 1.5]  numpy pinv: [1.5 1.5]
fitted values: [3. 3. 3.]

With identical columns, any (x1,x2)(x_1, x_2) with x1+x2=3x_1 + x_2 = 3 fits equally well (the fitted value 3 is the mean of b\mathbf{b}); the pseudo-inverse picks the shortest, (1.5,1.5)(1.5, 1.5).

Geometry

The image of the unit sphere under AA is an ellipsoid with semi-axes σiui\sigma_i\mathbf{u}_i. The largest stretch is σ1=∥A∥2\sigma_1 = \lVert A \rVert_2, and the ratio σ1/σr\sigma_1/\sigma_r is the condition number, which measures how much AA can amplify errors. More on the geometry in the linear algebra SVD lesson.

In software

  • np.linalg.svd(X, full_matrices=False) is how scikit-learn's PCA and TruncatedSVD compute components.
  • np.linalg.lstsq and np.linalg.pinv use the SVD.
  • Recommenders and latent semantic analysis use truncated SVDs of large sparse matrices (Matrix Approximation).

Exercises

Exercise 1: Singular values by hand

Find the singular values of A=[300−200]A = \begin{bmatrix} 3 & 0 \\ 0 & -2 \\ 0 & 0 \end{bmatrix}.

Answer

3 and 2.

Solution

ATA=diag⁡(9,4)A^TA = \operatorname{diag}(9, 4), whose eigenvalues are 9 and 4; the singular values are their square roots. The minus sign goes into UU.

Exercise 2: Rank and condition number

A matrix has singular values 10,2,0.00110, 2, 0.001. What is its rank and its condition number?

Answer

Rank 3, condition number 10 00010\,000.

Solution

All three singular values are nonzero, so the rank is 3, but σ1/σ3=10/0.001=104\sigma_1/\sigma_3 = 10/0.001 = 10^4: the matrix is nearly rank 2, and solving with it can amplify relative errors in the data up to 10410^4 times.

Exercise 3: SVD of an outer product (code)

Compute the SVD of uvT\mathbf{u}\mathbf{v}^T with u=(1,2,2)\mathbf{u} = (1, 2, 2) and v=(3,4)\mathbf{v} = (3, 4). What is σ1\sigma_1?

Answer

σ1=∥u∥ ∥v∥=3⋅5=15\sigma_1 = \lVert \mathbf{u} \rVert\,\lVert \mathbf{v} \rVert = 3 \cdot 5 = 15, σ2=0\sigma_2 = 0.

Solution

uvT=15⋅u3(v5)T\mathbf{u}\mathbf{v}^T = 15 \cdot \frac{\mathbf{u}}{3}\left(\frac{\mathbf{v}}{5}\right)^T is already in SVD form with unit vectors.

python · try it yourself
import numpy as np
M = np.outer([1.0, 2.0, 2.0], [3.0, 4.0])
print(np.round(np.linalg.svd(M, compute_uv=False), 6) + 0)
[15.  0.]

Test yourself

Which matrices have an SVD?

  1. Only square ones
  2. Only symmetric ones
  3. Every real matrix
  4. Only invertible ones

The right singular vectors of AA are eigenvectors of…

  1. AA
  2. ATAA^TA
  3. AATAA^T
  4. A+ATA + A^T

The condition number of AA (full column rank) is…

  1. σ1σr\sigma_1 \sigma_r
  2. σ1/σr\sigma_1 / \sigma_r
  3. ∑iσi\sum_i \sigma_i
  4. det⁡A\det A