šŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
šŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

PCA & Dimensionality Reduction in Python

Learn about PCA & Dimensionality Reduction in this comprehensive Python tutorial. Learn how to use Principal Component Analysis to compress datasets while retaining critical variance.

⚔ Total XP: 0|šŸ’» python XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

System Hub

Core logic.

Quick Quiz //

What does PCA(n_components=2) accomplish?


šŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
šŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

Listen up. If you're building ML pipelines, understanding PCA & Dimensionality Reduction in Python is non-negotiable. This is where models go from messy research scripts to production-grade engineering.

1The Curse of Dimensionality

A dataset with 500 columns doesn't just take longer to train — the geometry itself works against you. As the number of dimensions grows, the volume of the feature space explodes exponentially, so your training data becomes sparser relative to that volume. Distance-based algorithms (KNN, RBF-kernel SVMs, k-means) start to break down because every point ends up roughly equidistant from every other point, and 'nearest neighbor' loses its meaning.

Visualization collapses too: humans can plot two or three axes, not five hundred. Without some way to compress the feature space, you're flying blind — you can't eyeball clusters, outliers, or separability before you even start modeling.

This is the problem PCA exists to solve. Rather than discarding columns and hoping you kept the important ones, it finds a smaller set of new axes that capture as much of the original variance (information) as mathematically possible, so training gets faster and the data becomes visualizable again.

āœ•
—
+
# The Curse of Dimensionality
# More columns = exponentially harder to analyze
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Metrics calculated successfully.

2PCA: An Unsupervised Compression Algorithm

Principal Component Analysis (PCA) is an unsupervised technique — it never looks at your target labels, only at the variance structure of the feature matrix X. That's an important distinction from feature selection, which can use the target to decide which columns to drop.

In scikit-learn, you import it from sklearn.decomposition and instantiate it with n_components, the number of new axes you want to keep: PCA(n_components=2) squashes however many original features you have down to just two. Because it's unsupervised, the exact same PCA object can be reused across a classification task, a regression task, or a clustering task on the same X — the transformation only cares about the feature covariance.

Under the hood, PCA computes the eigenvectors of the feature covariance matrix (equivalently, via SVD) and ranks them by how much variance each one explains. The n_components you keep are simply the top-ranked directions.

āœ•
—
+
from sklearn.decomposition import PCA

# Compress 500 columns down to just 2 columns
pca = PCA(n_components=2)
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Metrics calculated successfully.

3What PCA Is Actually Optimizing For

The core question to internalize before writing any PCA code is: what is it actually optimizing for? PCA's objective is to find the smaller set of features that retains as much of the original variance as mathematically possible — not to guess which columns are 'important' by name, and not to improve predictive accuracy directly.

That matters because it's easy to conflate PCA with feature selection. Feature selection picks a subset of your existing, human-readable columns (drop 'zip_code', keep 'income'). PCA instead builds brand-new columns that are linear combinations of all the original ones, chosen so that the first component explains the most variance, the second explains the most of what's left, and so on.

So when you call pca.fit_transform(X), you're not filtering — you're re-projecting your data onto a new coordinate system, ordered by how much of the original spread each axis captures.

āœ•
—
+
# Dimensionality Reduction
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Metrics calculated successfully.

4How PCA Builds Principal Components

PCA does not delete columns and keep the rest — it mathematically blends all of them together. Each principal component is a weighted sum of every original feature: something like PC1 = (0.5 Ɨ Age) + (0.3 Ɨ Salary) + (0.2 Ɨ Height), where the weights (loadings) are chosen so PC1 points in the direction of maximum variance in the data.

The second component, PC2, is constrained to be orthogonal (uncorrelated) to PC1 while capturing as much of the remaining variance as possible, and so on for every subsequent component. That orthogonality is what makes the components independent summaries of the data rather than redundant copies of each other.

In code, this whole process happens in one call: X_compressed = pca.fit_transform(X) computes the covariance structure, derives the components, and projects every row of X onto them — turning a 10-column dataset into a 2-column X_compressed in a single step.

āœ•
—
+
# PC1 = (0.5 * Age) + (0.3 * Salary) + (0.2 * Height)

X_compressed = pca.fit_transform(X)
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Metrics calculated successfully.

5What Principal Components Really Are

If you reduce a 10-column dataset to 2 principal components, those 2 new columns are not a subset of the original 10 — all 10 original features contribute to both of them, just with different weights. There's no single column you can point to and say 'this is PC1'; it's a blend.

This is exactly why PCA trades interpretability for compression. A random forest's feature importances can tell you 'Age mattered most.' A model trained on PCA components can only tell you 'PC1 mattered most,' and PC1 is itself a mixture of Age, Salary, Height, and everything else — a number without a name.

The upside is that this blending is exactly what lets PCA capture correlated structure that a naive column-dropping approach would miss. If Age and Salary are correlated, PCA can fold both into a single component instead of treating them as two independent signals to choose between.

āœ•
—
+
# Principal Components
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Metrics calculated successfully.

6The Interpretability Trade-off

