🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.linalg.svd()

AI & DATA SCIENCE // np-linalg-svd

np.linalg.svd() decomposes a matrix into three component matrices, U, S, and V transpose, whose product reconstructs the original matrix — a factorization that works for any matrix, not just square ones.

Syntax

np.linalg.svd(A, full_matrices=True)

Deep Dive Course

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.

editor.html
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)
localhost:3000

2Practical Example

Here is a real-world application of np.linalg.svd() showing how it is used in production NumPy code.

editor.html
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))
localhost:3000

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.

editor.html
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)
localhost:3000

Examples

Example 01Basic Usage
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)
Example 02Advanced Example
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))

Best Practices

  • 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
  • Use SVD for tasks like dimensionality reduction or low-rank approximation, keeping only the largest singular values and their corresponding vectors
  • Prefer SVD over eigendecomposition specifically when the matrix isn't square, since eigendecomposition only applies to square matrices

Interview Question

Why is SVD applicable to any matrix, including non-square ones, while eigendecomposition only works on square matrices?

Hint: Think about what eigenvectors/eigenvalues fundamentally require versus what singular vectors/values represent.

Eigendecomposition relies on eigenvectors, which are vectors mapped to a scalar multiple of themselves by the same matrix, a concept that only makes sense when the matrix maps a vector space back to itself, requiring a square matrix. SVD instead uses two separate sets of orthogonal vectors, left and right singular vectors, that can belong to different-dimensional spaces entirely, describing how the matrix transforms vectors from an n-dimensional input space into an m-dimensional output space, which is a well-defined operation for a rectangular, non-square matrix in a way that self-referential eigenvectors simply aren't.

Exercises

MediumPractice using np.linalg.svd() in a real scenario.
View Solution
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)

Frequently Asked Questions

Why is SVD applicable to any matrix, including non-square ones, while eigendecomposition only works on square matrices?

Eigendecomposition relies on eigenvectors, which are vectors mapped to a scalar multiple of themselves by the same matrix, a concept that only makes sense when the matrix maps a vector space back to itself, requiring a square matrix. SVD instead uses two separate sets of orthogonal vectors, left and right singular vectors, that can belong to different-dimensional spaces entirely, describing how the matrix transforms vectors from an n-dimensional input space into an m-dimensional output space, which is a well-defined operation for a rectangular, non-square matrix in a way that self-referential eigenvectors simply aren't.

Related Functions

np-linalg-eignp-linalg-invndarray-t