Singular Value Decomposition (SVD) can be computed in Python by using the numpy.linalg.svd function.
Linear Algebra
PROGRAMMES
IN PYTHON AND C LANGUAGES
UNIT − IV:
MATRIX DECOMPOSITION
COMPUTATION OF SVD BY
USING PYTHON
Singular
Value Decomposition (SVD) can be computed in Python by using the
numpy.linalg.svd function. This function takes a matrix as input and returns
the left singular vectors (U), the singular values (S), and the right singular
vectors (V transposed).
Here
is an example of how to compute SVD using numpy.
Python
import numpy as np
Create a sample matrix
A = np.array([[1, 2, 3],
[4, 5,6],
[7, 8, 9]
#Perform Singular Value Decomposition U, s, Vt =
np.linalg.svd(A)
# print the results
print ("Left Singular Vectors (U):")
print (U)
print("\nSingular Values (s):")
print (s)
print ("\nRight Singular Vectors Transposed(Vt):")
A print (Vt)
# Reconstruct the original matrix (optional)
# Note: s is a 1D array of singular values, so it needs to be
converted to a diagonal
Sigma = np.zeros((A.shapte[0], A.shape[1]))
Sigma [:A.shape[1], :A.shape[1]] = np.diag(s)
# For non−square matrices, adjust dimension
A_reconstructed = U @ Sigma @ vt
print ("\nReconstructed Matrix:")
bu print (A_reconstructed)
•
A = np.array(...): Defines the matrix for which SVD will be computed.
•
U, s, Vt = np.linalg.svd(A): This is the core function call.
U:A
matrix containing the left singular vectors as columns.
s:A ID array containing the singular values in
descending order.
Vt: A
matrix containing the right singular vectors as rows (it's already transposed,
hence Vt).
• Printing results:
The code then prints U, s, and Vt to display the computed components of the
SVD.
• Reconstruction
(Optional): The last part demonstrates how to
reconstruct the original matrix A from its SVD components.
•
Since s is a 1D array, it needs to be placed into a diagonal matrix (sigma)
with the appropriate dimensions before multiplication.
•
U @ Sigma @ Vt performs matrix multiplication to reconstruct A. Note that due
to numerical precision, the reconstructed matrix might have very small
differences from the original.
Linear Algebra: Practice Programs in Python and C Languages : Tag: maths, mathematics : - Computation of SVD by Using Python (Linear Algebra: Matrix Decomposition)
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Transforms and its Applications
MA25C03 2nd Semester EEE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Applied Physics (CE) II
PH25C02 2nd Semester Civil, Agri Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Applied Physics (EE) II
PH25C04 2nd Semester EEE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Applied Physics (ME) II
PH25C05 2nd Semester Mechanical Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Applied Chemistry (CE) II
CY25C02 2nd Semester Civil Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Applied Chemistry (ME) II
CY25C03 2nd Semester Mechanical Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Electron Devices
EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Civil and Mechanical Engineering
GE25C01 2nd Semester EEE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Drawing
ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures and Algorithms
CS25C04 2nd Semester EEE Dept | 2025 Regulation
Circuits and Network Analysis
EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Mechanics
ME25C02 2nd Semester Mech, Civil, Agri Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation