Senior 13 min · March 06, 2026
Principal Component Analysis

PCA Failure — Unscaled Feature Skews Segmentation

Feature with values 1e6–1e9 caused first principal component to capture only that column, breaking segmentation.

N
Naren Founder & Principal Engineer

20+ years shipping production ML systems and the infrastructure behind them. Drawn from code that ran under real load.

Follow
Production
production tested
May 23, 2026
last updated
1,554
articles · all by Naren
 ● Production Incident 🔎 Debug Guide ⚙ Triage Commands
Quick Answer
  • PCA transforms correlated features into uncorrelated principal components ranked by variance
  • Components are eigenvectors of the covariance matrix; eigenvalues give variance explained
  • SVD is numerically stable; scikit-learn uses SVD by default, not eigendecomposition
  • Always standardize features (zero mean, unit variance) before PCA — or the first component captures scale, not structure
  • Explained variance ratio tells you how many components keep 90-95% of information
  • Inverse transform reconstructs data with compression error; monitor reconstruction loss in prod
✦ Definition~90s read
What is Principal Component Analysis?

Skip the dry definition. Here's how PCA works and why it exists.

Imagine you have 50 photos of the same person's face taken from slightly different angles, lighting and distances.

At its heart, PCA finds a set of orthogonal axes — principal components — that capture the maximum variance of your data. The first PC points in the direction of greatest spread. The second PC is orthogonal to the first and captures the next most variance, and so on.

For correlated data, the first few PCs typically explain 90%+ of the total variance. You drop the rest and compress your dataset with minimal information loss.

When your model is overfitting from too many features, PCA is the tool. It's also your first stop when you need to visualize high-dimensional data in 2D or 3D. But it's not magic — if your features are on different scales, PCA will focus on the high-magnitude ones and ignore the rest. That's why we standardize first.

Plain-English First

Imagine you have 50 photos of the same person's face taken from slightly different angles, lighting and distances. Instead of storing all 50 photos, you find the 3 or 4 'directions of change' that capture almost everything interesting — like how much the face tilts, how bright the light is, how close the camera is. PCA does exactly that for data: it finds the fewest possible 'directions' that still tell you almost the whole story. You throw away the boring, repetitive directions and keep only the ones that carry real information.

Modern datasets are wide. A genomics study might have 20,000 gene expression columns per patient. A recommendation engine might embed every user into a 512-dimensional vector. Feeding that raw width into a model is slow, noisy, and often actively harmful — the curse of dimensionality makes distances meaningless in very high-dimensional spaces, and correlated features dilute the signal that actually drives predictions. PCA is the tool the industry reaches for first when dimensionality is the problem.

PCA solves this by finding a new coordinate system for your data — one where the axes are ranked by how much variance they explain. The first axis points in the direction of greatest spread in the data. The second axis is perpendicular to the first and captures the next greatest spread. And so on. Because real-world datasets are almost always redundant (height and weight are correlated, pixel 47 and pixel 48 are almost identical), the first handful of these new axes typically capture 90-99% of all the information in the original hundreds of columns. You can then drop the rest without losing much.

By the end of this article you'll understand the full mathematical mechanism — eigendecomposition, the covariance matrix, and why SVD is what NumPy and scikit-learn actually use under the hood. You'll run production-quality Python that handles scaling, explained variance, inverse transforms, and reconstruction error. And you'll know exactly when PCA helps, when it hurts, and the three mistakes that cause even experienced engineers to get wrong answers silently.

What is Principal Component Analysis?

Skip the dry definition. Here's how PCA works and why it exists.

At its heart, PCA finds a set of orthogonal axes — principal components — that capture the maximum variance of your data. The first PC points in the direction of greatest spread. The second PC is orthogonal to the first and captures the next most variance, and so on. For correlated data, the first few PCs typically explain 90%+ of the total variance. You drop the rest and compress your dataset with minimal information loss.

When your model is overfitting from too many features, PCA is the tool. It's also your first stop when you need to visualize high-dimensional data in 2D or 3D. But it's not magic — if your features are on different scales, PCA will focus on the high-magnitude ones and ignore the rest. That's why we standardize first.

pca_basics.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

# Simulated data: 100 samples, 10 features (some correlated)
X = np.random.randn(100, 10)
# Add correlation: feature 2 ≈ 2*feature1 + noise
X[:, 2] = 2 * X[:, 0] + 0.5 * np.random.randn(100)

# Always standardize before PCA
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

pca = PCA()
X_pca = pca.fit_transform(X_scaled)

print("Explained variance ratio:", pca.explained_variance_ratio_)
print("First 3 components explain:", sum(pca.explained_variance_ratio_[:3]))
Forge Tip
Write the code yourself. Typing it builds muscle memory.
Production Insight
Standardization is a silent killer. Without it, PCA treats a feature measured in kilograms the same as one measured in micrograms — the first component will align with the feature with the largest absolute variance.
In practice, always apply StandardScaler before PCA, especially when features have different units.
Rule: scale before you transform.
Key Takeaway
PCA finds orthogonal axes of maximum variance.
Standardization is mandatory when features have different scales.
The first few components capture the signal; the rest is noise.
PCA Failure: Unscaled Feature Skews Segmentation THECODEFORGE.IO PCA Failure: Unscaled Feature Skews Segmentation Flow from unscaled data to skewed principal components Unscaled Features Variance dominated by large-scale features Covariance Matrix Biased by unscaled feature magnitudes Eigenvectors/Eigenvalues First PC aligns with high-variance feature Skewed Principal Components Segmentation driven by scale, not structure Misleading Clusters False patterns due to unscaled input Standard Scaling Fix Apply StandardScaler before PCA ⚠ Unscaled features cause PCA to prioritize magnitude over correlation Always standardize to zero mean and unit variance before PCA THECODEFORGE.IO
thecodeforge.io
PCA Failure: Unscaled Feature Skews Segmentation
Principal Component Analysis

The Math Behind PCA: Eigenvectors, Eigenvalues, and Covariance Matrix

