Singular Value Decomposition factors any m by n matrix A into U, an orthogonal matrix of left singular vectors, S, a 1D array of singular values sorted from largest to smallest representing the transformation's scaling along each principal direction, and Vt, an orthogonal matrix of right singular vectors, such that A equals U times a diagonal matrix built from S times Vt. Unlike eigendecomposition, SVD works on any matrix, including non-square and singular ones, which makes it one of the most broadly useful decompositions in numerical linear algebra — it underlies techniques like dimensionality reduction (PCA), pseudo-inverse computation, and low-rank matrix approximation.
1Understanding np.linalg.svd()
Singular Value Decomposition factors any m by n matrix A into U, an orthogonal matrix of left singular vectors, S, a 1D array of singular values sorted from largest to smallest representing the transformation's scaling along each principal direction, and Vt, an orthogonal matrix of right singular vectors, such that A equals U times a diagonal matrix built from S times Vt. Unlike eigendecomposition, SVD works on any matrix, including non-square and singular ones, which makes it one of the most broadly useful decompositions in numerical linear algebra — it underlies techniques like dimensionality reduction (PCA), pseudo-inverse computation, and low-rank matrix approximation.
np.linalg.svd() returns the singular values S as a plain 1D array, not the actual diagonal matrix from the decomposition — reconstruct the diagonal matrix explicitly with np.diag(S), adjusted for shape, if you need to actually multiply U, the diagonal matrix, and Vt back together.
import numpy as np
A = np.array([[1, 0], [0, 1], [1, 1]])
U, S, Vt = np.linalg.svd(A, full_matrices=False)
print(S)2Practical Example
Here is a real-world application of np.linalg.svd() showing how it is used in production NumPy code.
import numpy as np
A = np.array([[1, 0], [0, 1], [1, 1]])
U, S, Vt = np.linalg.svd(A, full_matrices=False)
reconstructed = U @ np.diag(S) @ Vt
print(np.round(reconstructed, 10))3Best Practices
Follow these guidelines when working with np.linalg.svd():
1. Remember S comes back as a 1D array of singular values, not a ready-to-multiply diagonal matrix — build the diagonal matrix explicitly if you need to reconstruct A
2. Use SVD for tasks like dimensionality reduction or low-rank approximation, keeping only the largest singular values and their corresponding vectors
3. Prefer SVD over eigendecomposition specifically when the matrix isn't square, since eigendecomposition only applies to square matrices
Tip: np.linalg.svd() returns the singular values S as a plain 1D array, not the actual diagonal matrix from the decomposition — reconstruct the diagonal matrix explicitly with np.diag(S), adjusted for shape, if you need to actually multiply U, the diagonal matrix, and Vt back together.
import numpy as np
A = np.array([[1, 0], [0, 1], [1, 1]])
U, S, Vt = np.linalg.svd(A, full_matrices=False)
print(S)