Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.linalg.svd

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.

How it works

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.

Syntax

numpy.linalg.svd(a, full_matrices=True, compute_uv=True, hermitian=False)

Parameters

NameTypeDescription
a(..., M, N) array_likeInput array whose final two dimensions are decomposed; higher-dimensional inputs are handled as stacks of matrices.
full_matricesbool, optionalIf true, return full-sized singular-vector matrices; otherwise return the reduced form.
compute_uvbool, optionalIf true, compute u and vh in addition to s.
hermitianbool, optionalIf true, assume a is Hermitian, symmetric when real-valued, enabling a more efficient method for finding singular values.

Returns

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.

Key points

  • Computes the singular value decomposition of an array.
  • For a two-dimensional input in reduced mode, u and the Hermitian transpose of vh have orthonormal columns.
  • For stacked inputs, the leading dimensions of the outputs match the corresponding leading dimensions of a.
  • The singular-value vector for each matrix is sorted in descending order.
  • Use compute_uv to choose between singular values alone and the full set of singular-vector outputs.
  • The hermitian option tells NumPy to treat the input as Hermitian, or symmetric when it contains real numbers.

Common mistakes

  • If compute_uv is false, the result contains only the singular values, so unpacking three outputs raises an error.
  • Singular values are ordered from largest to smallest; do not assume the returned order is arbitrary.
  • Setting hermitian=True is appropriate only when the input is Hermitian, or symmetric for real-valued data.

Example: Compact SVD and reconstruction

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, descending

Computes a compact SVD and reconstructs the input matrix by broadcasting each column of u by its corresponding singular value before multiplying by vh.

When to use it

Low-rank approximation

Reduce a matrix to a lower-rank approximation by retaining only its largest singular values and corresponding vectors.

Rank and conditioning analysis

Inspect singular values to estimate numerical rank or identify ill-conditioned directions.

Batched linear algebra

Decompose each matrix in a batch without writing an explicit loop over the leading dimensions.

Interview questions

QWhat does numpy.linalg.svd compute?

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).

QWhat does compute_uv change?

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.

QHow does SVD handle stacked arrays?

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.eig
  • numpy.linalg.eigh
  • numpy.linalg.qr
  • numpy.linalg.matrix_rank
  • numpy.linalg.pinv

Source: numpy.linalg.svd. Checked 2026-10-15.