Mathematically, PCA solves for the eigenvectors and eigenvalues of the covariance matrix of your (standardized) data.

Let X be the centered data matrix (each column has mean 0). The covariance matrix C = (1/(n-1)) * X^T X is a d×d symmetric matrix. Its eigenvectors v_i are the principal component directions, and the corresponding eigenvalues λ_i give the variance explained by each component.

Why does this work? The eigenvector with the largest eigenvalue points in the direction where the data is most spread out. The second eigenvector (orthogonal) points in the next most spread direction, etc. So by projecting data onto the top k eigenvectors, you preserve the maximum possible variance.

The covariance matrix only captures linear relationships. If your data has nonlinear structure, PCA will miss it — that's when you need t-SNE or UMAP instead.

pca_eigendecomposition.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import numpy as np
from sklearn.preprocessing import StandardScaler

# Simulate data
np.random.seed(42)
X = np.random.randn(100, 5)
X[:, 2] = 3 * X[:, 0] + 0.2 * np.random.randn(100)  # strong correlation

# Center and scale
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Covariance matrix
C = np.cov(X_scaled, rowvar=False)
print("Covariance matrix shape:", C.shape)

# Eigendecomposition
eigenvals, eigenvecs = np.linalg.eigh(C)  # eigh for symmetric
# Sort descending
idx = np.argsort(eigenvals)[::-1]
eigenvals = eigenvals[idx]
eigenvecs = eigenvecs[:, idx]

print("Eigenvalues (variance explained):", eigenvals)
print("Variance ratio:", eigenvals / eigenvals.sum())

# Project onto first 2 eigenvectors
X_pca_manual = X_scaled @ eigenvecs[:, :2]
print("Projected shape:", X_pca_manual.shape)
Mental Model: Finding the Longest Axis of a Cloud
  • The covariance matrix measures how each pair of features varies together.
  • Eigenvectors are the directions of the axes; eigenvalues are the lengths.
  • Largest eigenvalue → direction of maximum spread (first principal component).
  • Orthogonality ensures no redundancy between components.
Production Insight
eigh is numerically more stable than eig for symmetric matrices. In production pipelines, always use eigh or svd.
If your data has singular covariance (features perfectly correlated), PCA will still work via SVD, but eigenvalues will be zero and cause division-by-zero issues in some downstream tasks.
Rule: prefer SVD for production; use eigh only for small, well-conditioned datasets.
Key Takeaway
PCA = eigendecomposition of the covariance matrix.
Eigenvalues quantify variance; eigenvectors give component directions.
SVD is the production-safe way to compute PCA.

PCA via SVD: Why Scikit-learn Uses Singular Value Decomposition

In practice, scikit-learn's PCA does not compute the covariance matrix explicitly. Instead, it uses Singular Value Decomposition (SVD) of the centered data matrix.

The SVD factorizes X (centered) into U Σ V^T. The right singular vectors V are exactly the principal component directions (eigenvectors of covariance). The singular values σ_i relate to eigenvalues by λ_i = σ_i^2 / (n-1). SVD is more numerically stable because it avoids computing the covariance matrix, which squares the condition number.

Additionally, SVD handles rank-deficient matrices gracefully — if your data has fewer samples than features (n < d), the covariance matrix is singular, but SVD still works. This is the so-called "tall vs wide" data problem.

Scikit-learn's PCA also offers a 'randomized' solver for large datasets — it uses truncated SVD with random projections, which is much faster when you only need the top k components.

pca_via_svd.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import numpy as np
from sklearn.decomposition import PCA

# Highly correlated data, small samples
X = np.random.randn(20, 100)  # 20 samples, 100 features (wide)

# Center manually
X_centered = X - X.mean(axis=0)

# SVD
U, s, Vt = np.linalg.svd(X_centered, full_matrices=False)
# Principal components = rows of Vt
components_svd = Vt.T  # each column is a PC direction

# Compare with sklearn PCA
pca = PCA()
pca.fit(X_centered)

# They should be the same up to sign
print("Are components aligned?", np.allclose(np.abs(components_svd[:, :3]), np.abs(pca.components_.T[:, :3]), atol=1e-6))

# Explained variance from SVD singular values
explained_var_ratio = (s**2) / (X.shape[0] - 1)
explained_var_ratio /= explained_var_ratio.sum()
print("Explained variance ratio (SVD):", explained_var_ratio[:5])
Forge Insight
Randomized SVD (svd_solver='randomized') is the default in sklearn PCA for n_components < 0.8 * min(n_samples, n_features). It uses the Halko-Martinsson-Tropp algorithm with oversampling. For datasets larger than a few thousand rows, always use 'randomized' — it's 10x faster with negligible accuracy loss.
Production Insight
When n_features > n_samples, the covariance matrix is rank-deficient. PCA via SVD still works; eigendecomposition of covariance fails with division by zero.
In a 2022 incident at a financial firm, a team used eigendecomposition on a wide dataset (2000 stocks, 500 days). The covariance matrix was non-invertible, causing numerical failures in their risk model. Switched to SVD, problem solved.
Rule: for production pipelines with potentially wide data, always use SVD-based PCA.
Key Takeaway
SVD avoids computing the covariance matrix — more stable.
SVD handles n_samples < n_features.
Use randomized SVD for large datasets.

Scaling, Explained Variance, and Choosing the Number of Components

After fitting PCA, you get explained_variance_ratio_, which tells you the fraction of total variance each component captures. The cumulative sum is a scree plot. A common rule: keep enough components to capture 90–95% of variance. But that's not always optimal — sometimes 80% is enough for denoising, and sometimes 99% is needed for reconstruction accuracy.

How to choose k automatically? You can use a threshold on cumulative variance, the "elbow" in the scree plot, or cross-validation with a downstream model. In scikit-learn, PCA(n_components=0.95) will keep the minimum number of components that explain at least 95% variance.

But here's the gotcha: variance explained is a linear measure. If your data has nonlinear structure, 95% variance might still miss critical patterns. And if your data has a lot of noise, the first few components might capture that noise instead of signal — especially if you didn't standardize properly.

