Matrix Decompositions for Machine Learning
breaking matrices into fundamental components for analysis and computation
Core Concepts Covered
- Eigenvalues, eigenvectors, and spectral decomposition
- Singular Value Decomposition (SVD) and low-rank approximation
- Principal Component Analysis (PCA) for dimensionality reduction
Local Setup Recommendation
To execute and experiment with the code cells below on your local machine, ensure you have set up your isolated virtual environments and scientific libraries by following the detailed protocols in Unit 03: Environment Setup or run them in Google Colab.
1. Eigenvalues, Eigenvectors, and Spectral Decomposition
Just as integers can be factored into prime numbers (e.g., ), matrices can be decomposed into fundamental vector and scaling components. This allows us to analyze the structural properties of complex linear transformations.
An Eigenvector of a square matrix is a special non-zero vector such that multiplying by only scales the vector, without changing its geometric direction. This scaling factor is called the Eigenvalue . Mathematically, this is written as:
The Stretching Sheet Analogy: Imagine taking a square rubber sheet and stretching it diagonally. Most points on the sheet move in new directions. However, points lying along the exact diagonal line of the stretch continue to point in the exact same direction—they have only been pulled further apart. This diagonal line represents the Eigenvector of the stretching transformation, and the amount of stretch represents its Eigenvalue.
If a matrix is symmetric (), we can perform Spectral Decomposition. This factors the matrix into an orthogonal basis of eigenvectors and a diagonal matrix of eigenvalues:
where is an orthogonal matrix whose columns are the eigenvectors of , and (Lambda) is a diagonal matrix containing the corresponding eigenvalues. We use eigenvalues to measure the scale of variance along specific vector directions.
import numpy as np
# Create a symmetric matrix A
A = np.array([[4, 2], [2, 4]])
# Compute eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(A)
print("Eigenvalues:", eigenvalues)
# Columns of eigenvectors correspond to eigenvectors
print("Eigenvector Matrix:\n", eigenvectors)
# Verify Av = lambda v for the first eigenvector
v0 = eigenvectors[:, 0]
lambda0 = eigenvalues[0]
print("Av0:", A @ v0)
print("lambda0 * v0:", lambda0 * v0)
print("Av0 equals lambda0 * v0:", np.allclose(A @ v0, lambda0 * v0))Eigenvalues: [6. 2.]
Eigenvector Matrix:
[[ 0.70710678 -0.70710678]
[ 0.70710678 0.70710678]]
Av0: [4.24264069 4.24264069]
lambda0 * v0: [4.24264069 4.24264069]
Av0 equals lambda0 * v0: True2. Singular Value Decomposition (SVD)
While eigensystem decompositions are mathematically restricted strictly to square matrices, Singular Value Decomposition (SVD) is a highly robust factorization that works on *any* rectangular matrix (such as a table with samples and features).
SVD decomposes into three matrices:
• : An orthogonal matrix containing the left-singular vectors of (which represent eigenvectors of ).
• : A diagonal matrix containing positive singular values sorted in descending order of magnitude.
• : The transpose of an orthogonal matrix containing the right-singular vectors of (which represent eigenvectors of ).
The Movie Recommender Example: Imagine a matrix where rows represent users and columns represent movies. The cell is the rating user gave to movie . SVD factorizes this rating grid into: User-concept profiles (), mapping users to latent concepts (like *Action* or *Romance*); concept importance scales (); and Movie-concept profiles (), mapping movies to those same concepts. By keeping only the top largest singular values in (a low-rank approximation), we compress the massive rating matrix, filtering out minor noise and predicting missing user reviews smoothly.
3. Dimensionality Reduction & Principal Component Analysis (PCA)
In high-dimensional datasets, features are often highly redundant (for instance, house area in square feet vs. square meters). We use Principal Component Analysis (PCA) to reduce dataset dimensions while retaining as much variance (information) as possible.
To perform PCA on a dataset matrix :
1. Zero-center the data by subtracting the mean of each feature: .
2. Compute the Covariance Matrix: . This matrix measures how all features vary together.
3. Compute the Eigensystem: Find the eigenvectors and eigenvalues of the covariance matrix . The eigenvector corresponding to the largest eigenvalue is the First Principal Component—the axis in coordinate space along which the data is most spread out (maximum variance).
4. Project: Project our original high-dimensional coordinates onto these principal eigenvectors, compressing the coordinates onto fewer dimensions while minimizing information loss.
Interactive Practice Quiz
Test your understanding with instant feedback
Which of the following describes the definition of an Eigenvector and Eigenvalue for a matrix ?
Why is Singular Value Decomposition (SVD) preferred over Eigensystem decomposition for general-purpose dataset matrices?
In SVD decomposition , what does the diagonal matrix contain?
In Principal Component Analysis (PCA), the principal components are calculated as the eigenvectors of which matrix?
How do we compress data using SVD low-rank approximations?
If a matrix is symmetric (), which of the following properties must its eigenvalues and eigenvectors satisfy?
In Spectral Decomposition , what does the diagonal matrix represent?
For a dataset with samples and features, the dimension of the right-singular vector matrix in SVD is:
Which of the following describes the first step of Principal Component Analysis (PCA)?
The first principal component axis extracted during PCA represents:
Further Readings
Explore these highly recommended external references to deepen your understanding
