关于NumPy中SVD奇异值(s)维度与预期不符的疑问
Hey there! Let's clear up the confusion around the shape of the s array returned by NumPy's svd function. This is a common misunderstanding when first working with singular value decomposition, so let's break it down step by step.
The Math Behind SVD
First, a quick recap of how singular value decomposition (SVD) works for an m×n matrix A:
The decomposition takes the form:A = U @ Σ @ VT
Where:
Uis an m×m orthogonal matrix (columns are left singular vectors)VTis an n×n orthogonal matrix (rows are right singular vectors)Σis an m×n diagonal matrix where the diagonal elements are the singular values (sorted in descending order)
The key point here is that Σ can only have min(m, n) non-zero (or numerically non-negligible) diagonal entries. You can't have more singular values than the smaller dimension of your matrix—there simply isn't space for them in the diagonal of an m×n matrix.
Why NumPy Returns s With Shape (min(m,n),)
NumPy's svd function doesn't return the full Σ matrix directly. Instead, it returns the diagonal elements of Σ as a 1D array s, which has length equal to min(m, n). This is a space-efficient choice, since the rest of the Σ matrix is just zeros.
Let's apply this to your examples:
Example 1: 3×10 Matrix
Your matrix A has shape (3, 10) (m=3, n=10). Here, min(m, n) = 3, so s has shape (3,). Even though your matrix has a rank of 2 (the three rows are linearly dependent), NumPy still returns 3 singular values—one of them will be extremely small (numerically close to zero) because of the linear dependence.
Example 2: 3×2 Matrix
Your matrix A has shape (3, 2) (m=3, n=2). Here, min(m, n) = 2, so s has shape (2,). This matrix is full rank (rank 2), so both singular values are non-zero.
If You Need the Full Σ Matrix
If you want to reconstruct the full diagonal Σ matrix, you can use np.diag(s) and pad it with zeros to match the shape of A:
# For Example 1 (3×10 matrix) Σ = np.zeros((3, 10)) Σ[:3, :3] = np.diag(s) # For Example 2 (3×2 matrix) Σ = np.zeros((3, 2)) Σ[:2, :2] = np.diag(s)
This will give you the full m×n diagonal matrix that fits into the SVD equation.
内容的提问来源于stack exchange,提问作者venkysmarty

