Index Ai Academy
Glossary · Phase 3 · Machine learning

Explained variance ratio

Also written: explained variance

The share of the data's spread that each PCA component keeps. Adding them up shows how much a few components hold.

What it means

In principal component analysis (PCA), the explained variance ratio is the share of the data's total spread that each component captures. It's that component's variance divided by the variance of all of them together, so the shares add up to 1.

Its main use is deciding how many components to keep: add the shares up from the largest, and stop when the running total is high enough.

An example with scikit-learn

import numpy as np
from sklearn.decomposition import PCA

rng = np.random.default_rng(0)
base = rng.normal(size=300)
# Three columns, but the first two are mostly the same information.
X = np.c_[base + rng.normal(scale=0.1, size=300), 2 * base + rng.normal(scale=0.1, size=300), rng.normal(scale=0.5, size=300)]

pca = PCA().fit(X)
print("explained variance ratio:", pca.explained_variance_ratio_.round(3))
print("running total:           ", pca.explained_variance_ratio_.cumsum().round(3))
print("by hand:                 ", (pca.explained_variance_ / pca.explained_variance_.sum()).round(3))
What it prints
explained variance ratio: [0.954 0.045 0.002]
running total:            [0.954 0.998 1.   ]
by hand:                  [0.954 0.045 0.002]

The first two columns carry almost the same information, so the first component alone captures 95.4% of the spread, and two components capture 99.8%. You could drop from three columns to two and lose very little. The last line shows it's a plain ratio: each component's variance over the total.

Things to watch

A common rule of thumb is to keep enough components to reach 90% or 95%, and PCA(n_components=0.95) in scikit-learn does that for you. Scale your columns first with StandardScaler, or a column with big numbers will dominate just because of its units. And remember that the ratio measures spread, not usefulness: a small component can still be the one that predicts what you care about.

Try it now

Turn the line and see how much of the spread survives. PCA simply finds the best direction, then the next best, and so on.

Learn it properly

Dimensionality reduction · Visualize and compress high-dimensional data. About 30 minutes.

Create a free account See what's in the lesson

Other terms from the same lesson

Never trained an AI?

Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account.

Teach it to write