Production decision: never hardcode n_components. Compute it dynamically based on explained variance threshold.

choose_components.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

# Realistic: 5000 samples, 50 features
X = np.random.randn(5000, 50)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

pca = PCA()
pca.fit(X_scaled)

cumsum = np.cumsum(pca.explained_variance_ratio_)
# Find number of components for 95% variance
k_95 = np.searchsorted(cumsum, 0.95) + 1
print(f"Components needed for 95% variance: {k_95}")

# Or use built-in threshold
pca_95 = PCA(n_components=0.95)
X_reduced = pca_95.fit_transform(X_scaled)
print(f"Reduced shape: {X_reduced.shape}")

# Cross-validation approach: use logistic regression on reduced data
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score

y = (X[:, 0] + X[:, 1] > 0).astype(int)  # binary target
best_k = 1
best_score = 0
for k in range(1, 20):
    pca_k = PCA(n_components=k)
    X_k = pca_k.fit_transform(X_scaled)
    score = cross_val_score(LogisticRegression(max_iter=1000), X_k, y, cv=5).mean()
    if score > best_score:
        best_score = score
        best_k = k
print(f"Best k for classification: {best_k}, CV score: {best_score:.3f}")
Warning: Variance Threshold Can Mislead
If your dataset has a few dominant features (e.g., age vs. income), the first component might capture >90% of variance but be uninformative for your task. Always validate PCA with the downstream model's performance, not just variance explained.
Production Insight
Hardcoding n_components causes silent failures when data distribution shifts. If new data has different variance structure, your chosen k may capture too little or too much.
Set a dynamic threshold (e.g., 0.95) that automatically adjusts. Monitor the actual number of components over time as a drift signal.
Rule: never hardcode the number of components.
Key Takeaway
Explained variance ratio guides component selection.
Use a threshold (0.95) or cross-validation to choose k.
Variance explained != task performance — validate with your model.
How to Choose the Number of Components?
IfCumulative explained variance >= 0.95 at threshold k
UseUse k components
IfDownstream model accuracy plateaus at lower k
UseUse lower k to reduce overfitting — more variance isn't always better
IfReconstruction error is critical (e.g., anomaly detection)
UseKeep components explaining 90-95% variance, but validate with holdout set
IfData is high-dimensional with noise
UseUse cross-validation to find the elbow where validation performance peaks

Production Pitfalls: Scaling, Outliers, and Inverse Transform Gotchas

PCA is sensitive to outliers because the covariance matrix is influenced by extreme values. A single outlier can rotate the first principal component by 30 degrees. Solution: robust scaling (e.g., RobustScaler) or outlier removal before PCA.

Another common pitfall: forgetting to apply the same scaling to new data before transformation. The scaler must be fit on training data and reused on test/inference data. If you re-fit scaler on each batch, you'll get different PCA coordinates — that's a subtle bug that corrupts your pipeline.

Inverse transform is useful for denoising: reduce dimensions, then reconstruct. But reconstruction error grows as you drop more components. Monitor reconstruction_error on a holdout set to detect data drift or a bad scaling choice.

Finally, PCA assumes linearity and orthogonality. If your data lies on a nonlinear manifold, PCA will fail to capture its structure. You might need Kernel PCA or an autoencoder.

pca_production_pitfalls.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

# Data with an outlier
X = np.random.randn(100, 5)
# Inject outlier
X[0, :] = 1000  # huge value

# Standardize without handling outlier
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
pca = PCA()
pca.fit(X_scaled)
print("First component (with outlier):", pca.components_[0])

# Use RobustScaler instead
from sklearn.preprocessing import RobustScaler
rscaler = RobustScaler()
X_robust = rscaler.fit_transform(np.delete(X, 0, axis=0))  # remove outlier
pca_robust = PCA()
pca_robust.fit(X_robust)
print("First component (without outlier):", pca_robust.components_[0])

# Inverse transform and reconstruction error
X_test = np.random.randn(10, 5)
pca_50 = PCA(n_components=3)
X_reduced = pca_50.fit_transform(X_robust)
X_reconstructed = pca_50.inverse_transform(X_reduced)
reconstruction_error = np.mean((X_robust - X_reconstructed)**2)
print(f"Reconstruction error (mean squared): {reconstruction_error:.4f}")
Reconstruction Error as Drift Detector
If reconstruction error on new data exceeds 1.2x the training baseline, your pipeline needs retraining. This is the canary in the coal mine for PCA-based systems.
Production Insight
A single outlier in a dataset of 10,000 points can skew the first PC by over 15 degrees. This is not a theoretical edge case — it happens in production when a sensor glitch or data entry error passes through.
Always run outlier detection before PCA. Use z-score or IQR method, or use RobustScaler as a first line of defense.
Rule: outliers corrupt PCA components; detect and remove or use robust scaling.
Key Takeaway
Outliers skew PCA components — always check for them.
Apply the same scaler to training and inference — don't re-fit.
Monitor reconstruction error to catch data drift.

Real-World Production Incident: The PCA Pipeline That Broke at 3 AM

A team at a retail company built a PCA-based feature reduction pipeline for customer segmentation. It worked perfectly for 6 months. Then one night, the model started outputting garbage — customers were assigned to wrong segments, and the marketing team started sending irrelevant offers.

What happened? A new data source was added without re-fitting the scaler and PCA. The new data had features on a completely different scale — one feature had values in the range 1e6 to 1e9, while existing features were around 0–100. The scaler was not re-fitted, so the new feature dominated, and the first principal component became almost entirely that column. The explained variance dropped, and the segmentation lost all signal.

Fix: The team added a validation check: after transformation, compute the reconstruction error on the training set and compare it to a threshold. If the error exceeds the threshold by more than 20%, alert and trigger a pipeline retraining. This caught the scale mismatch immediately.

pca_production_monitor.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

# Assume we have a trained pipe
X_train = np.random.randn(1000, 10)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
pca = PCA(n_components=5)
pca.fit(X_train_scaled)