Because every principal component is a linear combination of all original features, the resulting columns lose their real-world meaning. 'Age' is instantly understandable to a business stakeholder; 'PC1' is not, even though PC1 might be doing most of the predictive work.

This is a genuine cost, not just a cosmetic one. If a regulator, auditor, or executive asks 'why did the model predict this?', 'because PC1 was high' is not an acceptable answer in domains like credit scoring or healthcare, where feature-level explainability can be a legal requirement.

The trade-off is explicit: you gain training speed and the ability to visualize previously invisible high-dimensional structure, but you give up the ability to attribute a prediction to a specific, nameable input feature. Decide whether that trade-off is acceptable for your use case before reaching for PCA.

āœ•
—
+
# Trade-off: You gain extreme speed and visualization capability.
# You lose the ability to say "Age caused this prediction".
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Metrics calculated successfully.

7Why You Lose Explainability

The single biggest downside of running PCA is the total loss of feature interpretability. The components it produces — PC1, PC2, and so on — are abstract mathematical directions in feature space, not variables a human can reason about the way they can reason about 'Age' or 'Income'.

This isn't a storage problem, and it isn't specific to images or any one data type — it applies to any tabular dataset you run PCA on. The moment you call fit_transform, you've exchanged your original, explainable columns for a smaller set of unexplainable ones.

That's why PCA tends to show up in exploratory analysis, visualization, and as a preprocessing step before distance-based or otherwise dimensionality-sensitive models — and much less often in settings where you need to explain individual predictions back to a human.

āœ•
—
+
# The Trade-off
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Metrics calculated successfully.

8ADA Defense Protocol: Verifying Data Loss

Compressing data always raises the question of how much information you actually kept. Before trusting a PCA-reduced dataset for modeling or reporting, you need a way to verify — not assume — how much of the original signal survived the projection.

This is where scikit-learn's PCA object becomes genuinely useful beyond just fit_transform: it exposes the exact statistics you need to audit the compression, rather than leaving you to guess whether 2 components were enough or whether you should have kept 5.

Getting comfortable reading these diagnostics is what separates 'I ran PCA' from 'I can defend this dimensionality reduction to a stakeholder' — which is exactly the skill this next check exercises.

āœ•
—
+
# SYSTEM WARNING:
# ADA Protocol initiating...
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Metrics calculated successfully.

9Measuring Variance Loss with explained_variance_ratio_

When you compress 500 columns down to 2, you are guaranteed to lose some information — the only question is how much. Scikit-Learn doesn't hide this from you; the fitted PCA object reports precisely how much of the original variance each retained component accounts for.

That number lives in pca.explained_variance_ratio_, an array with one entry per component. Summing the first two entries tells you, for example, that your 2-component compression retained 95% of the original variance — and the remaining 5% is what was discarded.

In practice, this is the number you check before deciding how many components to keep: plot the cumulative sum of explained_variance_ratio_ against the number of components and pick the point where the curve levels off, often called a 'scree plot.'

āœ•
—
+
# ADA initializing variance checks...
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Metrics calculated successfully.

10ADA Defense: Proving Retained Variance

To mathematically prove that 2 components still retain 95% of the original information, you check pca.explained_variance_ratio_ after fitting. It returns the fraction of total variance explained by each component, in order, so np.sum(pca.explained_variance_ratio_[:2]) gives you the combined percentage retained by your first two components.

This is a direct, quantitative answer — not an approximation from opening the CSV in a spreadsheet, and not something an accuracy score can tell you, because PCA has no notion of correct or incorrect predictions. It's purely about variance in the feature space.

This is also the number you'd put in a slide for a non-technical stakeholder: '2 components, 95% of the original signal retained' is a defensible, auditable claim, backed directly by the fitted PCA object rather than intuition.

āœ•
—
+
# DEFEND THE SYSTEM
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Metrics calculated successfully.

11Wrapping Up: From PCA to Pipelines

At this point you've covered the full arc of PCA: why high-dimensional data is a problem, how PCA blends the original features into new orthogonal components ordered by variance, the interpretability cost that blending introduces, and how to quantify exactly how much variance you kept via explained_variance_ratio_.

That combination — dimensionality reduction plus a rigorous way to audit information loss — is what makes PCA a standard first step in exploratory data analysis and a common preprocessing stage before distance-sensitive estimators.

From here, the natural next steps are evaluating how a downstream model performs on the reduced data and chaining PCA together with a scaler and an estimator inside a single scikit-learn Pipeline, so the same transformation is applied consistently to both training and test data.

āœ•
—
+
print("System secured.\
Dimensions reduced.")
localhost:3000
Jupyter Notebook / Console Output
Model Code Executed
Metrics calculated successfully.

12Step-by-Step Breakdown

What if your dataset has 500 columns (features)? Training models will take forever, and you cannot visualize a 500-dimensional graph.

Principal Component Analysis (PCA) is an Unsupervised Dimensionality Reduction algorithm. It squashes massive datasets down to just a few columns.

