Anybody Can AI

Quick Stats

Completed

0

Time Spent

0m

Streak

0

User

User

Machine Learning Fundamentals

Unsupervised Learning & Beyond/Dimensionality Reduction: Simplifying Complex Data

Dimensionality Reduction: Simplifying Complex Data

Learn how to visualize and work with high-dimensional data by reducing it to its essential components.

Imagine describing a movie using 10,000 features: every actor, every scene length, every color palette, every line of dialogue. Overwhelming, right?

Now imagine capturing the essence of that movie with just 3 features: genre intensity, emotional tone, and pacing. Much simpler—and often just as useful.

That's dimensionality reduction.

The Curse of Dimensionality

More features sounds better, right? Not always.

Problems with high-dimensional data:

1. Computational Cost
- 10 features, 1,000 samples: Fast to process
- 10,000 features, 1,000 samples: Extremely slow

2. Sparsity
In high dimensions, data points are far apart. Distance-based algorithms (K-Means, KNN) break down.

Example: In 2D space, you can have dense coverage with 100 points. In 1,000D space, those same 100 points are like dust particles in a stadium—too sparse to find patterns.

3. Overfitting
With 1,000 features and 100 samples, your model can memorize rather than learn.

4. Visualization
Humans can't visualize more than 3 dimensions. How do you explore 100-dimensional data?

5. Noise
More features = more noise. Many features might be irrelevant or redundant.

Solution: Reduce dimensions while preserving the important information.

Two Approaches to Dimensionality Reduction

Feature Selection: Choose a subset of existing features
- Keep: Age, Income, Purchase History
- Drop: Account Creation Date, Last Login IP, Browser Type
- Result: Fewer features, but they're the original ones

Feature Extraction: Create new features that combine the old ones
- Original: Height, Weight, Age, Exercise Hours
- New: Health Index (combination of all four)
- Result: Fewer features, but they're transformed versions

We'll focus on feature extraction, specifically Principal Component Analysis (PCA).

Principal Component Analysis (PCA)

PCA is the most popular dimensionality reduction technique. It finds the directions (principal components) that capture the most variance in your data.

Intuition: Imagine photographing a 3D object

Option 1: Photograph from random angle → Lose important details
Option 2: Photograph from the angle that shows the most information → Preserve key features

PCA finds the "best angles" (components) to view your high-dimensional data.

How PCA Works: Visual Example

Imagine data about students:
- Feature 1: Hours studied
- Feature 2: Test scores

When you plot them, you notice:
- Students who study more score higher
- There's a clear diagonal trend

PCA's process:

Step 1: Center the data (subtract the mean)
Move the data cloud so its center is at the origin (0,0)

Step 2: Find the direction of maximum variance
Draw a line through the data that captures the most spread
This is Principal Component 1 (PC1)

In our example: PC1 goes diagonally, capturing the "general academic ability" dimension

Step 3: Find perpendicular directions
PC2 is perpendicular to PC1
Captures remaining variance

In our example: PC2 might capture "test-taking efficiency" (high score relative to study time)

Step 4: Transform data to new coordinates
Instead of (hours, scores), express each student as (PC1 value, PC2 value)

Step 5: Keep only top components
If PC1 explains 85% of variance and PC2 only 10%, you might drop PC2
Now you have 1 dimension instead of 2, with minimal information loss

PCA Step-by-Step: Iris Flowers

Classic example with 4 features:
- Sepal length
- Sepal width
- Petal length
- Petal width

