The Math Behind the Methods

Eigenvectors of a Covariance Matrix and What They Reveal About Data

Work a small example to see how covariance eigenvectors show the main directions of spread, redundancy among features and their limits.

Key takeaways

  • The covariance matrix records each feature's variance on the diagonal and how pairs of features move together elsewhere.
  • Its eigenvectors are orthogonal directions, and each eigenvalue is the variance of the data along its direction.
  • The share of total variance along a direction is its eigenvalue divided by the sum of all eigenvalues.
  • Eigenvalues close to zero reveal that some features are nearly linear combinations of others.
  • Results depend on feature units and outliers, and high variance does not imply predictive value.

The eigenvectors of a covariance matrix are the perpendicular axes along which your data cloud is naturally laid out. The eigenvalue attached to each one is the variance of the data along that axis. The largest eigenvalue marks the direction of greatest spread, and eigenvalues near zero mark directions in which the data barely move.

That makes them a compact summary of how features vary together. It is a summary of linear spread only, and it says nothing by itself about which directions help predict a target.

The covariance matrix

Start with X of shape (n, d), rows as examples. Subtract each column's mean to get the centered matrix X_c. The sample covariance matrix is then:

Σ = (1 / (n − 1)) · X_cᵀ X_c        shape (d, d)

Entry (j, j) is the variance of feature j. Entry (j, k) is the covariance of features j and k, positive when they tend to be above their means together and negative when one is high while the other is low. The matrix is symmetric.

It also answers a useful question about any direction. If u is a vector of length 1, project every centered example onto u. The variance of those projected numbers equals uᵀΣu.

What an eigenvector is

A nonzero vector v is an eigenvector of Σ if Σv = λv. Applying the matrix stretches v by the factor λ without turning it off its line.

Symmetric matrices behave especially well. A d by d covariance matrix has d eigenvectors that can be chosen to be mutually orthogonal and of length 1. All eigenvalues are real, and none is negative, because each equals a variance: λ = vᵀΣv for a unit eigenvector v.

The direction that maximizes uᵀΣu over all unit vectors is the eigenvector with the largest eigenvalue. The next one maximizes it among directions orthogonal to the first, and so on.

A worked example

Take four points that are already centered: (3, 3), (−3, −3), (1, −1) and (−1, 1).

variance of x     = (9 + 9 + 1 + 1) / 3   = 20/3
variance of y     = (9 + 9 + 1 + 1) / 3   = 20/3
covariance x, y   = (9 + 9 − 1 − 1) / 3   = 16/3

v₁ = (1, 1)/√2     Σv₁ = (36/3) · v₁      λ₁ = 12
v₂ = (1, −1)/√2    Σv₂ = (4/3) · v₂       λ₂ = 4/3

The total variance is 20/3 + 20/3 = 40/3, which equals 12 + 4/3. This is no accident: the sum of the eigenvalues always equals the sum of the diagonal entries, called the trace.

The first direction holds 12 ÷ (40/3) = 0.9 of the total, so 90% of the spread lies along the diagonal line y = x. Projecting the point (3, 3) onto v₁ gives 6/√2 ≈ 4.24. The point (1, −1) projects to 0 and shows up only along v₂.

From a feature table to its main axes
1 Center Subtract the training mean of each feature. 2 Form covariance Measure how each pair of features varies together. 3 Decompose Find orthogonal directions and their variances. 4 Sort Order directions from largest variance to smallest. 5 Project Express each example in the new coordinates. Center Subtract the training mean of each feature. 1 Form covariance Measure how each pair of features varies together. 2 Decompose Find orthogonal directions and their variances. 3 Sort Order directions from largest variance to smallest. 4 Project Express each example in the new coordinates. 5

What the eigenvalues reveal

Read the sorted list of eigenvalues, often called the spectrum, as a description of the cloud's shape.

Pattern in the spectrum What it suggests
One value far above the rest A single dominant direction, such as overall size or brightness
Several large values, then a long flat tail A few strong factors plus noise
All values similar A roughly round cloud with no preferred directions
One or more values near zero Redundant features or an exact linear constraint
Only n − 1 or fewer nonzero values Too few examples to span all d features

A near-zero eigenvalue deserves a look at its eigenvector. If it has large entries on "subtotal", "tax" and "total" with matching signs, you have found a bookkeeping identity in your columns. Such redundancy makes the weights of a linear regression unstable.

The number of directions needed to cover most of the variance gives a rough sense of effective dimension. Choosing a cutoff is a judgment call, discussed in how many principal components to keep.

Finding the leading direction by repeated multiplication

You can find the top eigenvector without any special routine. Start with almost any vector, multiply it by Σ, rescale it to length 1 and repeat. This is power iteration.

