Singular Value Decomposition: SVD
Learn one of the most important matrix decompositions, singular value decomposition.
Definition
The Singular Value Decomposition (SVD) of an matrix, , is the factorization of , multiplied by the product of three matrices:
, where the matrices and are orthogonal and the matrix, , is a
SVD in numpy
We can compute SVD of a matrix, A
, using U,S,Vt = numpy.linalg.svd(A)
, where S
is an array containing diagonal entries of and Vt
is .
The diag
function in the implementation below converts the S
array into the generalized diagonal matrix, . We’ve used the D
variable to represent in the code.
import numpy as npfrom numpy.linalg import svd# Function to construct generalized diagonal matrixdef diag(S, m, n):D = np.zeros((m, n))k = len(S)D[:k, :k]=np.diag(S)return Dm, n = 2, 3A = np.random.rand(m, n)# Compute SVD using numpyU, S, Vt = svd(A)# Compute Frobenius normfrobNorm = np.linalg.norm(U.dot(diag(S, m, n)).dot(Vt)-A)R = [0, frobNorm]print(f'The Frobenius norm of UDV^T - A is {R[frobNorm>1e-10]}')print(f'U^TU\n{abs(np.round(U.T.dot(U), 1))}')print(f'V^TV\n{abs(np.round(Vt.dot(Vt.T), 1))}')
Singular values and vectors
- The diagonal values of are the singular values of .
- The columns of are the left singular vectors of .
- The rows of are the right singular vectors of .
We assume that the singular values in are in descending order. That’s how
numpy.linalg.svd
returns the values inS
. We can arrange the values in any order if we’re provided the corresponding arrangement of the columns of and the rows of .
Economic SVD
When has some zeros in the diagonal, we can drop them. We can also drop the corresponding columns in and the rows in . If is the diagonal matrix with only non-zero singular values, and and are the matrices of the corresponding columns and rows in and , respectively, then
...