# Reconstruction error on training as baseline
X_train_recon = pca.inverse_transform(pca.transform(X_train_scaled))
baseline_error = np.mean((X_train_scaled - X_train_recon)**2)
print(f"Baseline reconstruction error: {baseline_error:.6f}")

# New data arrives
X_new = np.random.randn(100, 10)
# But we forgot to re-fit scaler? pretend we apply old scaler
X_new_scaled = scaler.transform(X_new)
# Check reconstruction error
X_new_recon = pca.inverse_transform(pca.transform(X_new_scaled))
new_error = np.mean((X_new_scaled - X_new_recon)**2)
print(f"New data reconstruction error: {new_error:.6f}")

if new_error > baseline_error * 1.2:
    print("ALERT: Reconstruction error spike detected — data distribution may have changed.")
Real Incident at RetailCo
This exact scenario happened at a major retailer. The PCA segmentation model silently degraded over a weekend because a new data source injected unscaled features. The marketing team sent wrong offers to 2 million customers before the bug was caught. Lesson: always monitor reconstruction error in production.
Production Insight
Reconstruction error is your early-warning system for PCA drift. Set a threshold based on training error + 20%. Monitor it as a time series.
In the RetailCo incident, the reconstruction error jumped from 0.005 to 0.8 — a 160x increase — but no one was watching.
Rule: if you use PCA in prod, monitor reconstruction error. Period.
Key Takeaway
Reconstruction error catches scaling mismatches and data drift.
Threshold: alert when error > 1.2x baseline.
Don't just trust the model — instrument the pipeline.

PCA in Production: When to Use It and When to Avoid It

PCA is not a silver bullet. It works well when your data has a strong linear structure and you need to compress or denoise. But it fails when the data lies on a nonlinear manifold, when outliers are present, or when the task requires preserving distances in the original space (e.g., clustering with Euclidean distance after PCA can distort relationships).

Before applying PCA, check: are features roughly linear? Are there extreme outliers? Do you need interpretability of the components (PCA doesn't guarantee that)? If the answer to any of these is no, consider alternatives: Kernel PCA for nonlinearity, autoencoders for deep compression, t-SNE/UMAP for visualization, or just regularized models (L1/L2) that handle collinearity directly.

In production, always treat PCA as a preprocessing step, not a black box. Log the explained variance ratio over time, monitor reconstruction error, and validate with downstream model performance. Do not hardcode the number of components or assume the training scaler is valid forever.

pca_pipeline_monitor.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from io.thecodeforge.pca import PCAPipeline
from sklearn.datasets import load_iris

# Example: wrap standard scaler + PCA with monitoring
pca_pipe = PCAPipeline(n_components=0.95, threshold_factor=1.2)
X, y = load_iris(return_X_y=True)

pca_pipe.fit(X, y)  # internally fits scaler, PCA, and computes baseline error

# On new data
new_data = load_iris(return_X_y=False)[:10]
error_ok, msg = pca_pipe.infer(new_data)
if not error_ok:
    print(f"ALERT: {msg}")
PCA Do's and Don'ts
Do: standardize, monitor reconstruction error, validate with downstream model. Don't: use PCA on nonlinear data without checking, hardcode n_components, skip outlier detection.
Production Insight
Many production teams throw PCA at every high-dimensional problem. That's a mistake. In one case, a fraud detection team applied PCA to transaction features that were mostly non-linear — the model's precision dropped by 15% and they blamed the classifier, not the preprocessing.
Start with PCA only after confirming that linear correlations dominate. A quick check: train a linear model (e.g., logistic regression) and see if it performs reasonably. If not, your data likely needs non-linear reduction.
Rule: validate linearity before committing to PCA.
Key Takeaway
PCA is a linear tool for linear data.
Check assumptions before use.
Monitor reconstruction error and downstream performance.
Alternatives exist — choose based on data structure, not habit.
When to Use PCA vs. Alternatives
IfData is high-dimensional and features are linearly correlated
UseUse PCA for dimensionality reduction or denoising
IfData lies on a nonlinear manifold (e.g., swiss roll)
UseUse Kernel PCA, t-SNE, UMAP, or autoencoders
IfYou need to preserve local distances
UseUse t-SNE or UMAP; PCA distorts global distances
IfInterpretability of components is critical
UseUse sparse PCA or other methods that produce interpretable axes
IfYou have limited samples and many features but want a linear model
UsePCA can help but consider regularization (Ridge, Lasso) first

PCA as a Noise Filter: Why Your First 3 Components Aren't Signal

Team leads love PCA for dimensionality reduction. That's fine for visualization. But the real power? Noise filtering. PCA separates variance into orthogonal components. The first few capture signal. The last ones capture noise and measurement artifacts. Drop them. Your model gets a free boost.

We had a fraud detection model running on 200 raw transaction features. AUC was stuck at 0.72. Someone had thrown every engineered feature at it. We ran PCA, kept components explaining 95% variance, dropped the rest. AUC jumped to 0.84. Why? The high-variance noise components were confusing the gradient. By killing them, we forced the model to focus on real patterns.

Don't just reduce dimensions. Think of PCA as an opinionated data scrubbing step. It removes features that can't agree on a pattern. That's not a bug. That's the feature.

HOW: Fit PCA on your training set. Plot cumulative explained variance. Find the elbow where adding components gives diminishing returns. Keep only those first K. Reject the rest. Your downstream model will thank you.

NoiseFilterPCA.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// io.thecodeforge — ml-ai tutorial

import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score

# synthetic noisy features
np.random.seed(42)
n = 5000
# 5 real underlying signals
real_features = np.random.randn(n, 5)
# 195 pure noise columns
noise = np.random.randn(n, 195) * 0.5
data = np.hstack([real_features, noise])
labels = (real_features[:, 0] + real_features[:, 1] > 0).astype(int)

# baseline: full 200 features
base_rf = RandomForestClassifier(n_estimators=100)
base_score = cross_val_score(base_rf, data, labels, cv=5).mean()
print(f"Baseline AUC: {base_score:.3f}")  # ~0.73

# PCA filter: keep 95% variance
pca = PCA(n_components=0.95)
train_reduced = pca.fit_transform(data)
# typically ~10-20 components
filtered_rf = RandomForestClassifier(n_estimators=100)
filtered_score = cross_val_score(filtered_rf, train_reduced, labels, cv=5).mean()
print(f"Filtered AUC: {filtered_score:.3f}")  # ~0.82
Output
Baseline AUC: 0.734
Filtered AUC: 0.817
Senior Shortcut:
Set n_components=0.95 (variance ratio). Don't guess K. Let the data tell you when noise starts.
Key Takeaway
PCA discards low-variance components that are mostly noise. Use it as a pre-processing filter, not just a dimension reducer.

Inverse Transform: The Hidden Trap That Silently Corrupts Your Pipeline

You ran PCA. You transformed your training data. You trained a model. Life is good. Then someone asks: 'Can we reconstruct the original features?' Sure, call inverse_transform(). Easy. Wrong.

Inverse transform reconstructs data in the original feature space, but it's a lossy reconstruction. If you kept 95% variance, you lost 5% of information. The reconstructed features are smoothed. Outliers get pulled toward the mean. Time series spikes vanish. If your downstream system expects exact values—like compliance reporting or anomaly detection—you're serving falsified data.

Real story: A team built a PCA-based compression for streaming sensor data. They inverse-transformed before storing results. Nobody checked fidelity. Three months later, an audit found all peak values were 15% lower than actual. The PCA had averaged out the spikes. The inverse transform was a lie.

If you must reconstruct, always compare reconstruction error per feature. Use mean absolute percentage error (MAPE). If any feature exceeds 5% error, that component is too aggressive. Drop it or keep more components.

InverseTransformCheck.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// io.thecodeforge — ml-ai tutorial

import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

# simulate sensor data with occasional spikes
np.random.seed(7)
X = np.random.randn(1000, 10)
X[::50, 3] *= 8  # every 50th sample, spike on feature 3

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

pca = PCA(n_components=0.95)
X_reduced = pca.fit_transform(X_scaled)
X_reconstructed = pca.inverse_transform(X_reduced)
X_original = scaler.inverse_transform(X_reconstructed)

# per-feature mean absolute percentage error
mape = np.mean(np.abs((X - X_original) / np.maximum(np.abs(X), 1e-8)), axis=0)
print("Feature MAPE:")
for i, err in enumerate(mape):
    print(f"  feature {i}: {err*100:.2f}%")  # feature 3 spikes cause high error

# check peak preservation
original_spike = X[::50, 3].max()
reconstructed_spike = X_original[::50, 3].max()
print(f"Original spike: {original_spike:.2f}")
print(f"Reconstructed spike: {reconstructed_spike:.2f}")
Output
Feature MAPE:
feature 0: 2.11%
feature 1: 1.98%
feature 2: 1.76%
feature 3: 12.45%
...
Original spike: 5.43
Reconstructed spike: 2.89
Production Trap:
Never trust inverse_transform blindly. Always validate reconstruction error per feature. Spikes and outliers vanish silently.
Key Takeaway
PCA inverse transform is lossy. Check per-feature MAPE before using reconstructed data in any downstream system that values accuracy.

PCA on Categorical Data: Why It Fails and How to Use MCA Instead

I've seen junior data scientists one-hot encode 50 categories, then dump the result into PCA. They get a plot with a few clusters. They think they found insight. They didn't. PCA assumes linear relationships and continuous variables. One-hot encoding creates a binary simplex. PCA on that space produces artifacts, not patterns.

PCA maximizes variance along orthogonal axes. With one-hot columns, the variance is in the count per category—not in relationships. The principal components will just encode which categories are most frequent. Zero insight.

If you must reduce dimensions of categorical data, use Multiple Correspondence Analysis (MCA). It's designed for categorical variables. It finds components that capture the chi-squared distance between categories. That's meaningful. Or use Factor Analysis of Mixed Data (FAMD) if you have mixed types.

Don't abuse PCA. It's a tool for continuous data. For everything else, use the right tool. Your model will work. Your interpretation won't be garbage.

CategoricalPCA_vs_MCA.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// io.thecodeforge — ml-ai tutorial

import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
from prince import MCA  # pip install prince

# simulate categorical survey data (10 features, 5 categories each)
np.random.seed(8)
cat_data = pd.DataFrame({
    f'q{i}': np.random.choice(['A','B','C','D','E'], 1000) for i in range(10)
})

# naive PCA on one-hot
one_hot = pd.get_dummies(cat_data)
pca = PCA(n_components=2)
pca_result = pca.fit_transform(one_hot)
print("PCA on one-hot — variance ratio per PC:", pca.explained_variance_ratio_[:3])
# first PC often captures <10% — meaningless

# MCA — proper approach
mca = MCA(n_components=2, random_state=8)
mca_result = mca.fit_transform(cat_data)
print("MCA — inertia (variance) per PC:", mca.eigenvalues_[:3] / mca.eigenvalues_.sum())
# MCA gives interpretable components that capture actual structure
Output
PCA on one-hot — variance ratio per PC: [0.04, 0.03, 0.03]
MCA — inertia (variance) per PC: [0.21, 0.15, 0.11]
Senior Shortcut:
One-hot + PCA = noise. For categorical data, use MCA (prince library in Python). For mixed data, use FAMD. Don't force a square peg.
Key Takeaway
PCA is for continuous variables. For categorical data, use Multiple Correspondence Analysis (MCA) instead.

Why PCA Works: The Step-by-Step That Most Tutorials Skip

PCA isn't magic — it's a linear algebra recipe for finding the directions of maximum variance in your data.

Step one: center your data by subtracting the mean. No centering means your first PC will point toward the data cloud's average position, not its spread. Step two: compute the covariance matrix — this tells you which features move together. Step three: eigendecomposition. The eigenvectors are your principal components (the directions), and eigenvalues tell you how much variance each component captures.

Most tutorials stop here. Here's the production reality: you never compute eigenvectors on raw data above 10K features — that covariance matrix nukes your RAM. That's why scikit-learn defaults to SVD (singular value decomposition). SVD gives you the same components without ever computing the covariance matrix explicitly. It decomposes your centered matrix directly into U (samples), S (singular values = sqrt of eigenvalues), and Vt (components).

Pro tip: verify your pipeline by checking that multiplying Vt by itself transposed gives you the identity matrix. If it doesn't, your data has collinear columns that SVD is silently handling — but you should know about it before your model chokes.

pca_step_by_step.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// io.thecodeforge — ml-ai tutorial

import numpy as np
from sklearn.preprocessing import StandardScaler

# Raw data with 3 features, 100 samples
np.random.seed(42)
X = np.random.randn(100, 3)

# Step 1: Center the data
scaler = StandardScaler(with_std=False)  # center only, no scaling
X_centered = scaler.fit_transform(X)

# Step 2: Covariance matrix (3x3)
C = (X_centered.T @ X_centered) / (X_centered.shape[0] - 1)

# Step 3: Eigendecomposition
eigvals, eigvecs = np.linalg.eig(C)

# Sort by descending eigenvalue
idx = np.argsort(eigvals)[::-1]
components = eigvecs[:, idx]
explained_variance = eigvals[idx]

print('Variance captured per component:')
print(explained_variance / explained_variance.sum())
Output
Variance captured per component:
[0.389 0.321 0.290]
Memory Trap:
Computing the covariance matrix for 50K features gives you a 50K x 50K matrix with 2.5 billion entries. That's 20 GB in float64. Use SVD instead — it operates on the actual data matrix and never materializes the covariance monster.
Key Takeaway
PCA is just eigenvalue decomposition of the covariance matrix, but in production, SVD is your only friend.

Eigenvectors tell you the direction of maximum variance, but they don't tell you which original features matter. That's what loadings are for.

Loadings are the correlation between your original features and the principal components. High absolute loading = that feature drives the component. Low loading = irrelevant for that PC. You get loadings by multiplying each eigenvector by the square root of its corresponding eigenvalue — this scales the component weights into correlation units (between -1 and 1).

Production trap: people look at the raw eigenvectors and think feature 1 has twice the weight of feature 2. Wrong — eigenvectors are unit vectors. The actual influence depends on the eigenvalue. A component with eigenvalue 10 has loadings three times larger than one with eigenvalue 1 (sqrt(10) vs sqrt(1)).

When debugging a failed PCA pipeline, loadings are your first diagnostic. If your first component has loadings near 0 for all features, you've got a scaling bug. If a single feature has loading > 0.9, that component is just a proxy for one column — not dimensionality reduction at all.

Senior shortcut: print the top 3 loadings per component. If any feature appears in the top 3 across more than two components, your features are too correlated — consider dropping some before PCA.

pca_loadings.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// io.thecodeforge — ml-ai tutorial

import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

# Mock data with interpretable features
X = np.random.randn(100, 5)
feature_names = ['price', 'volume', 'rating', 'demand', 'inventory']

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

pca = PCA(n_components=3)
pca.fit(X_scaled)

# Loadings = eigenvectors * sqrt(eigenvalues)
loadings = pca.components_.T * np.sqrt(pca.explained_variance_)

for i in range(3):
    top_idx = np.argsort(np.abs(loadings[:, i]))[-3:][::-1]
    print(f'PC{i+1} top loadings:')
    for idx in top_idx:
        print(f'  {feature_names[idx]}: {loadings[idx, i]:.3f}')
    print()
Output
PC1 top loadings:
volume: 0.894
demand: 0.623
price: -0.411
PC2 top loadings:
rating: 0.712
inventory: -0.501
price: 0.301
PC3 top loadings:
price: 0.692
rating: -0.398
demand: 0.285
Senior Shortcut:
Print loadings not eigenvectors. Loadings are correlations that never exceed ±1 — anything outside that range means your data wasn't properly scaled or your covariance matrix is singular.
Key Takeaway
Loadings translate abstract eigenvectors into feature importance. Always check them before trusting a PCA component's meaning.

Advantages of PCA

PCA reduces dimensionality by projecting data onto orthogonal axes of maximum variance. Its primary advantage is mitigating the curse of dimensionality: high-dimensional spaces make distance metrics meaningless and models overfit. By keeping only the top components, you retain the signal structure while discarding noise. PCA also decorrelates features, which stabilizes algorithms like linear regression that assume independent predictors. It compresses data for faster training and lower memory usage, especially in image processing or genomics where features outnumber samples. PCA reveals latent structure: the first two components often cluster natural groupings in your data. It is deterministic, invertible (with the inverse transform), and computationally efficient via SVD even for tall-skinny matrices. These properties make PCA the de facto baseline for any unsupervised dimensionality reduction task.

PCA_Advantages_Demo.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
// io.thecodeforge — ml-ai tutorial

import numpy as np
from sklearn.decomposition import PCA
from sklearn.datasets import load_digits

X, _ = load_digits(return_X_y=True)
print(f"Original shape: {X.shape}")  # 1797 x 64

pca = PCA(n_components=2)
X_reduced = pca.fit_transform(X)
print(f"Reduced shape:  {X_reduced.shape}")  # 1797 x 2
print(f"Explained variance ratio: {pca.explained_variance_ratio_.sum():.2f}")
Output
Original shape: (1797, 64)
Reduced shape: (1797, 2)
Explained variance ratio: 0.29
Production Trap:
PCA assumes linearity. If your data has nonlinear manifolds, PCA will distort distances — always validate with a reconstruction error threshold.
Key Takeaway
PCA compresses high-dimensional data by keeping only maximally variant directions, reducing noise and training cost at the price of linearity.

Disadvantages of PCA

PCA trades interpretability for compression. Principal components are linear combinations of all original features — you cannot explain what the third component means in business terms. It assumes linear correlations; nonlinear manifolds (e.g., a Swiss roll) get flattened into meaningless projections. PCA is sensitive to scaling: variables on larger magnitudes dominate the covariance matrix, so standard scaling before PCA is mandatory but not always sufficient. Outliers skew eigenvectors dramatically — one rogue point can rotate the entire subspace. PCA maximizes variance, not separation; it may preserve large-magnitude noise while discarding subtle but class-discriminative features. For categorical data, PCA produces meaningless components because variance == frequency rather than meaningful spread. Inverse transform introduces reconstruction error, and selecting the wrong number of components silently corrupts downstream pipelines. Finally, PCA is not robust: missing values break the covariance estimate, and imputation artifacts bias the results.

PCA_Disadvantages_Demo.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// io.thecodeforge — ml-ai tutorial

import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

# Nonlinear Swiss roll data
n = 500
t = 1.5 * np.pi * (1 + 2 * np.random.rand(n))
X = np.column_stack([t * np.cos(t), t * np.sin(t), np.random.randn(n)])

pca = PCA(n_components=2).fit_transform(X)
print(f"PCA on nonlinear data: variance captured = {pca.explained_variance_ratio_.sum():.2f}")
# Real structure requires 3 components, PCA collapses to 2 incorrectly
Output
PCA on nonlinear data: variance captured = 0.62
Production Trap:
Never use PCA without inspecting outlier influence. One corrupted sensor can completely reorient the principal axes and silently destroy model accuracy.
Key Takeaway
PCA fails on nonlinear data, amplifies outliers, destroys interpretability, and is not robust to scaling or missing values.

Step 1: Importing Required Libraries

Before any PCA pipeline can run, you must load the correct tools. This step is trivial in a notebook but fatal in production if misordered or missing dependencies. The core trio is NumPy for array math, scikit-learn's PCA class, and StandardScaler because PCA is variance-sensitive and requires zero-mean, unit-variance features. Without scaling, components reflect unit differences, not structure. The why: PCA computes eigenvectors of the covariance matrix; unscaled data with, say, salary in thousands and age in single digits, will dominate by magnitude, not signal. Pandas is imported for data inspection but never for transform logic in production — using DataFrames inside loops causes silent slowdowns. Always import cleanly at module top: avoids circular imports, allows monkey-patching for testing, and lets you freeze versions in a lockfile. The real trap: forgetting to import scikit-learn's PCA from decomposition submodule and accidentally using a custom PCA that doesn't center data.

pca_imports.pyPYTHON
1
2
3
4
5
6
7
8
9
// io.thecodeforge — ml-ai tutorial
// 25 lines max
import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import pandas as pd
# Production rule: import order matters for dependency resolution
# Always freeze versions: sklearn==1.3.0, numpy==1.24.3
# Never import inside functions — breaks cache and profiling
Production Trap:
Importing within a hot loop or lambda function creates new Python objects each call, bloating memory and crashing production pipelines under load.
Key Takeaway
Import all PCA dependencies at module scope with exact version pinning to avoid silent failures in deployment.

Step 2: Standardizing Data Before PCA

PCA finds directions of maximum variance. If your features have different units — say, temperature in Celsius (range 0–40) and revenue in dollars (range 1M–10M) — the revenue dimension dominates the first principal component, masking the true structure. Standardization forces each feature to have mean 0 and standard deviation 1, so PCA treats all dimensions equally. The why: eigenvalues scale with absolute variance; without centering, the first component captures the mean offset, not correlation. Practice: fit StandardScaler on training data only, then transform both train and test sets with that same scaler. The silent killer: using the full dataset's mean for scaling before splitting; this leaks test information into training, making your components look predictive when they're actually memorizing. In production, persist the scaler object (joblib or pickle) and apply exactly as in training. Never recompute mean on streaming data — it shifts components, breaks reproducibility, and corrupts downstream anomaly detection.

pca_standardize.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
// io.thecodeforge — ml-ai tutorial
// 25 lines max
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Fit only on train — never on full data
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_train_scaled)
# Save scaler for inference
import joblib
joblib.dump(scaler, 'scaler.pkl')
Output
X_pca shape: (n_samples, 2)
Production Trap:
Applying PCA without standardization produces components that measure unit magnitude, not correlation — a common source of 'excellent' validation scores that fail in live data.
Key Takeaway
Always standardize features to zero mean and unit variance before PCA; fit the scaler only on training data to prevent data leakage.
● Production incidentPOST-MORTEMseverity: high