Original: 4D data (can't visualize easily)

After PCA:

PC1 (explains 73% of variance)
- Combines all 4 measurements
- Roughly captures "overall flower size"

PC2 (explains 23% of variance)
- Captures differences between sepal and petal proportions
- Roughly captures "flower shape"

PC3 (explains 3% of variance)
- Captures minor variations

PC4 (explains 1% of variance)
- Mostly noise

Decision: Keep PC1 and PC2 (96% of variance with just 2 dimensions!)

Result: Can now plot flowers on a 2D scatter plot and see clear species clusters

From 4D → 2D with minimal information loss

Interpreting Principal Components

PCA creates new features, but what do they mean?

Example: Customer behavior with 10 original features

After PCA:

PC1 might represent: Overall engagement
- High values: Active users across all metrics
- Low values: Inactive users
- Loadings: All features contribute positively

PC2 might represent: Browser vs. Mobile preference
- High values: Primarily mobile usage
- Low values: Primarily desktop usage
- Loadings: Mobile features positive, desktop features negative

PC3 might represent: Weekend vs. Weekday behavior
- High values: Weekend-heavy activity
- Low values: Weekday-heavy activity

You determine meaning by examining which original features contribute most to each PC (called "loadings").

Important: PCs don't always have clean interpretations. Sometimes they're just mathematical combinations.

Choosing the Number of Components

How many PCs should you keep?

Method 1: Explained Variance Threshold
Keep enough PCs to explain 90% (or 95%) of variance

Example:
- PC1: 40% variance
- PC2: 30% variance
- PC3: 15% variance
- PC4: 10% variance
- PC5: 3% variance
- PC6+: <2% variance each

To reach 90%: Keep PC1-PC4
Reduced from 10+ dimensions to 4 dimensions

Method 2: Scree Plot
Plot variance explained by each PC
Look for the "elbow" where additional PCs add little value

Method 3: Fixed Number
If you need exactly 2D for visualization, keep first 2 PCs

Method 4: Downstream Performance
Test your model with different numbers of PCs
Keep however many maximize your evaluation metric

When to Use PCA

Great for:

1. Visualization
Reduce to 2D or 3D to explore and communicate

2. Speeding up algorithms
Fewer dimensions = faster computation

3. Reducing overfitting
Fewer features = harder to memorize noise

4. Removing multicollinearity
Correlated features become independent components

5. Compression
Store data more efficiently

Example use case:
- Original: 10,000 pixel values per image
- After PCA: 100 components capture 95% of information
- Benefit: 100x faster training, similar accuracy

Limitations of PCA

1. Linear transformations only
PCA finds linear combinations of features
Can't capture complex nonlinear relationships

Example: If your data forms a spiral pattern in 3D, PCA can't capture it well because it only uses linear projections

2. Interpretability loss
Original features (age, income) are interpretable
PCs (combination of 20 features) are often hard to interpret

3. Assumes variance = importance
PCA maximizes variance, but what if important information is in low-variance directions?

Example: Fraud detection—fraud is rare (low variance) but important

4. Sensitive to scaling
Features with larger scales dominate PCs

Solution: Always scale features before PCA

5. Doesn't consider the target variable
PCA reduces dimensions without knowing what you're trying to predict
The reduced data might lose information important for your specific task

Alternative Techniques

t-SNE (t-Distributed Stochastic Neighbor Embedding)
- Excellent for visualization (especially 2D)
- Preserves local structure (nearby points stay nearby)
- Nonlinear (can capture complex patterns)
- Slow on large datasets
- Primarily for visualization, not for feeding into models

Good for: Exploring clusters in high-dimensional data

UMAP (Uniform Manifold Approximation and Projection)
- Similar to t-SNE but faster
- Preserves both local and global structure
- Can be used for preprocessing (unlike t-SNE)

Good for: Visualization and preprocessing for clustering

Autoencoders (Deep Learning)
- Neural networks that compress and reconstruct data
- Learn nonlinear transformations
- More powerful than PCA but require more data and compute

Good for: Image compression, anomaly detection, complex data

LDA (Linear Discriminant Analysis)
- Like PCA but considers class labels
- Maximizes class separation instead of variance
- Supervised technique

Good for: Classification problems, when you have labels

Practical PCA Workflow

Step 1: Prepare Data
- Remove or impute missing values
- Scale features to similar ranges (standardize)
- Remove constant features (zero variance)

Step 2: Apply PCA
- Fit PCA on training data only
- Transform both training and test data using same PCA

Critical: Never fit PCA on test data (data leakage!)

Step 3: Choose Number of Components
- Check explained variance ratios
- Use scree plot
- Try different numbers and evaluate downstream performance

Step 4: Examine Components
- Look at loadings (which features contribute most)
- Try to interpret what each PC represents
- Check if interpretations make domain sense

Step 5: Use Reduced Data
- Train your model on reduced dimensions
- Create visualizations
- Compare performance to using all features

Step 6: Document and Monitor
- Record how many components you kept and why
- Monitor if PCA remains effective over time
- Retrain PCA periodically if data distribution shifts

Real-World Example: Image Recognition

Problem: Classify handwritten digits (0-9)
Original data: 28×28 pixel images = 784 features per image

Without PCA:
- Training time: 45 minutes
- Model size: 500 MB
- Accuracy: 97%

With PCA (150 components):
- Explains 95% of variance
- Training time: 8 minutes
- Model size: 95 MB
- Accuracy: 96%

Tradeoff: 1% accuracy loss for 5x faster training and 5x smaller model

When it matters:
- Deploying to mobile devices (size matters)
- Real-time predictions (speed matters)
- Training many models (experimentation speed matters)

When to skip PCA:
- Already have fast training
- Need maximum accuracy
- Have interpretable features you want to preserve

Key Takeaways

PCA reduces dimensions while preserving information
- Finds directions that capture maximum variance
- Transforms data into uncorrelated components

It's a tool, not a requirement
- Use when you have many features and need simplification
- Skip when features are already few and interpretable

Always scale features first
- PCA is sensitive to feature scales
- Standardization is usually necessary

Validate the tradeoff
- Fewer dimensions = faster computation
- But may sacrifice some accuracy
- Test to find the right balance

Next lesson: We'll put everything together and explore the complete ML pipeline from data to deployment.