NumPy – Singular Value Decomposition (SVD)
Singular Value Decomposition (SVD) is one of the most powerful techniques in linear algebra and data science.
It is widely used in:
- Machine learning
- Data compression
- Recommendation systems
- Image processing
- Natural language processing
In Python, NumPy provides a simple function np.linalg.svd() to perform SVD.
What is SVD?
SVD is a matrix factorization technique that breaks a matrix into three simpler matrices.
Formula:
A = U Σ Vᵀ
Where:
- A = original matrix
- U = left singular vectors
- Σ (Sigma) = singular values (diagonal matrix)
- Vᵀ = right singular vectors transpose
Simple Meaning
SVD breaks a complex matrix into simpler components to analyze structure and patterns.
Import NumPy
import numpy as np
1. Basic SVD in NumPy
import numpy as np
A = np.array([[1, 2],
[3, 4],
[5, 6]])
U, S, VT = np.linalg.svd(A)
print("U matrix:\n", U)
print("\nSingular Values:\n", S)
print("\nVT matrix:\n", VT)
Output:
U matrix:
...
Singular Values:
[9.52551809 0.51430058]
VT matrix:
...
Explanation
- U → represents row space
- S → strength/importance of features
- VT → represents column space
2. Reconstruct Original Matrix
SVD can rebuild the original matrix:
import numpy as np
A = np.array([[1, 2],
[3, 4],
[5, 6]])
U, S, VT = np.linalg.svd(A)
Sigma = np.zeros((3, 2))
np.fill_diagonal(Sigma, S)
A_reconstructed = U @ Sigma @ VT
print(A_reconstructed)
3. Understanding Singular Values
import numpy as np
A = np.array([[2, 4],
[1, 3]])
U, S, VT = np.linalg.svd(A)
print("Singular Values:", S)
Meaning:
- Large values → important features
- Small values → less important information
4. SVD for Image Compression (Concept)
SVD can compress images by:
- Keeping only top singular values
- Removing noise
- Reducing storage size
5. Low-Rank Approximation
import numpy as np
A = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
U, S, VT = np.linalg.svd(A)
k = 2 # keep top 2 features
A_approx = U[:, :k] @ np.diag(S[:k]) @ VT[:k, :]
print(A_approx)
Real-World Applications of SVD
1. Machine Learning
- Dimensionality reduction
- Feature extraction
2. Recommendation Systems
- Netflix / YouTube recommendations
3. Image Processing
- Compression
- Noise reduction
4. NLP (Natural Language Processing)
- Latent Semantic Analysis (LSA)
Why SVD is Important?
SVD helps to:
- Simplify complex data
- Extract important patterns
- Reduce dimensions
- Improve model performance
SVD vs Eigen Decomposition
| Concept | Use |
|---|---|
| Eigen decomposition | Square matrices only |
| SVD | Any matrix (m × n) |
Common Error
❌ Shape confusion
ValueError: shapes not aligned
Solution:
Ensure correct matrix multiplication order:
U @ Sigma @ VT
Why Use NumPy?
Using NumPy allows:
- Fast SVD computation
- Stable numerical results
- Easy matrix factorization
Combined with Python, it becomes essential for AI and data science.
Summary
Singular Value Decomposition (SVD) is a powerful technique that breaks a matrix into simpler components:
A = U Σ Vᵀ
NumPy makes this process simple using:
np.linalg.svd(A)
Conclusion
SVD is a foundational concept in linear algebra and machine learning. It is widely used for compression, feature extraction, and data analysis. With NumPy, implementing SVD becomes easy and efficient.


0 Comments