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.
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:
Both inputs got rotated as well as stretched. But a few special directions do not turn at all. Try (1, 1) and (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.
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.
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).
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.
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.
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:
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.
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).
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.
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):
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:
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 k | Squared size kept | Share kept | Error (√ of discarded squares) |
|---|---|---|---|
| 1 | 81 / 102 | 79.4% | √21 ≈ 4.58 |
| 2 | 97 / 102 | 95.1% | √5 ≈ 2.24 |
| 3 | 101 / 102 | 99.0% | √1 = 1.00 |
| 4 | 102 / 102 | 100% | 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.
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:
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:
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:
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.
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:
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:
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.
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):
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.
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.
Compressed embeddings mean smaller vector databases and faster similarity search. The singular value table is your measuring stick for how much accuracy that costs.
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.
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.
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?
2. A 3×2 matrix is the outer product of u = (1, 2, 3) and v = (4, 5). What is its rank?
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?
4. In PCA on centered data, the first principal component is:
5. LoRA adapts a 4096×4096 weight matrix with rank r = 8. How many numbers does it train for that matrix?