Hello! Welcome back to your course on Linear Algebra.
In our previous lesson, we explored eigenvalues and eigenvectors, discovering the special vectors that a matrix only stretches or shrinks. This concept, however, is limited to square matrices. Today, we're going to learn about a more general and incredibly powerful technique that works for any matrix, square or rectangular.
This lesson addresses the learning outcome: Apply Singular Value Decomposition (SVD) to decompose a matrix. SVD is arguably one of the most important matrix factorizations in data science, forming the backbone of algorithms for dimensionality reduction (like PCA), recommendation systems, and image compression. We will learn what SVD is, how to calculate it by hand, and see how it's done in Python.
1. The Core Idea: Decomposing Any Matrix
Singular Value Decomposition states that any matrix can be factored into three other matrices:
Where:
- is an orthogonal matrix whose columns are the left singular vectors.
- (Sigma) is an diagonal matrix. The diagonal entries, , are the singular values of A. They are always non-negative and are arranged in descending order ().
- is the transpose of an orthogonal matrix , whose columns are the right singular vectors.
The diagram below gives a great visual representation of how these matrices fit together.

Conceptually, you can think of SVD as breaking down the original transformation into three separate, simpler operations:
- A rotation or reflection ().
- A scaling along the axes ().
- Another rotation or reflection ().
The crucial connection to our last lesson is this: the singular values () in are the square roots of the eigenvalues of the square matrix . The columns of are the corresponding eigenvectors of .
2. How to Calculate SVD: A Step-by-Step Process
Let's outline the manual process for finding the SVD of a matrix . This process directly builds upon your knowledge of finding eigenvalues and eigenvectors.
- Compute . This will always result in a square, symmetric matrix.
- Find the eigenvalues of . Solve the characteristic equation .
- Construct . The singular values are the square roots of the eigenvalues you just found: . Place these values, in descending order, on the main diagonal of a matrix with the same dimensions as .
- Find . The columns of are the normalized eigenvectors of corresponding to the eigenvalues.
- Find . The columns of can be calculated using the formula , where are the columns of .
- Assemble the decomposition. Combine the matrices to get . Remember to take the transpose of .
This process might seem complex, so let's watch a video where it's done step-by-step for a 2x2 matrix.
2. SVD | Singular Value Decomposition | Dimensionality Reduction | Solved Example by Mahesh Huddar
This video by Mahesh Huddar provides a clear, worked example of performing SVD on a 2x2 matrix. It follows the exact algorithm we just outlined.
Watch the entire video (up to 11:46). As you watch, map the steps in the video to the 6-step algorithm above: Steps 1 & 2: Finding eigenvalues of AᵀA (01:07 - 04:08). Step 4: Finding the normalized eigenvectors to form V (04:08 - 07:21). Step 3: Constructing the singular matrix Σ (07:42 - 08:39). Step 5: Calculating U using A and V (08:39 - 10:44). Step 6: Assembling and verifying the final decomposition (10:44 onwards).
3. SVD for a Non-Square Matrix
The real power of SVD is that it works for rectangular matrices. The procedure is the same, but the dimensions of and will be different. Let's walk through an excellent text-based example.
Singular Value Decomposition (SVD)
This article from GeeksforGeeks demonstrates how to perform SVD on a rectangular 2x3 matrix. This is a great way to solidify your understanding of the process.
Read the section 'How to perform Singular Value Decomposition'. Follow along with the calculations from Step 1 to Step 5. Notice how the shape of the original matrix A (2x3) influences the shape of the final Σ matrix.
Test your understanding!
In the GeeksforGeeks example, the original matrix is 2x3. The resulting matrix is also 2x3. Why is the third column of all zeros?
Show answer
The matrix is a 3x3 matrix, but the matrix is a 2x2 matrix. The number of non-zero singular values of a matrix is equal to its rank. In this case, the rank of can be at most 2 (since it only has 2 rows). Therefore, there are only two non-zero singular values ( and ). The third singular value, , must be 0. This is reflected in the third column of the matrix being all zeros.
4. SVD in Python: A Practical First Look
While performing SVD by hand is essential for understanding the mechanics, in practice, you'll always use a library like NumPy. As you prepare for your GATE exam and future work in data science, being able to apply these concepts in code is crucial.
The numpy.linalg module has a convenient function, svd(), that does all the heavy lifting for us. Let's get an introduction to how it works and what its outputs mean.
Easiest Way to Understanding Singular Value Decomposition (SVD) with Python: numpy.linalg.svd
This video from 'Dr. Data Science' explains how to use numpy.linalg.svd and provides insights into a major application of SVD: low-rank approximation.
Watch the video from 01:13 to 20:38. Focus on these key points: (01:13 - 05:14): The properties of the U, S, and V matrices. Note that the video calls the singular value matrix 'S', which is a common notation, but it's the same as our Σ. (05:14 - 13:15): How to call np.linalg.svd and reconstruct the original matrix from its components. Pay close attention to how the 1D array of singular values is converted into a 2D diagonal matrix. (13:15 - 20:38): The concept of low-rank approximation. This is a key application where you use only the largest singular values to create a compressed version of the original matrix.
Here is a simple code snippet demonstrating the core idea from the video. You can try running this yourself in a Python environment.
import numpy as np
# Define a matrix A
A = np.array([[3, 2, 2],
[2, 3, -2]])
print("Original Matrix A:")
print(A)
# Perform SVD
# The svd() function returns U, a 1D array of singular values (s), and V_transpose (Vt)
U, s, Vt = np.linalg.svd(A)
print("\nMatrix U:")
print(U)
print("\nSingular values (s):")
print(s)
print("\nMatrix V_transpose (Vt):")
print(Vt)
# To reconstruct A, we need to convert the 1D array 's' into a diagonal matrix 'Sigma'
# with the correct dimensions (same as A's).
Sigma = np.zeros(A.shape)
Sigma[:min(A.shape), :min(A.shape)] = np.diag(s)
print("\nDiagonal Matrix Sigma:")
print(Sigma)
# Reconstruct the original matrix
A_reconstructed = U @ Sigma @ Vt
print("\nReconstructed Matrix A:")
print(np.round(A_reconstructed, decimals=2)) # Rounding to handle floating point inaccuracies
# Verify the reconstruction is close to the original
assert np.allclose(A, A_reconstructed)
print("\nVerification successful: U @ Sigma @ Vt is indeed A!")
This ability to decompose a matrix and then reconstruct it (or an approximation of it) is fundamental to many advanced machine learning techniques.
Conclusion
Congratulations on completing this lesson on Singular Value Decomposition! It's a challenging but deeply rewarding topic. You've bridged the gap from the theoretical world of eigenvalues for square matrices to a practical tool that can be applied to any matrix.
Key Takeaways:
- Any matrix can be decomposed into , where and are orthogonal matrices and is a diagonal matrix.
- The singular values in are the square roots of the eigenvalues of , and they represent the "magnitudes" of the principal components of the matrix.
- The columns of are the eigenvectors of .
- SVD can be computed manually by finding eigenvalues/eigenvectors, but in practice, we use library functions like
numpy.linalg.svd. - A major application of SVD is creating low-rank approximations of a matrix for tasks like data compression.
In our next lesson, we will fully focus on the practical side. We will use NumPy to compute determinants, inverses, eigenvalues, and SVD of matrices. This will solidify your coding skills and ensure you can efficiently apply all the linear algebra concepts we've covered in this module.