Eigenvalues & SVD
After this lesson, you will be able to:
- Identify eigenvectors as the special directions that a matrix only stretches (never rotates), and eigenvalues as how much it stretches them
- Break any matrix apart into a simple sequence of rotate-stretch-rotate steps (called SVD) and explain why every matrix can be broken down this way
- See how these ideas power real applications: compressing data, recommending songs, and customizing AI models on a laptop
- Use truncated SVD to approximate a matrix with far fewer numbers, and calculate the compression ratio for a realistic example
Before you start
Active recall
Before we start: without looking back, write one sentence describing what a matrix DOES to a vector geometrically. (Hint: it's some combination of two simple operations.) The point isn't to be exactly right — it's to commit to an answer before you read the next paragraph.
Write your answer in your own words — don't look back at the lesson. This is the most effective way to remember what you just learned.
Type your explanation above
#The Hinge on the Door
This is one of the most beautiful ideas in all of math. It sounds intimidating, but the core concept is simple: every transformation has a few "special directions" that reveal its true nature. Once you see it, you will never look at data the same way again.
One honest caveat: some matrices have no such direction at all. A pure rotation, such as turning every vector by 90 degrees, has no real eigenvector ("real" here just means ordinary numbers), because every direction gets turned. The Going Further section at the end says what happens then.
#A First Test: Which Directions Do Not Turn
Before any formula, try one matrix on two vectors. Take $A = \begin 3 & 1 \ 0 & 2 \end$. To multiply it by a vector $[x, y]$, take row one times the vector for the top answer and row two times the vector for the bottom answer.
First test vector $[1, 0]$:
The answer points the same way as the input, just 3 times as long. Same direction, stretched by 3: this is an eigenvector, and 3 is its eigenvalue.
Second test vector $[0, 1]$:
The input pointed straight up; the answer leans to the right. The matrix turned it, so $[0, 1]$ is not an eigenvector.
Now try it yourself. Spin the test vector around and watch where the output lands. Look for the directions where the output stays on the same line as the input.
Those directions that do not turn are the eigenvectors, and the stretch factor along each one is its eigenvalue. The next section writes that idea as a single equation.
#The Core Equation
Read it as: "A times v lands on the same line as v, only $\lambda$ times as long." In the test above, $\vec = [1, 0]$ and $\lambda = 3$.
Try it! Open the Python REPL (bottom-right of the screen: click Quick Actions, then Python) and type these lines yourself.
Where:
- A is the matrix (the transformation)
- v is an eigenvector (a direction that survives the transformation unchanged)
- lambda is the eigenvalue (the stretch factor along that direction)
If the 2x2 matrix [[3, 0], [0, 0.5]] has eigenvalues 3 and 0.5, what happens to a circle of points when you apply the matrix?
#Interactive: See Eigenvectors Hold Steady
Watch how an eigenvector keeps its direction while another vector turns. Apply the matrix and compare the two arrows:
The eigenvector's arrow gets longer or shorter but stays on its own line. The other arrow lands on a different line, which means it was turned.
#What Eigenvalues Tell You
| Eigenvalue | Effect | Interpretation |
|---|---|---|
| lambda > 1 | Amplifies this direction | Data spreads out along this axis |
| 0 < lambda < 1 | Shrinks this direction | Data compresses along this axis |
| lambda = 0 | Collapses this direction | Dimension eliminated — information lost |
| lambda < 0 | Flips and scales | Direction reverses (reflected) |
Every row is a stretch, a shrink, or a flip along a line, never a turn. A matrix that turns every direction, like a pure rotation, has no real eigenvalue at all.
#Finding Eigenvalues
Guessing directions worked for $A = \begin 3 & 1 \ 0 & 2 \end$ because the numbers were friendly. Here is a method that always works for a 2x2 matrix. We take the same A and go one line at a time.
#Worked Example: Finding Eigenvalues by Hand
Verify: $A\vec_1 = \begin 3 \ 0 \end = 3\begin 1 \ 0 \end$ ✓ and $A\vec_2 = \begin 3\cdot 1 + 1\cdot(-1) \ 0\cdot 1 + 2\cdot(-1) \end = \begin 2 \ -2 \end = 2\begin 1 \ -1 \end$ ✓
Any multiple of an eigenvector is also an eigenvector, so $[2, 0]$ works as well as $[1, 0]$. Only the direction matters. The first test in this lesson already found $[1, 0]$ by trial; the method just found it, and the second direction, by algebra.
For a general 2x2 matrix with rows [a, b] and [c, d], multiplying out that determinant gives the quadratic $\lambda^2 - (a + d)\lambda + (ad - bc)$. The number $a + d$ is the trace (the sum of the diagonal) and $ad - bc$ is the determinant. So for every 2x2 matrix, the eigenvalues add up to the trace and multiply to the determinant.
The three boxes below are optional reading. They show why the equation is the right one, where the quadratic comes from, and why the sum and product work. Open one whenever you wonder where a formula came from.
For the diagonal matrix A = [[2, 0], [0, 5]] (zeros off the diagonal), without computing anything, what are its eigenvalues and eigenvectors?
Here is the football in numbers, small enough for a pencil. Three people have two measurements, x = [1, 2, 3] and y = [2, 4, 9]. The means are 2 and 5, so the deviations are [-1, 0, 1] and [-3, -1, 4]. Averaging the squares and the cross products (dividing by 3; numpy's np.cov divides by 2 unless you pass bias=True) gives a table called the covariance matrix: [[0.67, 2.33], [2.33, 8.67]]. The diagonal is each measurement's own spread, and the corner is how they move together.
Now ask that table the PCA question. Its diagonal adds to 9.33 and its determinant is 0.67 × 8.67 − 2.33 × 2.33 = 0.33. The eigenvalues solve λ² − 9.33λ + 0.33 = 0, so they are about 9.30 and 0.04. The big one holds 9.30 / 9.33 = 99.6% of the total spread. Its eigenvector, (0.26, 0.97), is the football's long axis, steep and nearly along y. Sliding the three people onto that axis gives positions −3.16, −0.97 and 4.12. Dropping the other direction loses only 0.4% of the spread.
#SVD: The Universal Decomposition
Its claim is that every matrix does three simple things in a row: turn, stretch, turn. Switch the three steps on one at a time and watch the circle turn, become an ellipse, then turn again:
Written as a formula, the three steps are:
Where:
- V^T ("V transpose", which means V with its rows and columns swapped, so the first column becomes the first row) turns the input into the "natural" directions of the transformation
- Sigma (capital Greek letter sigma) stretches along each axis by the singular values (never negative, sorted largest to smallest)
- U turns the result into the output directions
U and V are pure turns (possibly with a flip). A matrix like that is called orthogonal: it turns things without changing any length. Only Sigma changes lengths.
The first singular value captures the most important stretching; the last captures the least. If the last few singular values are near zero, those dimensions barely matter, and you can safely throw them away.
Where does this come from? The matrix $A^T A$ is square and symmetric, so it has perpendicular eigenvectors. Those become the columns of V, the square roots of its eigenvalues become the singular values, and A times each eigenvector, rescaled, gives the columns of U. The two optional boxes below build that out line by line.
Try it on A = [[3, 0], [4, 5]]. Here A transpose A = [[25, 20], [20, 25]], which has eigenvalues 45 and 5 with eigenvectors (1, 1)/√2 and (1, -1)/√2. The singular values are √45 = 6.708 and √5 = 2.236.
Push each input direction through A. A times (1, 1)/√2 is (3, 9)/√2, which has length √45 = 6.708, so u1 = (1, 3)/√10. A times (1, -1)/√2 is (3, -1)/√2, which has length √5 = 2.236, so u2 = (3, -1)/√10. As a check, 6.708 u1 v1^T equals 1.5 × [[1, 1], [3, 3]], and 2.236 u2 v2^T equals 0.5 × [[3, -3], [-1, 1]]. They add to [[3, 0], [4, 5]].
For an m by n matrix, U is m by m, Sigma is m by n and V^T is n by n. NumPy returns U, a flat array s of singular values, and Vt, which is V already transposed.
Replay the recipe step by step on a 2x2 matrix: form A^T A, take its eigenvectors, square-root the eigenvalues, then divide A v by sigma. Pick the Rank 1 preset to see what happens when sigma 2 is zero.
#Truncated SVD: Practical Compression
Here k is how many singular values you keep. Use the low-rank toggle in the next lab to keep fewer singular values and watch the point cloud change.
Build one layer by hand. Multiply the column (1, 2, 3) by the row (4, 5, 6): the grid is [[4, 5, 6], [8, 10, 12], [12, 15, 18]]. Six numbers filled nine cells, and each row is a multiple of (4, 5, 6). Add a second layer, the column (1, 0, -1) times the row (1, 1, 1), which is [[1, 1, 1], [0, 0, 0], [-1, -1, -1]]. The sum is [[5, 6, 7], [8, 10, 12], [11, 14, 17]]. One layer has rank 1, and the sum of two independent layers has rank 2.
How good is that approximation? The Eckart-Young theorem says nobody can do better: among all matrices of rank at most k, the truncated SVD is the closest one to A, where distance is measured by the Frobenius norm (the square root of the sum of the squared entries of the difference). The error is exactly the singular values you threw away, which the next box shows. The theorem's optimality proof is long, so it is stated here and proved in the references.
Move the slider to change k, the number of layers kept, switch between the structured picture and random noise, and tick show one layer to see a single layer on its own, then watch that the top k layers always make the best possible rank-k picture, that the squared error equals the sum of the dropped sigma squared, and that the structured picture comes back from a few layers while noise needs almost all of them.
You perform SVD on a 1000x500 matrix and find that the first 10 singular values are large while the remaining 490 are near zero. What does this tell you?
The data effectively lives in a 10-dimensional subspace, even though it is nominally 500-dimensional. The 490 near-zero singular values correspond to directions with almost no variation, so you can approximate the matrix well using only the top 10.
Here is how the saving is counted. Keeping k = 10 means storing $U_k$ (1000 x 10 = 10,000 numbers), the 10 singular values, and $V_k^T$ (10 x 500 = 5,000 numbers). The total is 15,010 numbers instead of 1000 x 500 = 500,000, about 33 times smaller (500,000 / 15,010 = 33.3) with minimal information loss. In general the count is k x (rows + columns + 1).
#Seeing It Work: Rebuilding an Image from k = 1, 2, 5
The cell below builds a small synthetic 30 x 30 image (a bright square plus a left-to-right brightness ramp, with a little seeded noise), then rebuilds it from only k singular values and prints how far each rebuild is from the original.
The real output: the first six singular values are 20.05, 4.32, 0.52, 0.46, 0.43 and 0.38. Two values stand far above the rest, because the image is mostly a square plus a ramp, and everything else is noise. With k = 1 you store 61 numbers instead of 900 and the relative error is 0.220. With k = 2 you store 122 numbers and the error drops to 0.065. With k = 5 you store 305 numbers and the error only falls to 0.052, because what is left is the noise. Past k = 2, more singular values buy almost nothing.
peft and pass r=8, the library does not compute an SVD. It learns two small matrices B and A directly, so the weight update B A has rank at most 8. The "r" plays the role of the number of singular values you keep.#Why SVD Matters for ML
#Try It Yourself
You have checked eigenvectors by guessing and by algebra. Now do the algebra yourself on a new matrix, [[4, 2], [1, 3]], and let Python check each step. For a 2x2 matrix, the quadratic from the characteristic equation always has the form $\lambda^2 - (\text)\lambda + \det$, where the trace is the sum of the diagonal.
Tests: Verify the polynomial is lambda^2 - 7 lambda + 10, the eigenvalues are 5 and 2, the eigenvectors are [2, 1] and [2, -2] with A @ v equal to lam * v, the eigenvalues sum to the trace 7 and multiply to the determinant 10, and B = [[2, 0], [0, 3]] has eigenvalues 3 and 2.
The real output: the polynomial is $\lambda^2 - 7\lambda + 10$, so the eigenvalues are 5 and 2. For $\lambda = 5$ the eigenvector is $[2, 1]$ and $A\vec = [10, 5] = 5\cdot[2, 1]$. For $\lambda = 2$ the eigenvector is $[2, -2]$ and $A\vec = [4, -4] = 2\cdot[2, -2]$. The eigenvalues add to 7, which is the trace, and multiply to 10, which is the determinant. These two relationships hold for every square matrix. For the diagonal matrix B the eigenvalues are just the diagonal entries, 3 and 2.
Matrix Computations
Gene Golub, Charles Van Loan (2013)
The definitive reference on numerical linear algebra including SVD algorithms. Chapter 2 covers eigenvalue problems; Chapter 8 covers SVD in depth. The algorithms in this book run inside every call to numpy.linalg.
⚡ Playground: Eigen Decomposition → — decompose a matrix and watch the singular vectors stretch the space.
#Run It Yourself: Eigen & SVD on a Real 3×3 Matrix
numpy.linalg.eig and numpy.linalg.svd in your browser. Try changing the matrix to the non-symmetric one in the comments: its eigenvalues come out as complex numbers (see Going Further), but its singular values are still real and never negative.Active recall
Close this lesson tab in your head for 30 seconds and write: (a) what the equation Av = λv MEANS in one English sentence, (b) why a 2D rotation matrix has no real eigenvectors, and (c) what 'r' represents when you load a LoRA adapter with r=8. If any of the three is fuzzy, that is the part that has not stuck yet — note it and come back to it tomorrow. Retrieval beats re-reading.
Write your answer in your own words — don't look back at the lesson. This is the most effective way to remember what you just learned.
Type your explanation above
#Key Takeaways
- Eigenvectors are directions that survive unchanged. When a matrix is applied, eigenvectors only get stretched (not rotated), and the eigenvalue tells you the stretch factor along that direction
- Eigenvalues reveal a transformation's DNA. Large eigenvalues indicate amplified directions, small ones indicate compressed directions, and zero eigenvalues mean that dimension is destroyed entirely
- SVD decomposes any matrix into rotate-stretch-rotate. Unlike eigendecomposition, SVD works on any matrix (including non-square ones), making it the universal tool for understanding matrix transformations
- PCA is eigendecomposition of the covariance matrix (a table of how pairs of features vary together, covered properly later in the track). The eigenvectors with the largest eigenvalues capture the directions of maximum variance, enabling dimensionality reduction that preserves the most information
- LoRA exploits low-rank structure. Weight updates during fine-tuning live in a low-dimensional subspace, so a low-rank approximation (inspired by SVD) captures the essential update with far fewer parameters
#Quick Check
What does it mean if a matrix has an eigenvalue of zero?
#Going Further (Optional)
Everything in this section is optional. The main path ends above: eigenvectors, eigenvalues, finding them for a 2x2 matrix, and SVD as rotate, stretch, rotate. Come back here when those feel comfortable.