Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Singular Value Decomposition (SVD) in Python

  • Abhishek WasnikAbhishek Wasnik
  • November 30, 2020
  • Python Programming Examples
Singular Value Decomposition (SVD) in Python

Singular Value Decomposition (SVD) is one of the widely used methods for dimensionality reduction. SVD decomposes a matrix into three other matrices.

If we see matrices as something that causes a linear transformation in the space then with Singular Value Decomposition we decompose a single transformation in three movements.

in this article we’ll see different methods to implement SVD.

Singular Value Decomposition Basics

SVD factors a single matrix into matrix U, D and V* respectively.

SVD
SVD

where,

  • U and V* are orthogonal matrices.
  • D is a diagonal matrix of singular values.

The SVD can also be seen as the decomposition of one complex transformation in 3 simpler transformations (rotation, scaling, and rotation).

In terms of transformations

  • Matrices U and V* causes rotation
  • Diagonal matrix D causes scaling.

So basically it allows us to express our original matrix as a linear combination of low-rank matrices. Only the first few, singular values are large.

The terms other than the first few can be ignored without losing much information and this is why SVD is referred to as a dimensionality reduction technique.

Implementation of SVD in Python

Let’s begin with the implementation of SVD in Python. We’ll work with multiple libraries to demonstrate how the implementation will go ahead.

1. Using Numpy

Python Numpy having capabilities to implement most Linear Algebra methods offers easy implementation of SVD.

We will use numpy.linalg module which has svd class to perform SVD on a matrix.

import numpy as np

#Creating a matrix A
A = np.array([[3,4,3],[1,2,3],[4,2,1]])

#Performing SVD
U, D, VT = np.linalg.svd(A)

#Checking if we can remake the original matrix using U,D,VT
A_remake = (U @ np.diag(D) @ VT)
print(A_remake)
Restored Matrix
Restored Matrix

D is a 1D array instead of a 2D array. D is a diagonal matrix with most of the values ends up being zero, such a matrix is called a sparse matrix. to save space it is returned as a 1D array.

2. Using scikit-learn

We will use TruncatedSVD class from sklearn.decomposition module.

In TruncatedSVD we need to specify the number of components we need in our output, so instead of calculating whole decompositions we just calculate the required singular values and trim the rest.

#Importing required modules
import numpy as np
from sklearn.decomposition import TruncatedSVD

#Creating array 
A = np.array([[3,4,3],[1,2,3],[4,2,1]])

#Fitting the SVD class
trun_svd =  TruncatedSVD(n_components = 2)
A_transformed = svd.fit_transform(A)

#Printing the transformed matrix
print("Transformed Matrix:")
print(A_transf)
Transformed Matrix
Transformed Matrix

Conclusion

In this article, we saw how we can implement Singular Value Decomposition (SVD) using libraries like Numpy and scikit-learn.

Happy Learning!

Abhishek Wasnik
Abhishek Wasnik
Articles: 29
Closures in Python – A Practical Reference
Previous Post Closures in Python - A Practical Reference
Next Post Label Encoding in Python - A Quick Guide!
Label Encoding in Python – A Quick Guide!

Related Posts

How to Calculate Distance Between Two Points in Python

How to Calculate Distance Between Two Points in Python

  • November 26, 2026
CIFAR 10 Dataset: Load and Use in Python

CIFAR 10 Dataset: Load and Use in Python

  • November 24, 2026
Maximum A Posteriori Estimation in Python

Maximum A Posteriori Estimation in Python

  • November 24, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.