[MMD]
Linear Algebra Basics Chapter 4: Eigenvalues, Eigenvectors, and PCA
In Chapter 3 we covered vector operations, linear transformations, matrix multiplication, and inverses. In this final installment, we'll interpret determinants geometrically, dive deep into eigenvalues and eigenvectors — the crown jewel of linear algebra — and trace exactly how they power PCA (Principal Component Analysis) in machine learning.
This post is based on Week 4 of DeepLearning.AI's Mathematics for Machine Learning and Data Science course.
What You'll Learn in This Chapter#
- Interpreting determinants as area (or volume)
- The product rule and inverse rule for determinants
- The concepts of Basis and Span
- Intuition and computation of eigenvalues and eigenvectors
- PCA — dimensionality reduction using variance, covariance, and eigenvectors
- Markov matrices and discrete dynamical systems
Singularity and Rank of Linear Transformations#
Linear transformations, like matrices, can be classified as singular or non-singular.
| Transformation Type | Result | Rank |
|---|---|---|
| Non-singular transformation | Covers the entire plane | 2 (in 2D) |
| Singular transformation (line) | Maps onto a single line | 1 |
| Singular transformation (point) | Collapses to the origin | 0 |
A non-singular linear transformation covers every point in the plane. A singular transformation compresses the plane onto a line or a point.
Determinant = Area (Geometric Interpretation)#
A determinant is more than just a number. It represents the scaling factor of area (or volume) — how much a linear transformation stretches or shrinks space.
Apply this transformation to the unit square (area = 1), and the resulting parallelogram has area .
| Determinant Value | Meaning |
|---|---|
| Space collapses to a line or point (area = 0) | |
| Space expands, orientation preserved | |
| Space expands, orientation reversed (reflection) |
Key insight: A singular matrix compresses area down to 0, which is exactly why .
Determinant of a Product#
The area scaling factor of two composed transformations equals the product of each transformation's scaling factor.
Important consequence:
The product of a singular and a non-singular matrix is always singular.
Determinant of an Inverse#
When is non-singular (invertible):
Intuition: If stretches space by a factor of 5, then must shrink it by to undo the transformation.
Examples:
Basis#
A basis is the minimal set of vectors that defines a vector space.
The defining property of a basis: every point in the space can be expressed as a linear combination of the basis vectors.
Requirements to form a basis:
- The vectors must be linearly independent (no vector can be expressed as a linear combination of the others)
- Vectors pointing in the same or opposite directions cannot form a basis
Span#
The span of a set of vectors is the set of all points reachable by taking every possible linear combination of those vectors.
| Number of Vectors | Span |
|---|---|
| 1 vector | A line (1-dimensional) |
| 2 linearly independent vectors | A plane (2-dimensional) |
| 2 linearly dependent vectors | A line (1-dimensional only) |
Basis = minimal spanning set: the smallest number of linearly independent vectors needed to span the space.
If you have more vectors than the dimension of the space, there will always be linear dependence among them.
Eigenbasis#
When a linear transformation is applied, most vectors change direction. But certain special vectors preserve their direction and only change in magnitude (scale). These are called eigenvectors.
- : eigenvector (direction preserved)
- : eigenvalue (scaling factor)
Intuition: When you look at data along an eigenvector direction, the matrix multiplication (a complex operation) reduces to a simple scalar multiplication.
Computing Eigenvalues and Eigenvectors#
The Core Formula#
For to exist, must be a singular matrix:
2×2 Matrix Example#
Step 1 — Characteristic polynomial:
Eigenvalues:
Step 2 — Eigenvector for :
Eigenvector for :
Eigenvalues and eigenvectors always come in pairs.
Number of Eigenvectors#
| Matrix Size | Eigenvalue Characteristics | Number of Eigenvectors |
|---|---|---|
| 2×2 | 2 distinct eigenvalues | Always 2 |
| 2×2 | Repeated eigenvalue | 1 or 2 |
| 3×3 | 3 distinct eigenvalues | Always 3 |
| 3×3 | One repeated eigenvalue | 2 or 3 |
| 3×3 | One eigenvalue with multiplicity 3 | 1 to 3 |
Note: Eigenvalues and eigenvectors can only be computed for square matrices.
Dimensionality Reduction and Projection#
The goal of dimensionality reduction is to map high-dimensional data into a lower-dimensional space while preserving as much information as possible.
Projection Formula#
Projecting a data point onto a unit vector :
- : the dot product gives the projection value
- Normalizing to a unit vector () ensures no stretching
The amount of information preserved depends on the direction you project onto. To minimize information loss, you should choose the direction of maximum variance (spread).
Variance and Covariance#
Understanding PCA requires two statistical concepts.
Variance#
How spread out the data is from its mean.
Covariance#
Whether two variables tend to move together in the same direction.
| Covariance Value | Meaning |
|---|---|
| Positive | When is large, tends to be large too (same direction) |
| 0 | No linear relationship between the two variables |
| Negative | When is large, tends to be small (opposite direction) |
Even if the mean and variance are identical, different covariance means the data patterns are completely different.
Covariance Matrix#
A matrix that captures all variances and covariances among multiple variables.
- Diagonal entries: variance of each variable
- Off-diagonal entries: covariance between pairs of variables
- Symmetric matrix:
Matrix notation:
Here is the mean-centered (centered) data matrix.
PCA — Principal Component Analysis#
PCA combines projection + eigenvalues/eigenvectors + covariance matrix to reduce dimensionality while preserving as much information as possible.
Core Idea#
The eigenvectors of the covariance matrix = the directions along which the data spreads the most.
The eigenvector corresponding to the largest eigenvalue = the direction containing the most information (the 1st principal component).
PCA Step by Step#
Step 1 — Center the data
Subtract the mean of each column so every feature has zero mean.
Step 2 — Compute the covariance matrix
Step 3 — Compute eigenvalues and eigenvectors
Sort eigenvalues in descending order: ()
Step 4 — Build the projection matrix
Select as many eigenvectors as the target number of dimensions to form the projection matrix . (Normalize each eigenvector to a unit vector.)
Step 5 — Project the data
Why Does It Work?#
The covariance matrix captures the direction and magnitude of the data's spread. Viewed as a linear transformation, the eigenvectors are the directions where the transformation only scales without rotating, and the eigenvalues tell us how much it scales.
Projecting onto the direction of the largest eigenvector → maximum variance (information) is preserved.
In practice: scikit-learn's
PCA(n_components=2)performs this entire process automatically. It's widely used for facial recognition, noise reduction, and data visualization.
from sklearn.decomposition import PCA
import numpy as np
X = np.array([[2.5, 2.4], [0.5, 0.7], [2.2, 2.9],
[1.9, 2.2], [3.1, 3.0], [2.3, 2.7]])
pca = PCA(n_components=1)
X_pca = pca.fit_transform(X)
print("Eigenvalues:", pca.explained_variance_)
print("Eigenvectors:", pca.components_)
print("Transformed data:", X_pca)
Markov Matrices and Discrete Dynamical Systems#
One practical application of eigenvectors is Markov matrices.
Markov Matrix#
- All entries are non-negative
- Each column sums to 1 — represents state transition probabilities
(Example: sunny day → stays sunny with 90% probability, sunny day → becomes cloudy with 10% probability)
Updating the State Vector#
From the current state , the next state is:
Repeated application causes the state vector to converge (reach equilibrium).
Equilibrium vector = eigenvector corresponding to eigenvalue 1
Markov matrices always have eigenvalue 1, so the long-run steady state can be found as an eigenvector.
In practice: Google's PageRank algorithm uses a Markov matrix to rank web pages.
Full Summary#
| Concept | Description |
|---|---|
| Determinant = Area | How much a linear transformation scales the unit square |
| Determinant of a product = product of determinants | |
| Determinant of an inverse = reciprocal | |
| Basis | Minimal set of linearly independent vectors that span the space |
| Span | Set of all points reachable by linear combinations of vectors |
| Eigenvector | A vector whose direction is unchanged by the transformation |
| Eigenvalue | The scaling factor applied to an eigenvector |
| Characteristic polynomial | Solve to find eigenvalues |
| Covariance matrix | Symmetric matrix encoding variance and covariance between variables |
| PCA | Dimensionality reduction that maximally preserves information using eigenvectors of the covariance matrix |
| Markov matrix | State transition probability matrix; equilibrium = eigenvector |
Quiz#
Q1. Given and , find and .
Show Answer
Since , this transformation reverses orientation.
Q2. Can the set form a basis for ?
Show Answer
Since , the two vectors are linearly dependent.
Vectors pointing in the same direction only span a line, so they cannot form a basis for .
Q3. Find the eigenvalues of the following matrix.
Show Answer
Eigenvalues:
(For a triangular matrix, the eigenvalues are simply the diagonal entries.)
Q4. Why do we choose the eigenvector corresponding to the largest eigenvalue in PCA?
Show Answer
The eigenvalue indicates how much the data spreads (variance) in that direction.
The largest eigenvalue = the direction of maximum variance = the direction that preserves the most information.
Therefore, to minimize information loss when reducing dimensions, we should choose the eigenvector direction with the largest eigenvalue.
Q5. The equilibrium vector of a Markov matrix is the eigenvector of what?
Show Answer
It is the eigenvector corresponding to eigenvalue 1 of the Markov matrix.
In other words, the equilibrium vector is a fixed point — a state vector that no longer changes no matter how many times you apply the update rule — and this is precisely the eigenvector for eigenvalue 1.
This concludes the Linear Algebra Basics series. Here's a recap of the journey from Chapter 1 to Chapter 4:
// Related Posts
Probability & Statistics in Practice: Inference Problems from the ML Trenches
From probability fundamentals and Bayes' theorem to distributions, MLE/MAP, confidence intervals, and hypothesis testing — a collection of practice problems grounded in real ML and data analysis scenarios.
Probability & Statistics Coding Assignments: Building ML Statistical Tools in Python
Bayesian updates, distribution simulation, CLT verification, MLE/MAP implementation, confidence intervals, hypothesis testing, and a full A/B test pipeline — implementing probability & statistics chapters 1–4 in code.
ML Probability & Statistics Chapter 4: Confidence Intervals and Hypothesis Testing
A complete guide to confidence intervals, the t-distribution, hypothesis testing fundamentals (null/alternative hypotheses, p-values, rejection regions, statistical power), various t-tests, and A/B testing.