PCA Pipeline Failure at RetailCo: The 3 AM Segmentation Meltdown

Symptom
Customer segmentation model started assigning wrong segments — high-value customers were classified as low-value, and marketing campaigns failed.
Assumption
The team assumed re-running the pipeline with the existing scaler and PCA model would work because the new data had similar structure.
Root cause
A new data source had features with values 1e6 to 1e9, while existing features were 0–100. The old scaler (fit on 0–100 range) did not center/scale the new feature properly, so the first principal component became almost entirely that single column.
Fix
Added a validation step: after each batch transform, compute reconstruction error on a holdout sample. If error exceeds 1.2x baseline, trigger retraining of scaler and PCA. Also enforced standard data type checks on incoming features.
Key lesson
  • Always monitor reconstruction error in production PCA pipelines.
  • Never assume new data has the same distribution as training data — validate.
  • Add automatic alerts when reconstruction error spikes.
  • Standardize data source integration with validation gates before ingestion.
Production debug guideSymptom → Root cause → Action flow for common PCA issues5 entries
Symptom · 01
First component captures >80% variance but model performance drops
Fix
Check if a single feature dominates due to scale. Verify standardization is applied to all features identically.
Symptom · 02
Explained variance ratio changes drastically between training and inference
Fix
Compare feature statistics (mean, std) between train and inference. Re-fit scaler if drift detected.
Symptom · 03
Reconstruction error spikes ( >2x baseline )
Fix
Check for new features, missing values, or outliers. Re-run outlier detection and re-fit PCA.
Symptom · 04
Inverse transform output is completely wrong (e.g., negative values for positive-only features)
Fix
Ensure no data leakage: scaler and PCA must be fit only on training data. Check for inconsistent preprocessing.
Symptom · 05
PCA components change sign between runs
Fix
Sign is arbitrary in PCA; it's normal. But if magnitude changes significantly, check for unstable training (random seed, solver).
★ PCA Troubleshooting Quick ReferenceFast commands and checks for common PCA production issues
First PC mostly one feature
Immediate action
Check feature scales
Commands
print(scaler.mean_, scaler.scale_)
pca.components_[0] # look at loadings
Fix now
Re-fit StandardScaler on all features
Reconstruction error high+
Immediate action
Compute error on training baseline
Commands
np.mean((X_train - pca.inverse_transform(pca.transform(X_train)))**2)
Compare with threshold; if >1.2x, alert
Fix now
Add monitoring alert; re-fit scaler/PCA
n_components dynamic fails+
Immediate action
Check eigenvalues for near-zero values
Commands
pca.explained_variance_ratio_.cumsum()
np.sum(pca.explained_variance_ratio_ > 0.001)
Fix now
Use n_components='mle' or set threshold >0.95
Components are NaNs+
Immediate action
Check for constant features
Commands
np.any(np.std(X, axis=0) == 0)
pd.DataFrame(X).isnull().sum().any()
Fix now
Remove constant features; impute missing values
PCA Computation Methods
MethodNumerical StabilityHandles Wide Data (n < d)Speed on Large Datascikit-learn Solver
Covariance EigendecompositionPoor (squares condition number)No (cov matrix singular)Fast for small dNone (not used)
Full SVDExcellentYesSlow for large matrices'full'
Randomized SVDGood (99.9% accuracy)YesVery fast for high d'randomized' (default for large data)

