Skip to content

Lab: PCA, units and how many components

The representation chapter's first lab. One column recorded in grams takes over PCA until the columns are standardized, and the number of components needs a rule set in advance.

Applied ~22 min6 steps

What runs in CodeSplash

You will be able to

  • Show how a column's units change PCA's first component
  • Compute variance shares from the eigenvalues of a covariance or correlation matrix
  • Choose the number of components with a rule fixed before looking
  • Say when standardizing is the wrong choice

Read first

01

The question

Read

PCAPrincipal component analysisRotates correlated variables into uncorrelated components ordered by how much variance they capture. finds the directions along which the rows vary most. Variance has units, so a column recorded in grams instead of kilograms has a variance a million times larger. Does that change what PCA reports? And once the scaling is settled, how many components should you keep? The lab answers both on five measurements driven by two hidden factors.

02

Explore it

Explore

AExplorable · Low-rank pictures

How few rank-one layers does it take to rebuild a matrix?

How many rank-one layers will it take to keep 90% of this picture's squared size?

11
How sure?

Picture

2
0.10
OriginalRank 2What's left

Each singular value measures one rank-one layer. Keeping the largest k layers gives the best rank-k picture there is (Eckart–Young); the error is the size of the layers you drop.

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

A matrix of rank k keeps only its k largest directions. Change k and watch the approximation error fall; the error left after k directions is exactly the variance in the directions you dropped. Then press Break it to see one direction dominate everything.

03

Do it yourself

Explore

The notebook runs PCA on the raw columns and on standardized columns, compares the first component's share of the variance, and counts the components needed to reach 90%.

Notebook exercise · ~12 min

PCA, units and how many components

Run PCA on raw and standardized columns, see one column's units take over, and choose the number of components honestly.

  1. The first component on raw columns

    Compute the covariance matrix of X as recorded and its eigenvalues (eig_desc() sorts them largest first). Store the share of the total variance on the first principal component in pc1_raw.

    Show a solution
    Python
    lam = eig_desc(np.cov(X, rowvar=False))
    pc1_raw = lam[0] / lam.sum()
    R
    lam <- eig_desc(cov(X))
    pc1_raw <- lam[1] / sum(lam)
  2. The first component after standardizing

    Standardize each column (or use the correlation matrix, which is the covariance of standardized columns) and store the first component's share of the variance in pc1_std.

    Show a solution
    Python
    lam_std = eig_desc(np.corrcoef(X, rowvar=False))
    pc1_std = lam_std[0] / lam_std.sum()
    R
    lam_std <- eig_desc(cor(X))
    pc1_std <- lam_std[1] / sum(lam_std)
  3. How many components?

    On the standardized columns, store in k90 the smallest number of components whose shares add up to at least 90% of the variance.

    Show a solution
    Python
    k90 = int(np.argmax(np.cumsum(lam_std) / lam_std.sum() >= 0.9)) + 1
    R
    k90 <- which(cumsum(lam_std) / sum(lam_std) >= 0.9)[1]

In the app this opens as a Python or R notebook with the sample data, and its checks mark your progress.

04

In CodeSplash

In the app

Discover's Map the data runs PCA with a Standardize variables option and shows the explained variance for each component. Run it twice on the same dataset, with and without standardizing, and compare the first component's loadings: the large-unit column takes over when the columns stay raw.

05

Check: the lab

Checkpoint

Check yourself

One column is recorded in grams and the others in comparable units. Unstandardized, the first component explains nearly all the variance. What does that mostly reflect?

Show the answer

Answer: a

PCA on a covariance matrix follows raw variance. A column with large numbers dominates until the scaling puts the columns on comparable footing.

Check yourself

Which way of choosing the number of components holds up best?

Show the answer

Answer: a

A rule chosen after seeing the result can be bent toward the answer you hoped for. Held-out reconstruction error or a stated variance share keep the choice honest.

Check yourself

When might you deliberately not standardize before PCA?

Show the answer

Answer: a

Standardizing gives every column equal weight. If the columns share a unit and a bigger variance really is more signal, equal weighting throws that away.

06

Recap

Recap
  • PCA follows variance, so it depends on units; decide the scaling from what the units mean.
  • Variance shares are eigenvalues over their sum (the trace).
  • Choose the number of components with a rule fixed in advance.

Concepts in this lesson