Low-rank approximation
Reduce a matrix to a lower-rank approximation by retaining only its largest singular values and corresponding vectors.
NumPy · Function
Computes the singular value decomposition of an array, returning singular values and optionally the corresponding left and right singular-vector factors. It supports both individual matrices and stacks of matrices.
For each matrix in a possibly stacked input, the function computes a singular-value decomposition. With vectors requested, it returns left singular vectors, singular values, and the Hermitian-transposed right singular vectors; otherwise it returns only the singular values.
numpy.linalg.svd(a, full_matrices=True, compute_uv=True, hermitian=False)| Name | Type | Description |
|---|---|---|
a | (..., M, N) array_like | Input array whose final two dimensions are decomposed; higher-dimensional inputs are handled as stacks of matrices. |
full_matrices | bool, optional | If true, return full-sized singular-vector matrices; otherwise return the reduced form. |
compute_uv | bool, optional | If true, compute u and vh in addition to s. |
hermitian | bool, optional | If true, assume a is Hermitian, symmetric when real-valued, enabling a more efficient method for finding singular values. |
When compute_uv=True, returns u with shape (..., M, M) or (..., M, K), s with shape (..., K), and vh with shape (..., N, N) or (..., K, N), where K=min(M,N). When compute_uv=False, returns only s.
import numpy as np
A = np.array([[3.0, 1.0], [0.0, 2.0]])
u, s, vh = np.linalg.svd(A, full_matrices=False)
reconstructed = (u * s) @ vh
print(np.allclose(A, reconstructed)) # True
print(s) # singular values, descendingComputes a compact SVD and reconstructs the input matrix by broadcasting each column of u by its corresponding singular value before multiplying by vh.
Reduce a matrix to a lower-rank approximation by retaining only its largest singular values and corresponding vectors.
Inspect singular values to estimate numerical rank or identify ill-conditioned directions.
Decompose each matrix in a batch without writing an explicit loop over the leading dimensions.
It factors a two-dimensional array into u, s, and vh so that the matrix can be reconstructed as (u * s) @ vh when using the compact form. The singular values are returned in descending order.
Follow-up: How does full_matrices affect the result? With it enabled, u and vh use full square dimensions; disabling it uses reduced dimensions based on K = min(M, N).
compute_uv controls whether the singular vectors are calculated. When false, only s is returned; when true, the function returns u, s, and vh.
Follow-up: Why might compute_uv=False be useful? It avoids calculating and storing the singular-vector arrays when only spectral magnitudes are needed.
For an input with more than two dimensions, SVD is applied in stacked mode: the leading dimensions identify separate matrices, while the final two dimensions are decomposed. The output keeps those leading dimensions.
Follow-up: What is the shape of s for an input shaped (..., M, N)? It is (..., K), where K is the smaller of M and N.
numpy.linalg.eignumpy.linalg.eighnumpy.linalg.qrnumpy.linalg.matrix_ranknumpy.linalg.pinv