It works because each multiplication stretches the component along eigenvector i by λᵢ. The component with the largest eigenvalue grows fastest. Starting from (1, 0) in the example:

start                    (1, 0)
after 1 multiplication   direction (20, 16),    ratio of entries 0.80
after 2                  direction (656, 640),  ratio about 0.976
after 3                  ratio about 0.997
target v₁                ratio 1

The direction above uses 3Σ, whose rows are (20, 16) and (16, 20), because rescaling the matrix does not change where vectors point. The gap to the target shrinks by roughly λ₂/λ₁ = 1/9 at each step.

When the top two eigenvalues are close, convergence is slow, which mirrors the fact that the leading direction is then poorly determined. A starting vector with no component along v₁ would miss it in exact arithmetic, so a random start is the usual choice. To get further directions, work with several vectors at once and keep them orthogonal.

Uses in machine learning

Principal component analysis projects data onto the top eigenvectors. It compresses features, removes correlations among the new coordinates and gives two-dimensional views. See PCA worked through on a small data set.

Whitening rotates onto the eigenvectors and divides each coordinate by √λ, so that the transformed training data have identity covariance. Tiny eigenvalues make this amplify noise, so a small constant is usually added before dividing.

Mahalanobis distance measures how far a point is from the mean in units of spread along each eigen-direction. In the example above, (2, 2) has distance √(8/12) ≈ 0.82, while (2, −2) has distance √(8 / (4/3)) ≈ 2.45. Both are equally far in the ordinary sense, but the second is far more unusual for this cloud.

Computing them

Use a routine designed for symmetric matrices. Here the array is centered with its own mean, which suits exploration of a training set.

import numpy as np

X = np.array([[3.0, 3], [-3, -3], [1, -1], [-1, 1]])   # (4, 2)
Xc = X - X.mean(axis=0)
S = Xc.T @ Xc / (len(X) - 1)                           # (2, 2)

vals, vecs = np.linalg.eigh(S)        # ascending order
order = np.argsort(vals)[::-1]
vals, vecs = vals[order], vecs[:, order]
print(vals, vals / vals.sum())        # about [12, 1.333] and [0.9, 0.1]
scores = Xc @ vecs                    # coordinates in the new axes

The eigenvectors are the columns of vecs. For wide or ill-conditioned data, applying singular value decomposition directly to X_c is usually preferred, because forming the product X_cᵀX_c loses numerical precision.

Reading loadings with care

The entries of an eigenvector are often called loadings. A large absolute loading means that feature moves the score along this axis strongly, per unit of that feature.

Say you standardize measurements of animals and the first eigenvector has similar positive loadings on length, height and weight. It is reasonable to describe that score as overall size. If the second has a positive loading on length and a negative one on weight, it contrasts long, light animals with short, heavy ones.

Treat such names as hypotheses. Check them by listing the examples with the highest and lowest scores. Correlated features share loadings, so a modest value does not mean a feature is unrelated to the axis. Recompute on bootstrap samples, aligning signs first, and report only patterns that survive. Differences between data sources, such as two instruments with different calibration, frequently appear as a leading axis, and that is worth knowing before you train anything.

Where the reading goes wrong

Units. Record one length in millimeters instead of meters and its variance grows by a factor of a million, so it will dominate the first eigenvector. When units are unrelated, standardize first, which amounts to decomposing the correlation matrix.

Outliers. Variance uses squared deviations. A handful of extreme rows can create a leading direction that points straight at them.

Signs and ties. If v is an eigenvector, so is −v, and different libraries or machines may return either. When two eigenvalues are nearly equal, the individual directions are unstable, although the plane they span may be steady.

Small samples. With d comparable to n, estimated eigenvalues spread out more than the true ones: the largest tend to be too large and the smallest too small. Resample the data and see which directions persist.

Interpretation. An eigenvector is a weighted mix of features, chosen to capture variance under an orthogonality constraint. It need not correspond to any real mechanism. A direction with small variance may carry exactly the signal that separates your classes.

Curved structure. Points on a circle have no single dominant axis, even though one angle describes them. Methods such as kernel PCA address some nonlinear cases.

Common questions

Should test data be included when computing the covariance?

No. Estimate the mean and the eigenvectors on the training fold, then apply the same shift and projection to validation and test examples.

Is a zero covariance the same as independence?

No. Covariance measures linear association. If x is symmetric around zero and y = x², their covariance is zero although y is completely determined by x.

How large can d be before this is impractical?

Forming and decomposing a full d by d matrix costs memory proportional to d² and time proportional to about d³. When you need only the leading directions, iterative or randomized methods work from matrix products and avoid building it.

Written for 4MachineLearning with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 26 articles in The Math Behind the Methods →

Latest news on this topic