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 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 of rank factors as
with and orthogonal, and diagonal with singular values (the rest are 0). The columns are the right singular vectors, the the left singular vectors, and .
The link to eigendecomposition
So the are eigenvectors of , the eigenvectors of , and are the nonzero eigenvalues of both. For centred data , is the covariance matrix, so the right singular vectors of are the principal directions and the variances along them.
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 columns of multiply zeros in . The thin SVD (full_matrices=False) drops them: becomes . The pseudo-inverse is
and is the minimum-norm least-squares solution of , valid even when is rank-deficient.
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 with fits equally well (the fitted value 3 is the mean of ); the pseudo-inverse picks the shortest, .
Geometry
The image of the unit sphere under is an ellipsoid with semi-axes . The largest stretch is , and the ratio is the condition number, which measures how much 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'sPCAandTruncatedSVDcompute components.np.linalg.lstsqandnp.linalg.pinvuse 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 .
Answer
3 and 2.
Solution
, whose eigenvalues are 9 and 4; the singular values are their square roots. The minus sign goes into .
Exercise 2: Rank and condition number
A matrix has singular values . What is its rank and its condition number?
Answer
Rank 3, condition number .
Solution
All three singular values are nonzero, so the rank is 3, but : the matrix is nearly rank 2, and solving with it can amplify relative errors in the data up to times.
Exercise 3: SVD of an outer product (code)
Compute the SVD of with and . What is ?
Answer
, .
Solution
is already in SVD form with unit vectors.
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?
- Only square ones
- Only symmetric ones
- Every real matrix
- Only invertible ones
The right singular vectors of are eigenvectors of…
The condition number of (full column rank) is…