04.14 Lesson 14 of 16
1 hDimensionality 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
ExerciseReduce a correlated feature set
Apply PCA to numeric features and compare original and reduced representations without leaking held-out data.
- Select correlated numeric columns and split before fitting transformations.
- Build a Pipeline that scales and then applies PCA.
- Compare component counts or a variance-retention threshold.
- Plot cumulative explained variance and a two-component projection.
- Explain what information is discarded and why a component is not an original feature.
Scaling and PCA are fitted on training data only; the report includes explained variance and the interpretability trade-off.
Free references
Study resources
- 01 Principal Component Analysis (PCA), Step-by-Step Video
Watch the individual PCA step-by-step video. Focus on components, loadings and explained variance.
- 02 Decomposing Signals in Components: PCA Article
Read the PCA section on centering, component scores and explained variance.
Check your understanding
Self-test
-
Question 1
What does the first principal component capture?
It is the direction in the centered feature space along which projected observations have the greatest variance. -
Question 2
How is PCA different from feature selection?
PCA creates new components as combinations of original features; feature selection keeps a subset of the original columns.
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.