You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于NumPy中SVD奇异值(s)维度与预期不符的疑问

Understanding NumPy's SVD Singular Values Shape

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:

  • U is an m×m orthogonal matrix (columns are left singular vectors)
  • VT is 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:27:03