04.14 Lesson 14 of 16

1 h

Dimensionality Reduction & PCA

Use principal component analysis to represent correlated numeric features with orthogonal directions that capture decreasing amounts of variance.

By the end of this lesson

You will be able to

  • Describe centering and principal components.
  • Interpret explained variance ratios and loadings.
  • Distinguish PCA from feature selection.
  • Recognize information loss and limits of two-dimensional plots.

PCA finds directions of high variance

Principal component analysis rotates a centered numeric feature space into orthogonal directions ordered by the variance they explain. The first component captures the largest possible variance; the next captures the largest remaining variance subject to orthogonality. Component scores are coordinates in this new basis, and loadings describe how original features contribute. PCA is not feature selection: it combines variables into new axes.

from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline

pca_pipeline = Pipeline([
    ("scale", StandardScaler()),
    ("pca", PCA(n_components=0.95)),
])
X_train_reduced = pca_pipeline.fit_transform(X_train_numeric)

Fit the projection only on training rows

PCA learns directions from data, so fitting it before the train/test split leaks information about the held-out feature distribution. Put scaling and PCA inside a pipeline so each training fold learns its own projection. explained_variance_ratio_ tells how much variance each component captures; a cumulative plot helps choose how many components retain a desired fraction. A 2D projection is useful for exploration, but it can hide class structure and does not prove visible clusters are real.

Apply what you learned

Exercise

Reduce a correlated feature set

Apply PCA to numeric features and compare original and reduced representations without leaking held-out data.

  1. Select correlated numeric columns and split before fitting transformations.
  2. Build a Pipeline that scales and then applies PCA.
  3. Compare component counts or a variance-retention threshold.
  4. Plot cumulative explained variance and a two-component projection.
  5. Explain what information is discarded and why a component is not an original feature.
Done when

Scaling and PCA are fitted on training data only; the report includes explained variance and the interpretability trade-off.

Free references

Study resources

2 links
  1. 01
    Principal Component Analysis (PCA), Step-by-Step Video

    Watch the individual PCA step-by-step video. Focus on components, loadings and explained variance.

  2. 02
    Decomposing Signals in Components: PCA Article

    Read the PCA section on centering, component scores and explained variance.

Check your understanding

Self-test

2 questions
  1. Question 1

    What does the first principal component capture?

  2. Question 2

    How is PCA different from feature selection?

Completion check

Move on when

You can fit PCA in a training-only pipeline, report explained variance and explain the information and interpretation trade-offs.

Quick find

Search Michi

Type to search every stage, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels