Eigenvalues and Eigenvectors
The mathematical foundation of PCA, spectral clustering, and PageRank — built from plain English first, then intuition, then math, then code.
Imagine spinning a blob of data. Most directions stretch and rotate. A few special directions only stretch — never rotate. Those are eigenvectors.
You have a dataset of 10,000 DoorDash customers. Each customer is described by 20 numbers — order frequency, average order value, preferred cuisine, delivery distance preference, time of day, and so on. That dataset is a cloud of 10,000 points in 20-dimensional space.
Now imagine squashing that cloud into 2 dimensions so you can plot it. You need to pick which 2 directions to keep out of the original 20. If you pick randomly, you lose most of the interesting structure. But there exist specific directions in the data — the directions along which the cloud is most spread out — that capture the most information. Finding those directions is exactly what eigenvalues and eigenvectors do.
Those special directions are the eigenvectors. How much the data spreads along each direction is the eigenvalue. The eigenvector with the largest eigenvalue is the single most informative direction in your entire dataset.
Think of a shadow. You hold an odd-shaped object under a light. As you rotate the object, its shadow changes shape — sometimes long and thin, sometimes short and wide. There is one specific angle where the shadow is longest. That angle is like an eigenvector — the direction that reveals the most about the object's structure. The length of that longest shadow is like the eigenvalue.
PCA (Principal Component Analysis) uses exactly this idea. It finds the directions in your data where the shadow is longest — where the data varies the most — and uses those as the new axes. Module 33 covers PCA in full. This module gives you the mathematical foundation to understand how it works.
A matrix is a transformation — it stretches, rotates, and reflects vectors
From Module 04 you know that matrix multiplication transforms vectors. When you multiply a matrix by a vector, the result is a new vector — usually pointing in a different direction and having a different length. The matrix is performing a geometric operation on the vector.
Most vectors get both rotated AND stretched when a matrix is applied to them. The direction changes and the length changes. But some special vectors only get stretched — they keep pointing in exactly the same direction. Those special vectors are eigenvectors.
A vector e is an eigenvector of matrix A if multiplying A by e gives back e scaled by a number λ:
A concrete example — verify the definition yourself
The best way to understand eigenvectors is to verify the definition by hand on a small example. We will use a 2×2 matrix so everything is visible. Once you see it work once, the concept locks in permanently.
Eigenvalues tell you how important each eigenvector is
Every square matrix has as many eigenvalue-eigenvector pairs as it has dimensions. A 20×20 matrix has 20 pairs. Each pair is a direction (eigenvector) and a number (eigenvalue). The eigenvalue tells you how much the transformation stretches the data in that direction.
In the context of data analysis: if you compute the covariance matrix of your dataset (a matrix that describes how features vary together), its eigenvectors point in the directions of maximum variance in the data. The eigenvalues tell you how much variance each direction captures.
Explained variance — turning eigenvalues into percentages
The most useful thing you can do with eigenvalues in ML is convert them to "explained variance" — what percentage of the total information does each direction capture? This tells you how many dimensions to keep when reducing the dimensionality of your data.
PCA from scratch using eigenvalues — the complete picture
PCA (Principal Component Analysis) is the most widely used dimensionality reduction technique in ML. It reduces a dataset from many dimensions to fewer dimensions while preserving as much information as possible. The entire algorithm is just four steps — all of which you now understand.
Subtract the mean and divide by standard deviation for each feature. This puts all features on the same scale so no single feature dominates the eigenvalue calculation because of its units.
The covariance matrix captures how every pair of features varies together. If two features always go up and down together, they have high covariance. This matrix is always square and symmetric.
The eigenvectors of the covariance matrix are the principal components — the directions of maximum variance. The eigenvalues tell you how much variance each direction captures. Sort both by eigenvalue, largest first.
Multiply the original data by the top k eigenvectors. The result is a new dataset with only k dimensions, but those k dimensions capture the most important variation in the original data.
When to use PCA vs when not to
Three algorithms you will use regularly — all built on eigenvalues
Eigenvalues are not just a PCA concept. They are the mathematical core of three widely deployed algorithms. Once you see the connection, these algorithms stop being black boxes.
Eigenvectors of the covariance matrix. Already covered in full above. Used everywhere: image compression, customer segmentation visualisation, noise removal from sensor data.
Regular K-Means fails when clusters are not round blobs. Spectral Clustering builds a graph where similar data points are connected, then finds eigenvalues of the graph's Laplacian matrix. The eigenvectors reveal cluster structure that K-Means can never find. Used at Amazon for discovering customer communities, at DoorDash for identifying restaurant "neighbourhoods".
The original Google algorithm that made Google worth a trillion dollars. The web is a graph: pages are nodes, links are edges. PageRank builds a matrix representing all links and finds its principal eigenvector. Pages that appear strongly in that eigenvector are the most "important" pages — the ones that many important pages link to. Every search result you see is influenced by this eigenvector.
Every common eigenvalue error — explained and fixed
When to actually reach for eigenvalues — and when a neural net is the better call
Section 6 above already walked through PCA, spectral clustering, and PageRank as production algorithms built on eigenvalues. What that section didn't cover is the judgment call: data scientists and ML engineers hit this decision constantly during feature engineering and model debugging, and eigen-decomposition is not always the right tool even when dimensionality is the problem. Here is the framework for making that call, plus the incident that shows up most often in practice — an unstable recurrent model traced back to eigenvalues of its own weight matrix.
An RNN or a deep network trains fine for hours, then the loss spikes to NaN with no obvious cause in the data. One of the first things an experienced engineer checks: the eigenvalues of the recurrent weight matrix (or, for very deep feedforward nets, the spectral norm of consecutive weight matrices). If the largest eigenvalue's magnitude is consistently above 1, the network is multiplying activations by a factor greater than 1 at every time step or layer — the exact mechanism behind exploding gradients. If it's consistently below 1, you get vanishing gradients instead.
This is the same A·e = λ·e relationship from Section 2, just applied to a weight matrix instead of a covariance matrix — the eigenvalues tell you how much repeated multiplication by that matrix stretches or shrinks a signal, which is exactly what happens to gradients flowing backward through many time steps or many layers.
Five things people get wrong about eigenvalues and eigenvectors
PCA is the most commonly taught example, but it's one of at least half a dozen production uses: spectral clustering (graph Laplacian eigenvectors), PageRank (principal eigenvector of a link matrix), recommender systems (matrix factorisation is built on eigen/singular decomposition), and stability analysis of recurrent and very deep networks, where the eigenvalues of a weight matrix directly predict exploding or vanishing gradients. Treating this as "the PCA math" makes every one of those other appearances look unrelated when they're the exact same underlying tool.
Some matrices — called defective matrices — do not have enough independent eigenvectors to form a full basis for their dimension, which breaks algorithms that assume diagonalisability. The reason covariance matrices are so convenient is the spectral theorem: every real symmetric matrix is guaranteed to have a full set of real eigenvalues and orthogonal eigenvectors. That guarantee is precisely why np.linalg.eigh (for symmetric matrices) is both safer and faster than the general-purpose np.linalg.eig — it can rely on a property that doesn't hold for arbitrary matrices.
Explained variance is a useful heuristic, not a universal rule. The right number of components depends on the downstream task: for visualisation you often want exactly 2 or 3 regardless of variance captured, and for anomaly detection the small-eigenvalue directions that a 95%-variance cutoff would discard are sometimes exactly where anomalies show up, since anomalies are often defined as points that violate the low-variance structure of normal data. Picking a threshold without checking it against actual downstream performance is a common way PCA preprocessing quietly hurts a model.
Eigenvectors are only defined up to sign and scale — if e is an eigenvector, so is −e and so is 5e, all satisfying A·e = λ·e equally well. Worse, when a matrix has a repeated eigenvalue, any vector in the entire subspace spanned by the eigenvectors for that eigenvalue is a valid eigenvector, so there is no single "correct" choice at all — just a convention. This is exactly why PCA results can flip sign between library versions or random seeds without being wrong; it's a documented consequence of the math, not a bug to chase down.
A full eigendecomposition of an n×n matrix costs roughly O(n³) — fine for a 50-feature covariance matrix, prohibitive for a 50,000-dimensional one. This is exactly why production systems almost never call np.linalg.eig directly on large matrices: they use randomized SVD, truncated SVD, or iterative methods like the power method or Lanczos algorithm, which approximate only the top few eigenvalues/eigenvectors needed without ever forming or decomposing the full matrix. sklearn's PCA switches to a randomized solver automatically once the data gets large enough.
Eigenvalues and eigenvectors — 5 questions interviewers actually ask
Most vectors change direction when you multiply them by a matrix — the matrix rotates and stretches them at the same time. An eigenvector is one of the special directions a specific matrix doesn't rotate at all; it only stretches or shrinks it, by a factor called the eigenvalue. Every square matrix has its own small set of these special directions, and they describe the "natural axes" the matrix operates along — which is why finding them tells you something fundamental about what the matrix does to any input.
The covariance matrix encodes how every pair of features varies together. Its eigenvectors point in the directions along which the data spreads out the most — the directions of maximum variance — and its eigenvalues tell you exactly how much variance each of those directions captures. Sorting by eigenvalue and keeping the top few eigenvectors gives you a lower-dimensional representation that preserves as much of the original variation as possible for the number of dimensions you're willing to keep.
eigh assumes the input is symmetric (or Hermitian) and exploits that guarantee to return real eigenvalues, use a numerically more stable algorithm, and run faster. eig makes no such assumption and can return complex eigenvalues even from a matrix that's supposed to be symmetric but has tiny floating-point asymmetries. In practice: use eigh for covariance matrices, graph Laplacians, and anything you know is symmetric by construction; use eig only for genuinely asymmetric matrices, like a Markov transition matrix in PageRank.
PageRank models the web as a transition matrix — each entry is the probability of moving from one page to another by following a link. A page's long-run importance is the stationary distribution of a random walk over this graph, and that stationary distribution is exactly the eigenvector of the transition matrix with eigenvalue 1. Pages that many important pages link to accumulate more of that eigenvector's weight, which is why PageRank captures something closer to "importance" than simply counting incoming links.
An RNN applies the same recurrent weight matrix repeatedly, once per time step, so a signal (or a gradient flowing backward) gets multiplied by that matrix over and over. The eigenvalues of that matrix determine what happens under repeated multiplication: if the largest eigenvalue's magnitude is above 1, the signal grows exponentially with sequence length and eventually overflows to NaN; if it's below 1, the signal vanishes instead. I'd check the spectral radius of the recurrent weight directly, and consider orthogonal initialisation, gradient clipping, or switching to an LSTM/GRU, whose gating mechanisms are specifically designed to control this.
You now understand how ML finds structure in data. Next: how it learns from it.
Eigenvalues help you find the important directions in a dataset before training. The next module — Derivatives, Gradients and the Chain Rule — explains how ML models actually improve during training. Specifically: how do you adjust millions of model parameters to make predictions better? The answer is gradient descent — the engine behind every neural network, logistic regression, and linear regression you will ever train.
The mathematical engine behind every learning algorithm. Understand gradient descent before you ever run model.fit().
🎯 Key Takeaways
- ✓An eigenvector is a special vector that does not rotate when a matrix transformation is applied to it — it only stretches or shrinks. The amount it stretches is the eigenvalue. The equation is A·e = λ·e.
- ✓Most vectors change direction when multiplied by a matrix. Eigenvectors are the exception — they are the "natural axes" of the transformation, the directions the matrix fundamentally operates along.
- ✓For a covariance matrix of your dataset, eigenvectors point in the directions of maximum variance in the data. The eigenvalue tells you how much variance that direction captures. Large eigenvalue = important direction. Small eigenvalue = noise.
- ✓PCA is four steps: standardise → covariance matrix → eigendecomposition → project onto top-k eigenvectors. The result is a lower-dimensional dataset that preserves the most important variation from the original.
- ✓Always use np.linalg.eigh() (not eig()) for covariance matrices — it guarantees real eigenvalues, is faster, and is numerically more stable. Always standardise data before computing eigenvalues.
- ✓Eigenvalues appear in three major production algorithms: PCA (covariance matrix eigenvectors), Spectral Clustering (graph Laplacian eigenvectors for non-convex clusters), and Google PageRank (principal eigenvector of the web link matrix).
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.