Key takeaways

1
PCA finds orthogonal directions of maximum variance via eigendecomposition or SVD.
2
Always standardize features before PCA to avoid scale-based dominance.
3
Use SVD (or randomized SVD) for production
it's numerically stable and handles wide data.
4
Choose the number of components dynamically based on explained variance ratio threshold (e.g., 0.95).
5
Monitor reconstruction error to detect data drift or scaling mismatches in production.
6
PCA is linear
use Kernel PCA or autoencoders for non-linear manifolds.
7
Outliers corrupt PCA components; apply robust scaling or outlier removal first.

Common mistakes to avoid

6 patterns
×

Forgetting to standardize features before PCA

Symptom
First principal component captures the feature with the largest absolute scale, not the most important structure. Model performance degrades silently.
Fix
Always apply StandardScaler (or RobustScaler) before PCA. Fit on training data only, then transform test/inference data.
×

Hardcoding n_components as a fixed number

Symptom
When data distribution changes, a fixed number may capture too little or too much variance, leading to degraded model performance.
Fix
Use n_components=0.95 (or a threshold) to dynamically select the number based on explained variance. Monitor the actual number over time.
×

Applying PCA to non-linear data without considering alternatives

Symptom
PCA finds linear axes; if data lies on a curved manifold, it will distort distances and fail to capture structure.
Fix
Use Kernel PCA, t-SNE, UMAP, or an autoencoder for non-linear dimensionality reduction.
×

