Interactive · Unsupervised learning

Principal Component Analysis, by hand

You have many numbers about each country, and most of them repeat each other. Principal Component Analysis, or PCA, is a way to replace many columns that move together with a few summary numbers that keep most of the information. On this page you do not just read about it. You drag the lines yourself, watch the numbers change, and find your own country in the result.

How to use this
Time: about 15 minutes. A laptop is easier than a phone.
Drag the lines and move the sliders. Every picture reacts at once.
Look for your home country in the final chart.

Data: loading…

Sixteen numbers, but how many stories?

hover

For each country we hold 16 health numbers: how long people live, how many babies survive their first year, how much a country spends on health, and more. Many of these numbers rise and fall together. A country where people live longer usually also loses fewer babies. So the 16 numbers are not 16 separate facts. Fewer forces are at work underneath.

To see this, here are all 193 countries on just two of the numbers.

Hover Point at a country.

The points fall along a band, not a scattered mess. High life expectancy comes with low infant mortality. Two numbers, but almost one line of information. PCA measures exactly this.

Find the direction of most spread

drag · the main idea

Let us keep those same two numbers, but first centre them (move the cloud so its middle sits at zero) and put both on the same scale. Now draw a straight line through the middle. Press each point flat onto the line. Some lines spread the pressed points far apart. Others bunch them together. The best line is the one that spreads them the most, because that line keeps the most information.

Drag Turn the line. Watch the number.

Spread captured by this line
Line angle

Four words, and nothing more for now

read

You have already seen all four ideas on the picture above. Here are their names.

Component

A component is a direction through the data, given as a recipe of weights, one weight for each input number. PC1 is the first and most important component. (This is the same idea as the “recipe of weights” in the Day 4 yield-curve lecture.)

Loading

A loading is one weight in that recipe. A large loading means that input matters a lot for this direction.

Score

A score is one country’s position along a component: take its numbers, apply the recipe, and you get a single value.

Variance explained

The variance explained is the share of the total spread that a component captures, written as a percentage. PC1 explained 94.5% above. In symbols this is the eigenvalue share λk ÷ Σλ, but you can read it as “share of the spread this axis keeps”.

The same data, seen from a better angle

animate

PCA now turns the picture so that PC1 lies flat along the bottom. The points do not move. Only the axes change. The second axis, at a right angle to the first, is the second principal component, PC2. It captures the spread that PC1 left behind. We did not search again for PC2. It is simply the direction at a right angle to PC1.

Press Rotate to the new axes, then back.

Before and after hold the very same countries. PCA is a change of viewpoint, not a change of data.

A decision you must make: standardise or not

toggle

Real health numbers use very different units. Health spending is in dollars and can reach thousands. Immunisation is a percentage between 0 and 100. If we feed these in as they are, the dollar column is numerically the largest, so it would control the first component by size alone, not by importance.

The fix is to standardise: rescale every column so it has the same spread (mean 0, spread 1). Then a one-step move means the same thing in every column. Toggle below and watch PC1’s recipe change.

PC1 catches
PC1’s biggest loading

In the Day 4 lecture we did the opposite. The nine bond yields were already in the same unit, percentage points, so we did not standardise. Same tool, opposite decision, each made on purpose. This is correlation PCA; the lecture used covariance PCA.

All sixteen numbers at once

hover · toggle

Now we use all 16 indicators, standardised. PCA gives each country two scores, one on PC1 and one on PC2, and we plot them. The arrows show each indicator’s loading: the direction that indicator pushes a country on the chart. Arrows pointing the same way belong to indicators that move together.

Hover A country to read it, an arrow to read the indicator.

PC1 is driven most by:

PC1 spreads countries from Central African Republic, Chad, DR Congo and Somalia on one side to Switzerland, Japan and the United States on the other. The income colours line up along it, even though PCA never saw the income labels. PCA found the rich–poor health gap on its own.

How many numbers do we really need?

slide

Each component keeps a share of the spread. List the shares from largest to smallest. The first is large, then the rest fall away more slowly. The point where the bars level off tells you how many to keep.

Slide Move the slider to keep the first k components. Read how much you keep.

Spread kept

One country, real (grey) vs rebuilt (navy) from k components, standardised units.

The first component alone holds 54%. Three components together hold 74%, and you need five to reach 85%. Health data does not squeeze as tightly as the bond yields in the Day 4 lecture, where three components held 95%. That contrast is the lesson: how far you can compress a dataset depends on how strongly its columns move together. Here the columns agree a lot, but not completely.

When PCA has nothing to find

switch · the honest caveat

PCA is honest. If the data has no main direction, PCA will tell you. Switch the shape of the cloud below.

PC1 catches
PC2 catches

Always read the variance explained before you trust a component. A first component that keeps only a little more than the second is not telling you much.

What PC1 turned out to be

read

We can now name the first component. High PC1 scores go with long life, low child mortality, clean water, and higher health spending. Low PC1 scores go with the opposite. PC1 is a health-and-development axis, discovered from the numbers alone, with no labels and no target.

Find your home country on the chart above. Where does it sit on the health-and-development axis? Which single indicator, if it improved, would move it most? Its loading tells you.

This is unsupervised learning: PCA was told only “find the directions of most spread”, and a real, readable story came out.

sources and next steps

Next: see the same method produce clusters instead of a gradient → Clusters: breast-cancer data

This page is the interactive companion to the Day 4 lecture (unsupervised learning). The lecture applies PCA to the US yield curve; this page applies it to global health.

How this was computed: correlation PCA (standardised inputs). The reference result comes from scikit-learn; your browser reproduces it live to six decimal places.

Sources & licencesLoading…
Oxford · United Kingdom Teaching CV
University of Oxford