Lesson 3 of 7 · Math for AI: Linear Algebra & Calculus Essentials

Eigenvectors, SVD & Low-Rank Structure: PCA and LoRA

A matrix can look like a wall of numbers, but most of the useful ones are built from just a few directions. Finding those directions is how you shrink embeddings, find the main axis of a dataset, and fine-tune a huge model by training a tiny fraction of it.

M1 gave you vectors and dot products. M2 gave you matrices as machines that take a vector in and push a new vector out. This lesson asks a sharper question about those machines: what is a matrix really doing, and how much of it do you actually need? The answer comes in three pieces: eigenvectors (the directions a matrix only stretches), the singular value decomposition (a way to take any matrix apart into rotate, stretch, rotate), and low-rank approximation (keep the biggest stretches, drop the rest). Two techniques you will meet constantly in AI work, PCA and LoRA, are direct applications.

1. Eigenvectors and eigenvalues: the directions a matrix only stretches

Feed most vectors into a matrix and they come out pointing somewhere new. Take the 2×2 matrix below, which you can picture as a tiny layer that mixes two features:

A = [ 2 1 ]
[ 1 2 ]
A × (1, 0) = (2×1 + 1×0, 1×1 + 2×0) = (2, 1) ← turned toward the y-axis
A × (0, 1) = (1, 2) ← turned toward the x-axis

Both inputs got rotated as well as stretched. But a few special directions do not turn at all. Try (1, 1) and (1, −1):

A × (1, 1) = (2×1 + 1×1, 1×1 + 2×1) = (3, 3) = 3 × (1, 1)
A × (1, −1) = (2×1 − 1×1, 1×1 − 2×1) = (1, −1) = 1 × (1, −1)

The matrix sent (1, 1) to three times itself and left (1, −1) exactly as it was. Those directions are the eigenvectors of A, and the stretch factors, 3 and 1, are the matching eigenvalues (written λ, the Greek letter lambda). In symbols: Av = λv. An eigenvector is a direction the matrix does nothing to except scale. A negative eigenvalue flips the direction; an eigenvalue of 0 flattens it away entirely.

Finding them by hand

Rearranging Av = λv gives (A − λI)v = 0, where I is the identity matrix (ones on the diagonal, zeros elsewhere, so Iv = v). A nonzero v can only satisfy that if the matrix A − λI squashes space flat, which happens exactly when its determinant is 0. For a 2×2 matrix [a b; c d], the determinant is ad − bc.

det(A − λI) = (2 − λ)(2 − λ) − (1)(1)
= λ² − 4λ + 3
= (λ − 1)(λ − 3)
⇒ λ = 3 or λ = 1
λ = 3: (A − 3I)v = [−1 1; 1 −1]v = 0 ⇒ v₁ = v₂ ⇒ v = (1, 1)
λ = 1: (A − 1I)v = [ 1 1; 1 1]v = 0 ⇒ v₁ = −v₂ ⇒ v = (1, −1)
Check: sum of eigenvalues 3 + 1 = 4 = trace (2 + 2); product 3 × 1 = 3 = det (4 − 1)

A short Python check (numpy’s np.linalg.eig) returns eigenvalues 3 and 1 with eigenvectors proportional to (1, 1) and (1, −1), matching the hand result. Any multiple of an eigenvector is also an eigenvector, so libraries return them scaled to length 1: (0.707, 0.707) and (−0.707, 0.707).

Key Insight Along its eigenvectors, a matrix is just multiplication by a number. A tangled 2×2 mixing becomes two independent, one-dimensional stretches: one by 3, one by 1. That simplification is why eigenvectors keep showing up: they are the coordinate axes in which the matrix is easy to understand.

2. Rank: how many independent directions a matrix really has

The rank of a matrix is the number of independent directions in its output, meaning how many of its columns carry information that the others cannot reproduce. A 3×2 matrix could in principle have rank 2, but it can have less. The smallest case is rank 1, built from an outer product: a column vector times a row vector.

u = (1, 2, 3) v = (4, 5)
u vᵗ = [ 1×4 1×5 ] [ 4 5 ]
[ 2×4 2×5 ] = [ 8 10 ]
[ 3×4 3×5 ] [ 12 15 ]