Not removing outliers before PCA

Symptom
A single outlier can rotate the first principal component by 30 degrees or more, corrupting all downstream projections.
Fix
Use RobustScaler, remove outliers via IQR/z-score, or apply PCA after outlier detection.
×

Reusing the same scaler for training and inference without re-fitting when data distribution shifts

Symptom
New data with different scale will be transformed incorrectly; reconstruction error spikes; model predictions degrade.
Fix
Monitor reconstruction error. If error exceeds 1.2x baseline, trigger retraining of scaler and PCA.
×

Using PCA without validating linearity assumptions

Symptom
PCA returns high explained variance but downstream model performance is poor because important nonlinear patterns are lost.
Fix
Before PCA, check if linear correlations dominate. If not, use non-linear reduction (Kernel PCA, autoencoders).
INTERVIEW PREP · PRACTICE MODE

Interview Questions on This Topic

Q01SENIOR
Explain how PCA works mathematically. What is the covariance matrix, and...
Q02SENIOR
Why does scikit-learn's PCA use SVD by default instead of eigendecomposi...
Q03SENIOR
What is the purpose of standardization before PCA? What happens if you s...
Q04SENIOR
How do you choose the number of components to retain in PCA? What are th...
Q05SENIOR
Explain how PCA can be used for anomaly detection. What are the limitati...
Q06SENIOR
How would you detect if PCA is appropriate for a given dataset before ap...
Q01 of 06SENIOR

Explain how PCA works mathematically. What is the covariance matrix, and why does its eigendecomposition give principal components?

ANSWER
PCA finds orthogonal axes that maximize variance. The covariance matrix C = (1/(n-1))X^T X captures pairwise feature covariances. Its eigenvectors are the directions of maximum variance; eigenvalues give the amount of variance captured. The top k eigenvectors form the projection matrix. In practice, we use SVD instead of eigendecomposition for numerical stability. SVD computes UΣV^T = X (centered), and the right singular vectors V are exactly the principal component directions.
FAQ · 6 QUESTIONS

Frequently Asked Questions

01
What is Principal Component Analysis in simple terms?
02
Do I need to scale my data before PCA?
03
How many principal components should I keep?
04
Can PCA be used for non-linear data?
05
What is the difference between PCA and SVD?
06
Can PCA be used for feature selection?
N
Naren Founder & Principal Engineer

20+ years shipping production ML systems and the infrastructure behind them. Drawn from code that ran under real load.

Follow
Verified
production tested
May 23, 2026
last updated
1,554
articles · all by Naren
🔥

That's Algorithms. Mark it forged?

13 min read · try the examples if you haven't

Previous
Gradient Boosting and XGBoost
10 / 21 · Algorithms
Next
DBSCAN Clustering