Python:NumPy .svd()
Anonymous contributor
Published May 26, 2024
Contribute to Docs
The .svd() function is a mathematical technique that decomposes a matrix into three simpler matrices. This means it factorizes the matrix a into two unitary matrices U and Vh, along with a 1-D array s containing singular values (real and non-negative). This decomposition satisfies the equation a = U @ S @ Vh, where S is a diagonal matrix formed from the singular values in s.
Syntax
numpy.linalg.svd(a, full_matrices=True, compute_uv=True, hermitian=False)
Parameters:
a: The input matrix to be decomposed, with at least two dimensions.full_matrices: This parameter determines whether the function computes full-sized or reduced-sized matricesUandVh. Iffull_matricesis set toTrue, the function computes the full-sized matrices. If set toFalse, it computes only the essential parts ofUandVh. The default value isTrue.compute_uv: If set toTrue, both singular values and singular vectors (UandVh) are computed. If set toFalse, only the singular values are computed. The default value isTrue.hermitian: This parameter indicates whether the input matrixais Hermitian, meaning it is equal to its conjugate transpose. Whenhermitianis set toTrue, the function assumes thatais Hermitian and uses a more efficient algorithm tailored for such matrices. If set toFalse, it uses a general algorithm. The default value isFalse.
Example
The following example demonstrates how to perform Singular Value Decomposition (SVD) on a 2D matrix using NumPy:
import numpy as np# Create a random 2D matrixA = np.random.randn(5, 3)print(f"Original 2D matrix A (shape: {A.shape}): ")print(A)# Compute the factor by Singular Value DecompositionU, s, Vh = np.linalg.svd(A)# Print the resultprint("\nFactor of the given array by Singular Value Decomposition:")print(f"\nU (shape {U.shape}):")print(U)print(f"\nSingular values (s) (length {len(s)}):")print(s)print(f"\nVh (shape {Vh.shape}):")print(Vh)
This produces the following output:
Original 2D matrix A (shape: (5, 3)):[[ 0.59796872 -1.38507085 0.03524285][ 1.28932701 0.81797526 -1.69122659][ 0.23620894 -0.94818582 1.59777167][-0.10227955 -1.92855728 -0.87461468][ 1.47713324 -1.16547922 -1.28372167]]Factor of the given array by Singular Value Decomposition:U (shape (5, 5)):[[-0.28838331 -0.39982197 -0.20916472 -0.43801489 -0.72205993][-0.45730765 0.58585849 -0.28673931 0.48902595 -0.35535015][ 0.17498222 -0.54945226 -0.61671509 0.52775216 0.09286329][-0.45505181 -0.43538359 0.63541079 0.44567257 -0.03159222][-0.68557556 -0.07386184 -0.29991051 -0.30306796 0.58543495]]Singular values (s) (length 3):[3.25448644 2.86930282 1.35462929]Vh (shape (3, 3)):[[-0.51832291 0.47198441 0.71314239][ 0.11219578 0.864227 -0.49043225][-0.84779329 -0.17419071 -0.50090332]]
Codebyte Example
The following codebyte example shows the usage of the .svd() function:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to clean, analyze, and visualize data with Python and SQL.
- Includes 15 Courses
- With Certificate
- Beginner Friendly.55 hours