PCA
Dimension Reduction
Classification
Machine Learning
Data Analysis

PCA Dimension reducion for classification

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Principal Component Analysis, or PCA, is often used before classification to reduce the number of input features. That can speed up training, reduce noise, and make a model easier to visualize, but it can also remove information that the classifier actually needs.

The right way to think about PCA is simple: it keeps directions of high variance, not directions that are most predictive of the class label. That difference explains both why PCA sometimes helps and why it sometimes hurts.

What PCA Does Before a Classifier

PCA transforms the original features into a new coordinate system. The first principal component explains the largest amount of variance in the data, the second explains the next largest amount, and so on. By keeping only the first few components, you compress the feature space.

This can help classification when:

  • the original features are strongly correlated
  • the dataset has noisy dimensions
  • training time matters
  • you want a simpler linear model

It can hurt when the label signal lives in low-variance directions. A feature can have small overall variance and still be very useful for separating classes.

That is why PCA should be treated as a model choice that needs validation, not as an automatic preprocessing step.

A Safe Pipeline for Classification

The safest way to use PCA is inside a pipeline so scaling, dimensionality reduction, and classification happen consistently during both training and evaluation. The example below trains logistic regression on the breast cancer dataset from scikit-learn.

python
1from sklearn.datasets import load_breast_cancer
2from sklearn.model_selection import train_test_split
3from sklearn.pipeline import Pipeline
4from sklearn.preprocessing import StandardScaler
5from sklearn.decomposition import PCA
6from sklearn.linear_model import LogisticRegression
7
8X, y = load_breast_cancer(return_X_y=True)
9
10X_train, X_test, y_train, y_test = train_test_split(
11    X, y, test_size=0.2, random_state=42, stratify=y
12)
13
14model = Pipeline([
15    ("scale", StandardScaler()),
16    ("pca", PCA(n_components=0.95)),
17    ("clf", LogisticRegression(max_iter=2000)),
18])
19
20model.fit(X_train, y_train)
21
22print("test accuracy:", model.score(X_test, y_test))
23print("components kept:", model.named_steps["pca"].n_components_)

This code does three important things correctly:

  • it splits the data before fitting PCA
  • it scales the features before PCA
  • it keeps enough components to explain roughly 95 percent of the variance

That last choice is a reasonable baseline, but it is not guaranteed to be optimal for the classifier.

How to Choose the Number of Components

There are two common ways to pick n_components.

The first is variance retention, such as 0.90, 0.95, or 0.99. This is convenient and often good enough for a first pass.

The second is model selection. Treat the component count like any other hyperparameter and compare it with cross-validation:

python
1from sklearn.model_selection import GridSearchCV
2
3search = GridSearchCV(
4    model,
5    param_grid={"pca__n_components": [5, 10, 15, 20, 25]},
6    cv=5,
7    n_jobs=-1,
8)
9
10search.fit(X_train, y_train)
11
12print("best params:", search.best_params_)
13print("best cv score:", search.best_score_)

This approach is usually better when the classification target matters more than compression alone. The best variance threshold is not always the best classification threshold.

When PCA Is a Good Idea

PCA is especially attractive for linear models and distance-based methods. Reducing correlation between features can make optimization more stable, and removing noisy dimensions can improve generalization on smaller datasets.

It is less compelling when:

  • you already have a model that handles high dimensional data well
  • feature interpretation matters
  • the classes are separated by low-variance signals

For tree-based models, PCA is often unnecessary and can even make the features less interpretable without a meaningful accuracy gain.

Common Pitfalls

The biggest mistake is data leakage. If you fit PCA on the full dataset before the train-test split, information from the test set leaks into training and makes the evaluation overly optimistic.

Skipping feature scaling is another common error. PCA is variance-based, so unscaled features with large numeric ranges dominate the result. Standardization is usually required unless all features already share a comparable scale.

People also assume that more retained variance automatically means better classification. That is not true. PCA is unsupervised, so a component that explains little variance can still carry useful label information.

Another pitfall is over-interpreting principal components. Once PCA mixes the original features, the transformed axes are harder to explain to stakeholders than the raw input columns.

Finally, do not forget the baseline. If a classifier without PCA is just as accurate and easier to explain, PCA may not be worth the added complexity.

Summary

  • PCA reduces dimensionality by preserving variance, not by directly optimizing class separation.
  • Use PCA inside a pipeline with scaling and the classifier to avoid leakage.
  • Cross-validate the number of components instead of assuming one threshold fits every problem.
  • PCA can improve speed and reduce noise, especially for linear models.
  • It can also remove predictive low-variance information and hurt accuracy.
  • Always compare against a no-PCA baseline before keeping it in production.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.