Reduce Dimensions with PCA

Reduce dimensions with PCA — Python for data science.

Focus: reduce dimensions with pca

Sponsored

Your dataset has 50 columns, but most of them are noise, redundant, or slow to train on. You could pick a few by hand, but you'd be guessing — and you'd probably lose the signal hidden in the correlations between features. That's the problem Principal Component Analysis (PCA) solves: it reduces dimensions with PCA by finding the directions of maximum variance in your data, so you keep the signal and drop the noise, all with a few lines of Python.

The problem this lesson solves

Real-world data rarely comes in tidy, independent columns. You often have hundreds of features — sensor readings, survey responses, text embeddings — and many of them are correlated with each other. For example, in a customer dataset, annual income and credit score might move together. You don't need both; you need one combined view. Training models on high-dimensional data leads to three specific pains:

  • Curse of dimensionality: As dimensions grow, data becomes sparse, and distance metrics (used by k-NN, clustering) lose meaning.
  • Slow training: More features means more computation in every model you build — and more memory.
  • Overfitting risk: With too many features and too few samples, your model memorize noise instead of learning patterns.

PCA is a dimensionality reduction technique that transforms your original features into a smaller set of new features — called principal components — that capture the most important variance in your data. It's unsupervised, so it works without labels, and it's a standard preprocessing step in almost every machine learning pipeline.

Why it matters now: Before you train a classifier or run clustering, PCA can reveal structure, remove redundancy, and cut training time dramatically — often with little to no loss in model accuracy.

Core concept / mental model

Think of PCA as rotating your data to find its natural axes, then keeping only the axes where the data spreads out the most.

Imagine a cloud of points shaped like a flat cigar floating in 3D space. The cigar has three dimensions, but its real variation is mostly along its long axis, a bit along its width, and almost none along its height. PCA finds those three axes (the principal components), orders them by how much variance they explain, and lets you drop the ones with the least variance. You keep the long and wide axes — and lose the negligible height axis — reducing dimensions with PCA from 3 to 2 without losing much information.

The key terms:

  • Principal Component: A new axis that is a linear combination of the original features. The first PC captures the largest variance, the second PC captures the next largest (and is orthogonal to the first), and so on.
  • Explained Variance Ratio: The fraction of total variance each component explains. This tells you how much information you kept.
  • Loading: The weight of each original feature in a component — tells you which features contribute most.

PCA is essentially a coordinate transformation that decorrelates your features and sorts them by importance. It's like summarizing a book by its most distinctive sentences, rather than reading every word.

How it works step by step

PCA follows a clear mathematical recipe. Here's the logical flow you can carry into any implementation:

  1. Standardize your data: Since PCA is variance-based, features with larger scales (e.g., income in dollars vs. age in years) would dominate. Standard scaling (mean=0, std=1) puts all features on the same footing.
  2. Compute the covariance matrix: This captures how each feature varies with every other feature.
  3. Find eigenvectors and eigenvalues: Each eigenvector is a direction (a principal component), and its eigenvalue tells you how much variance lies along that direction.
  4. Sort components by eigenvalue: The direction with the largest eigenvalue is the first PC, then the second, and so on.
  5. Project your data onto the top k components: This gives you a new, reduced dataset with k columns.

In Python with scikit-learn, you don't code the math yourself — PCA does it all. But understanding this flow helps you interpret results and avoid pitfalls like forgetting to scale.

Pro tip: Always ask "how many components?" — either choose k to explain at least 95% of variance, or use the elbow method on the explained variance plot. More components aren't always better; they add noise.

Hands-on walkthrough

Let's apply PCA to a classic dataset: the Iris flower dataset (4 features) and then scale up to a higher-dimensional example. We'll use scikit-learn and pandas.

First, install and import what you need:

# If you haven't installed scikit-learn yet:
# pip install scikit-learn

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA

Example 1: PCA on the Iris dataset (2D projection)

# Load data
iris = load_iris()
X = iris.data          # features
labels = iris.target   # species (for coloring)
feature_names = iris.feature_names

# Step 1: Standardize
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Step 2: Apply PCA for 2 components
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)

# Inspect the result
print("Original shape:", X.shape)
print("Reduced shape:", X_pca.shape)
print("Explained variance ratio:", pca.explained_variance_ratio_)
print("Total variance explained:", sum(pca.explained_variance_ratio_))

