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.
Spectral Reconstruction A = QΛQᵀ
Read diagram labels
- Q
- rotate
- Λ
- scale
- Qᵀ
- rotate back
- ×
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: TrueSolve and Reconstruct a Small Eigensystem
Find the eigensystem of and state its spectral reconstruction.
1., so the eigenvalues are and .
2.Normalized eigenvectors are and .
With and , multiplication verifies .
2. 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 compact rank- form uses , , and . Keeping only the largest singular values produces the best rank- approximation under the Frobenius norm.
The Movie Recommender Example: Rows can represent users and columns movies. A truncated SVD compresses the grid into user factors, factor strengths, and movie factors; discarded small singular directions often contain noise.
Compact SVD Shape Decomposition
Read diagram labels
- U
- m × r
- Σ
- r × r
- Vᵀ
- r × n
- ×
SVD Shape Trace
A matrix has shape and rank . Give its compact-SVD shapes and the factor shapes for a rank-5 reconstruction.
1. is , is , and is .
2.For , the product is .
The reconstruction remains , but uses only five singular directions.
Rank- Reconstruction Error
If singular values are , find the Frobenius errors of the best rank-1 and rank-2 approximations.
1.The squared truncation error is the sum of squared discarded singular values.
2..
.
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 centered coordinates onto the leading eigenvectors. The explained-variance ratio of component is .
PCA Projects Data onto Principal Axes
Read diagram labels
- X₀
- n × d
- Wₖ
- d × k
- Z
- n × k
- ×
PCA from Centering to Projection
For points , find the first principal direction, projected coordinates, and explained variance.
1.The mean is ; centered rows are .
2.With divisor , the covariance is , whose eigenvalues are and .
3.The first direction is and the projections are .
Its explained-variance ratio is .
Cumulative Component Choice
If PCA eigenvalues are , how many components retain at least of the variance?
1.Total variance is .
2.One component retains ; two retain .
Choose two components.
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
Mathematics for Machine Learning
https://mml-book.github.io/
NumPy: Singular Value Decomposition
https://numpy.org/doc/stable/reference/generated/numpy.linalg.svd.html
NumPy: Eigenvalues for Symmetric Matrices
https://numpy.org/doc/stable/reference/generated/numpy.linalg.eigh.html
Scikit-learn: PCA
https://scikit-learn.org/stable/modules/decomposition.html#pca