Look at the columns: (4, 8, 12) and (5, 10, 15). Each is just a multiple of u = (1, 2, 3), so the whole 6-number table is a single direction scaled two ways. Rank 1. Python’s np.linalg.matrix_rank confirms it. Storing the table takes 6 numbers; storing u and v takes 3 + 2 = 5, and the gap widens fast for bigger matrices: a 1000×1000 rank-1 matrix is 1,000,000 numbers but only 2,000 as an outer product.

Real matrices in AI are almost never exactly low-rank, but many are close to it: their information is dominated by a few directions, with the rest being small corrections. Measuring “how close” is what the next section is for.

3. The singular value decomposition: rotate, stretch, rotate

Eigenvectors only work cleanly for certain square matrices. The singular value decomposition (SVD) works for any matrix, of any shape, and it is the workhorse of this lesson. It says every matrix A can be written as:

A = U Σ Vᵗ
Vᵗ rotates (or reflects) the input into a convenient set of axes
Σ stretches each axis by a non-negative number σ₁ ≥ σ₂ ≥ ... (the singular values)
U rotates (or reflects) the stretched result into the output space

Read it right to left, the order a vector would travel through it: rotate, stretch, rotate. The superscript T means transpose, flipping a matrix across its diagonal. The columns of V are the “input directions,” the columns of U are the “output directions,” and the singular values σ (sigma) on the diagonal of Σ say how strongly each input direction is amplified. A big singular value marks an important direction. A tiny one marks a direction the matrix nearly ignores.

A hand-computed example

Take B = [3 0; 4 5]. The singular values are the square roots of the eigenvalues of BTB (a trick worth remembering: BTB is always symmetric, so its eigenvalues are real, and they are never negative because xTBTBx = ‖Bx‖² ≥ 0).

B = [ 3 0 ] BᵗB = [ 3×3+4×4 3×0+4×5 ] = [ 25 20 ]
[ 4 5 ] [ 0×3+5×4 0×0+5×5 ] [ 20 25 ]
det(BᵗB − λI) = (25 − λ)² − 20² = 0 ⇒ 25 − λ = ±20 ⇒ λ = 45 or 5
σ₁ = √45 ≈ 6.708 σ₂ = √5 ≈ 2.236
Input directions (eigenvectors of BᵗB): v₁ = (1, 1)/√2, v₂ = (1, −1)/√2
Output directions uᵢ = B vᵢ / σᵢ:
u₁ = B(1,1)/√2 / √45 = (3, 9)/√90 = (1, 3)/√10
u₂ = B(1,−1)/√2 / √5 = (3, −1)/√10

numpy’s np.linalg.svd returns the same singular values, 6.708 and 2.236, and the same directions up to an overall sign. One consistency check you can always run: the sum of the squared entries of the matrix equals the sum of the squared singular values. Here 3² + 0² + 4² + 5² = 50, and 45 + 5 = 50.

4. Low-rank approximation: keep the biggest stretches

The SVD can also be written as a sum of rank-1 pieces, each one a singular value times an outer product (the same kind of u vT object from section 2):

A = σ₁ u₁ v₁ᵗ + σ₂ u₂ v₂ᵗ + σ₃ u₃ v₃ᵗ + ... (pieces ordered biggest to smallest)

Keep only the first k pieces and you have the rank-k approximation. The Eckart–Young theorem (stated here, not proved) says this truncation is the best possible: no other rank-k matrix is closer to A. And the error is exactly what you threw away: the squared distance between A and its rank-k approximation equals the sum of the squared discarded singular values.

Apply it to B. Keep only the first piece, σ₁ u₁ v₁T:

σ₁ u₁ v₁ᵗ = √45 · (1,3)/√10 · (1,1)/√2 = (√45 / √20) · (1,3)ᵗ(1,1)
= 1.5 × [ 1 1 ] = [ 1.5 1.5 ]
[ 3 3 ] [ 4.5 4.5 ]
B − (rank-1) = [ 3−1.5 0−1.5 ] = [ 1.5 −1.5 ]
[ 4−4.5 5−4.5 ] [ −0.5 0.5 ]
Squared error = 1.5² + 1.5² + 0.5² + 0.5² = 2.25 + 2.25 + 0.25 + 0.25 = 5 = σ₂²
Error size = √5 ≈ 2.236 = σ₂, and the rank-1 piece holds 45 / 50 = 90% of the matrix’s squared size