# Plot the 2D projection
plt.figure(figsize=(8, 6))
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=labels, cmap='viridis', alpha=0.7)
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.title('Iris dataset after PCA (2D)')
plt.colorbar(label='Species')
plt.grid(alpha=0.3)
plt.show()

Expected output (numbers may vary slightly):

Original shape: (150, 4)
Reduced shape: (150, 2)
Explained variance ratio: [0.72962445 0.22850762]
Total variance explained: 0.95813207

The two components explain ~96% of the variance — you went from 4 to 2 dimensions while keeping almost all information. The scatter plot shows three clear clusters, which would help any downstream classifier.

Example 2: Choosing how many components with explained variance

# Fit PCA without specifying components to see all
pca_full = PCA()
pca_full.fit(X_scaled)

# Cumulative explained variance
cumulative = np.cumsum(pca_full.explained_variance_ratio_)
print("Cumulative explained variance:", cumulative)

# Plot elbow
plt.figure(figsize=(8, 5))
plt.plot(range(1, len(cumulative) + 1), cumulative, marker='o')
plt.xlabel('Number of components')
plt.ylabel('Cumulative explained variance')
plt.title('Elbow plot for PCA')
plt.axhline(y=0.95, color='r', linestyle='--', label='95% threshold')
plt.legend()
plt.grid(alpha=0.3)
plt.show()

Expected output:

Cumulative explained variance: [0.72962445 0.95813207 0.99482162 1.        ]

Here, 2 components already cross the 95% threshold — a clear sign that 2 is a good choice. In a high-dimensional dataset, this plot is your best friend.

Example 3: PCA on a higher-dimensional synthetic dataset

# Generate a 10-dimensional dataset with redundancy
np.random.seed(42)
X_high = np.random.randn(200, 10)
# Add strong correlations between features
X_high[:, 5] = X_high[:, 0] * 2 + X_high[:, 1] * 0.5 + np.random.normal(0, 0.1, 200)
X_high[:, 7] = X_high[:, 2] * 1.5 - X_high[:, 3] + np.random.normal(0, 0.1, 200)

# Standardize
X_high_scaled = scaler.fit_transform(X_high)

# Fit PCA
pca_high = PCA(n_components=0.95)  # keep components that explain 95% variance
X_reduced = pca_high.fit_transform(X_high_scaled)

print("Original shape:", X_high.shape)
print("Reduced shape:", X_reduced.shape)
print("Number of components kept:", pca_high.n_components_)
print("Explained variance ratio:", pca_high.explained_variance_ratio_)

Expected output (roughly):

Original shape: (200, 10)
Reduced shape: (200, 5)
Number of components kept: 5
Explained variance ratio: [0.281355   0.22487684 0.18627894 0.15390808 0.10358115]

You reduced 10 dimensions to 5 using the n_components=0.95 shortcut — no manual guessing. The model that trains on X_reduced will run faster and often generalize better.

Compare options / when to choose what

PCA isn't the only dimensionality reduction method. Here's a quick comparison to help you decide when to reach for PCA versus other approaches:

Method Best for Pros Cons
PCA Linear data, preserving global variance Fast, interpretable, widely supported Assumes linearity; doesn't handle non-linear patterns well
t-SNE Visualizing high-dimensional clusters Great for 2D/3D plots, captures non-linear structure Slow on large datasets; results can vary between runs; only for visualization
UMAP Non-linear reduction, often faster than t-SNE Preserves more global structure, faster than t-SNE More parameters to tune; less interpretable
Feature selection (e.g., SelectKBest) When you need to keep original feature names Interpretable; no transformation of data Ignores correlations between features; may drop useful combos

When to choose what:

  • Use PCA when your goal is preprocessing for modeling (classification, regression, clustering) or when you need a fast, deterministic transformation.
  • Use t-SNE when you want a beautiful 2D plot to explore clusters — but don't feed its output into a classifier.
  • Use UMAP as a modern alternative to t-SNE that's faster and scales better.
  • Use feature selection when interpretability is critical (e.g., medical diagnostics) and you need to keep the original columns.

Case in point: If you're building a credit risk model with 50 correlated financial ratios, PCA can collapse them to 10 components while retaining 95% variance — but if a regulator asks why a loan was denied, you'll need feature selection or a post-hoc explanation.

Troubleshooting & edge cases