What is the primary purpose of Principal Component Analysis (PCA)?

  • →To predict categorical labels.
  • →To mathematically reduce the number of features (columns) in a dataset while retaining as much of the original variance (information) as possible.
  • →To increase the number of columns to improve accuracy.

PCA does NOT just delete columns. It mathematically combines them. It creates new "Principal Components" that capture the variance of multiple original features.

If you use PCA to reduce a 10-column dataset to 2 Principal Components, what do those 2 new components actually consist of?

  • →They are just the 2 most important original columns; the other 8 are permanently deleted.
  • →They are entirely new mathematical variables created by blending the original 10 columns together.
  • →They are just random numbers.

Because PCA creates new blended variables, your dataset loses all human interpretability. Column "PC1" means nothing to a human executive.

What is the major negative trade-off of running PCA on your dataset?

  • →The dataset becomes too large to store on a hard drive.
  • →You completely lose feature interpretability. The new columns (PC1, PC2) are abstract mathematical concepts, not human-readable variables like 'Age'.
  • →It only works on image data.

Now, prepare yourself. We are about to enter the ADA Defense Protocol. Ensure you understand how to verify data loss.

When you compress 500 columns down to 2, you inevitably lose some data. Scikit-Learn tells you exactly how much.

ADA DEFENSE: You compress your dataset to 2 components. How can you mathematically prove to your boss that those 2 components still retain 95% of the original information?

  • →By checking pca.explained_variance_ratio_. It outputs the exact percentage of the original data's variance captured by the new components.
  • →By running an accuracy score on the PCA object.
  • →By opening the CSV in Excel and counting the missing cells.

Threat neutralized. Variance confirmed. Proceeding to Model Evaluation and Pipelines.

Reduce Real Dimensions with PCA. Finish reduce_dimensions(): fit_transform() learns the components and projects the data in one step.

Level Up šŸš€

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Readable Dimensionality-Reduction Code

Explicitly setting n_components and inspecting explained_variance_ratio_ documents your reasoning for future readers, rather than leaving a 'why 2 components?' mystery in the codebase.

pca = PCA(n_components=2) X_reduced = pca.fit_transform(X) print(pca.explained_variance_ratio_.sum())

SEO Implications

  • 1

    High-Intent Reference Content

    Searches like 'PCA explained variance ratio' and 'PCA vs feature selection' are common among practitioners preparing for interviews or debugging real pipelines, making precise, code-backed explanations valuable for organic search.

Best Practices

Scale Features Before PCA

PCA is sensitive to feature scale because it operates on variance — a column measured in thousands (salary) will dominate a column measured in single digits (age) unless you standardize first with StandardScaler.

Choose n_components From explained_variance_ratio_, Not Guesswork

Fit PCA once with n_components=None, plot the cumulative explained_variance_ratio_, and pick the smallest number of components that clears your target variance threshold instead of hardcoding an arbitrary number.

Frequent Bugs

THE BUG

Running PCA on unscaled features, so a single large-magnitude column dominates every principal component.

THE FIX

Standardize features with StandardScaler().fit_transform(X) before fitting PCA, so every feature contributes to the variance calculation on a comparable scale.

Real-World Examples

Compressing a High-Dimensional Feature Set for Visualization

A team has a 50-feature customer dataset they want to plot to look for natural clusters before choosing a clustering algorithm.

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

X_scaled = StandardScaler().fit_transform(X)
pca = PCA(n_components=2)
X_2d = pca.fit_transform(X_scaled)
print(pca.explained_variance_ratio_.sum())  # e.g. 0.83

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Fitting PCA on unscaled features

# Wrong: salary (thousands) dominates age (tens) pca = PCA(n_components=2) X_reduced = pca.fit_transform(X) # Correct: scale first from sklearn.preprocessing import StandardScaler X_scaled = StandardScaler().fit_transform(X) X_reduced = pca.fit_transform(X_scaled)

The Solution //

PCA maximizes variance, so a feature with a larger numeric range will dominate the components regardless of its actual predictive value. Always standardize features (mean 0, unit variance) before fitting PCA.

The Error //

Fitting PCA on the full dataset before splitting into train/test

# Wrong: leaks test-set variance into the components X_reduced = pca.fit_transform(X) X_train, X_test = train_test_split(X_reduced) # Correct: fit only on training data X_train, X_test = train_test_split(X) pca.fit(X_train) X_train_reduced = pca.transform(X_train) X_test_reduced = pca.transform(X_test)

The Solution //

Calling fit or fit_transform on the entire dataset lets information from the test set leak into the components used to train your model, inflating validation scores. Fit PCA only on the training split, then use transform (not fit_transform) on the test split.

Lesson Glossary

[01]PCA

Principal Component Analysis. A statistical procedure that uses an orthogonal transformation to convert a set of observations of possibly correlated variables into a set of values of linearly uncorrelated variables.

Code Preview
// PCA context

[02]Curse of Dimensionality

Various phenomena that arise when analyzing and organizing data in high-dimensional spaces that do not occur in low-dimensional settings.

Code Preview
// Curse of Dimensionality context

Continue Learning