The Frobenius norm check in numpy gives 2.2361 for the error, matching √5. Now a bigger, more typical case. Suppose a team measures four singular values for a feature matrix and gets 9, 4, 2, 1. Their squares are 81, 16, 4, 1, totalling 102.

Keep top kSquared size keptShare keptError (√ of discarded squares)
181 / 10279.4%√21 ≈ 4.58
297 / 10295.1%√5 ≈ 2.24
3101 / 10299.0%√1 = 1.00
4102 / 102100%0

Two directions out of four recover 95% of the matrix. That is the whole bet behind low-rank methods: if the singular values fall off fast, a few directions carry nearly everything, and the rest is cheap to drop.

Pitfall Low rank is a property you check, not one you assume. If the singular values of a matrix decay slowly (many of them about the same size), truncating costs real accuracy. Always look at the singular values, or the share of squared size kept, before deciding how many directions to keep.

5. PCA: the eigenvectors of the covariance matrix

Principal component analysis (PCA) answers: in which direction does a cloud of data vary the most? The recipe is to subtract the mean from every feature (centering), build the covariance matrix (a table of how each pair of features varies together, covered in the statistics course, Lesson 14, “Joint Distributions”, and previewed through correlation in Lesson 6), and take its eigenvectors. The eigenvector with the biggest eigenvalue is the first principal component: the main axis of the cloud. Its eigenvalue is the variance of the data measured along that axis.

A worked example with five points in 2-D. Imagine a team plotting two features of five short documents, say a toy embedding trimmed to two dimensions:

points (x, y): (1,2) (2,1) (3,4) (4,3) (5,5)
mean: x̄ = (1+2+3+4+5)/5 = 3 ȳ = (2+1+4+3+5)/5 = 3
centered: (−2,−1) (−1,−2) (0,1) (1,0) (2,2)
var(x) = (4+1+0+1+4)/4 = 10/4 = 2.5 (divide by n − 1 = 4)
var(y) = (1+4+1+0+4)/4 = 10/4 = 2.5
cov(x,y) = (2+2+0+0+4)/4 = 8/4 = 2.0
C = [ 2.5 2.0 ]
[ 2.0 2.5 ]

This covariance matrix is A from section 1 in disguise: same shape, with 2.5 and 2.0 in place of 2 and 1. The same method gives its eigenvalues:

det(C − λI) = (2.5 − λ)² − 2.0² = 0 ⇒ 2.5 − λ = ±2 ⇒ λ = 4.5 or 0.5
λ = 4.5: v = (1, 1)/√2 ← main axis, pointing up and to the right
λ = 0.5: v = (1, −1)/√2 ← the leftover, perpendicular direction
Share of variance along the main axis = 4.5 / (4.5 + 0.5) = 90%

To check the 4.5, project each centered point onto (1, 1)/√2 by taking a dot product (M1): (dx + dy)/√2 gives −2.12, −2.12, 0.71, 0.71, 2.83. Square them (4.5, 4.5, 0.5, 0.5, 8.0, total 18) and divide by 4: variance 4.5. Same answer from two routes. The picture:

feature x feature y mean (3, 3) (1,2) (2,1) (3,4) (4,3) (5,5) Principal axis (1,1)/√2 eigenvalue 4.5 = 90% of variance Second axis (1,−1)/√2 eigenvalue 0.5 = 10% of variance Drop to the main axis what PCA keeps if told “use 1 number”
Five centered-around-(3,3) points. Collapsing each onto the green axis keeps 90% of the spread and turns two numbers per point into one.

That collapse is dimensionality reduction: replace each point’s two coordinates with a single coordinate along the main axis, and lose only the 10% of variance along the second axis. In real work the same recipe turns 768 numbers per embedding into, say, 50, so you can plot, cluster, or store them more cheaply. PCA and SVD are the same computation seen two ways: the principal directions of centered data are the right singular vectors (V) of the data matrix, and the covariance eigenvalues are the squared singular values divided by n − 1.

Pitfall Forgetting to center. Run the eigen-analysis on uncentered data and the “main axis” often just points at the mean, telling you where the data sits instead of how it spreads. Also, PCA ranks directions by variance, not by usefulness: a high-variance direction can be pure noise, and a low-variance one can carry the label you care about.

6. LoRA: a low-rank update instead of a full one