PCA is powerful but easy to misuse. Here are common pitfalls and how to fix them:

  • Forgetting to scale data — If you run PCA on raw features with different units (e.g., age in years vs. income in dollars), the high-variance feature will dominate the first component regardless of importance. Fix: always StandardScaler().fit_transform(X) first.
  • Choosing too many components — Beyond the knee of the elbow plot, you're just keeping noise. Fix: use n_components=0.95 or inspect the cumulative explained variance plot.
  • Interpreting components as original features — Principal components are combinations of features; you can't say "PC1 is income." Fix: look at loadings to see which original features contribute most, but keep the abstraction.
  • Using PCA when data has missing valuesscikit-learn's PCA won't accept NaN. Fix: impute missing values first (e.g., with SimpleImputer).
  • Kernel PCA for non-linear data — Standard PCA misses curved patterns. If your data has a moon shape, try KernelPCA with an RBF kernel, which maps data into a higher-dimensional space before applying PCA.
  • Getting negative loadings or components — This is normal; the sign is arbitrary. Don't be alarmed, just don't read meaning into sign flips.

If your explained variance is surprisingly low (e.g., 50% with 2 components), your data might be highly non-linear, or you have too many independent dimensions. Reassess with the elbow plot and consider whether PCA is the right tool.

Edge case: If you have more features than samples (e.g., 1000 genes, 50 patients), PCA can still work but may overfit. In that case, consider using TruncatedSVD for sparse data or add regularization downstream.

What you learned & what's next

You've learned how to reduce dimensions with PCA — from the core concept (finding directions of maximum variance) to hands-on implementation in scikit-learn. You can now:

  • Explain what principal components are and how they capture variance.
  • Standardize data before PCA and apply PCA(n_components=2) for 2D visualization.
  • Use explained_variance_ratio_ and an elbow plot to choose the right number of components.
  • Apply PCA as a preprocessing step for machine learning, reducing training time and overfitting.

What's next: Now that your data is compact and clean, the next natural step is to build a classification model on the reduced features — for example, train a LogisticRegression on the Iris PCA output and compare accuracy to the original 4 features. That will cement the value of dimensionality reduction in a full data science workflow.

Remember: PCA is a tool, not a magic bullet. Use it when you have redundant, correlated features — and always combine it with good scaling and validation.

Practice recap

As a quick exercise, take the Iris dataset, apply PCA with n_components=2, and train a LogisticRegression on the reduced features. Compare its accuracy to a model trained on the original 4 features, and note the training time difference. Then try the same on the synthetic 10-dimensional dataset — see how your model's performance changes as you vary the number of components.

Common mistakes

  • Forgetting to standardize features before PCA — high-magnitude columns dominate the components, leading to misleading results.
  • Choosing too many components without referencing the explained variance ratio or elbow plot — you keep noise and lose the benefit of reduction.
  • Interpreting principal components as original features — each PC is a linear combination, not a single column.
  • Using PCA on data with missing values without imputation — scikit-learn's PCA raises an error on NaN.
  • Assuming PCA works well on non-linear data — standard PCA misses curved patterns; consider KernelPCA instead.

Variations

  1. KernelPCA: applies PCA in a higher-dimensional feature space via a kernel trick, capturing non-linear relationships.
  2. IncrementalPCA: fits PCA on mini-batches of data, useful for datasets too large to fit in memory.
  3. TruncatedSVD: works directly on sparse matrices and is often faster than PCA for text or one-hot encoded data.

Real-world use cases

  • Compressing a 1,000-dimension DNA expression matrix into 50 components for clustering patient subgroups.
  • Reducing 40 financial ratios to 10 components before training a credit risk classifier, speeding up training and reducing overfitting.
  • Projecting millions of user interaction features onto 2D PCA components to visualize user segments in a marketing analytics dashboard.

Key takeaways

  • PCA finds the axes of maximum variance and projects data onto a lower-dimensional space, preserving as much information as possible.
  • Always standardize your features before applying PCA to avoid scale dominance.
  • Use explained_variance_ratio_ or an elbow plot to choose the number of components that capture at least 95% of variance.
  • PCA is linear — for non-linear structures, turn to KernelPCA, t-SNE, or UMAP.
  • PCA is great for preprocessing, visualization, and noise reduction, but it sacrifices interpretability since components are combinations of original features.

Sponsored

Sponsored

Discussion

Questions, corrections, and tips help everyone reading this page.

0 comments

Add a comment

Shown publicly with your comment.

Be constructive · max 4,000 characters

No comments yet — start the thread.

Related tutorials, quizzes, and articles for this topic.