Fine-tuning a language model normally means changing every weight. A layer with a square weight matrix W of size d×d has d² trainable numbers, and a model has dozens of such matrices. LoRA (low-rank adaptation; see the paper in the sources) freezes W and learns only a small correction on top of it, built as the product of two thin matrices:

W′ = W + B A W: d × d (frozen) B: d × r (trained) A: r × d (trained)
output = W x + B (A x) r is the chosen rank, much smaller than d

The product B A is a d×d matrix, but its rank is at most r, because it is a sum of at most r outer products. The bet is the same one as in section 4: the change a model needs for a new task is concentrated in a few directions, so a rank-r update captures most of it. Count the savings for d = 4096 and r = 8:

Full update: d × d = 4096 × 4096 = 16,777,216 numbers
LoRA: B = d × r = 4096 × 8 = 32,768
A = r × d = 8 × 4096 = 32,768
total 2 × d × r = 2 × 4096 × 8 = 65,536 numbers
Ratio: 16,777,216 / 65,536 = 256 Share trained: 65,536 / 16,777,216 = 0.39%
256 times fewer trainable numbers for this matrix

If a team adapted two such matrices in each of 32 layers (an illustrative setup), the trainable count would be 65,536 × 2 × 32 = 4,194,304, against 16,777,216 × 2 × 32 = 1,073,741,824 for full updates. Fewer trainable numbers also means less memory for the optimizer’s bookkeeping and a small file to save and swap per task. The cost: if the true needed change is not low-rank, a small r underfits. That is why r is a setting you tune, usually by comparing a few values on held-out data.

7. Compressing embeddings

The same idea compresses stored tables of vectors. Suppose a team has an embedding table with 50,000 tokens, each a vector of 768 numbers: a 50,000×768 matrix. Replace it by a product of a 50,000×64 matrix and a 64×768 matrix (rank 64):

Original: 50,000 × 768 = 38,400,000 numbers
Rank 64: 50,000 × 64 + 64 × 768 = 3,200,000 + 49,152 = 3,249,152 numbers
38,400,000 / 3,249,152 ≈ 11.8 times smaller

Whether this is safe is the question from section 4: check the share of squared singular values you keep at rank 64. If it is 99%, you lose little; if it is 70%, you have blurred real distinctions between tokens. The same trick shrinks the final projection layer of a model, and PCA from section 5 does it to individual embedding vectors before they go into a search index.

8. Why this matters for building AI systems

Why This Matters Models are huge, and most of what they store is more structured than its size suggests. Low-rank thinking is how you exploit that structure.

Cheaper fine-tuning

LoRA cuts the number of trainable parameters by orders of magnitude, which is why adapting a large model needs far less memory than training all of it. Knowing why it works (rank, outer products, truncated SVD) tells you what r trades off and when it will fall short.

Cheaper storage and search

Compressed embeddings mean smaller vector databases and faster similarity search. The singular value table is your measuring stick for how much accuracy that costs.

Seeing inside your data

PCA on embeddings shows whether a dataset really has a few dominant themes or is spread in every direction, and the share of variance per component tells you which. The eigenvalue mathematics here is also what you reach for when studying why training is stable or unstable.

Recap

The linear algebra half of the course ends here. Leads into M4: derivatives, the tool that tells you which way to nudge those matrices to make a model better.

✅ Check Yourself

1. A matrix A sends the vector (1, 1) to (3, 3) and the vector (1, −1) to (1, −1). What does that tell you?

Both vectors are rotated by 45 degrees
A has rank 3
Both are eigenvectors of A, with eigenvalues 3 and 1

2. A 3×2 matrix is the outer product of u = (1, 2, 3) and v = (4, 5). What is its rank?

1, because every column is a multiple of u
2, because it has two columns
3, because it has three rows

3. A feature matrix has singular values 9, 4, 2, 1. Roughly what share of its squared size does the best rank-2 approximation keep?

About 79%
About 95%
About 50%, since 2 of 4 values are kept

4. In PCA on centered data, the first principal component is:

The data point farthest from the mean
The eigenvector of the covariance matrix with the smallest eigenvalue
The eigenvector of the covariance matrix with the largest eigenvalue

5. LoRA adapts a 4096×4096 weight matrix with rank r = 8. How many numbers does it train for that matrix?

65,536 (two matrices of 4096×8 each)
16,777,216 (the full 4096×4096)
32,768 (a single 4096×8 matrix)