الانتقال إلى المحتوى
Machine LearningUnsupervised Learning· 7 دقائق للقراءة

PCA Isn't Feature Selection: High Variance Doesn't Mean Predictive

PCA ranks directions by variance without looking at the target, and every component still needs every column.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
  • scikit-learn 1.9
محتويات المقال

A predictive-maintenance team has 13 readings per machine. Twelve are temperatures and pressures that move together in groups of three; one is a vibration sensor. Following a tutorial, they put PCA(n_components=0.9) in front of a logistic regression: "keep 90% of the information." PCA keeps four components, and the model predicts failures with an accuracy of 0.517, a coin flip. Vibration, the one reading that predicts failure, was in the 10% they threw away.

The common misconception

"PCA keeps the components that matter most for prediction, so it's a safe first step before any model." The belief has good roots:

  • "Principal" sounds like "most important", and "explained variance" sounds like "explained information".
  • scikit-learn and Google both suggest PCA before k-means on high-dimensional data, sensible advice for distance-based methods that gets stretched to every model.
  • On many datasets, the big directions do carry the signal, so the habit often works.

The half-truth: PCA really does keep the directions with the most variance, which is a reasonable way to compress data. But "varies the most" and "predicts the target" are different questions. They have the same answer only when the data happens to be arranged that way.

What actually happens

finds successive orthogonal directions that each capture as much of the remaining variance as possible, then keeps the first few. Every step of that is about X. The target never enters.

PCA never sees the target

scikit-learn's PCR vs PLS example puts it plainly: the PCA transformation is purely unsupervised, and no information about the target is used. Its user guide on cross decomposition adds the consequence: PCR, PCA followed by a regression, keeps the directions with the most variance, but directions with small variance may be the ones that predict the target.

Variance comes from correlation and units, not relevance

Three sensors that move together form one direction with about three times the variance of any lone column, so a correlated group outranks an independent reading, however predictive the reading is. Units matter too: scikit-learn's PCA centers each feature but doesn't scale it, so a weight in grams has a million times the variance of the same weight in kilograms. Without a scaler, the first component is often just the column with the biggest numbers.

The quiet direction can be the one that matters

In the PCR vs PLS example, the target is built from the low-variance direction of a two-dimensional dataset. PCR with one component scores an R² of −0.026 on test data, worse than predicting the mean. Partial least squares with one component, which picks its direction using the target, scores 0.658. PCR only recovers, with 0.673, when it's allowed to keep both components.

The scripts below rebuild the maintenance story: four groups of three correlated sensors, one independent vibration reading, and failures driven by vibration alone. Both scripts give the model four inputs.

Python
import numpy as npfrom sklearn.decomposition import PCAfrom sklearn.linear_model import LogisticRegressionfrom sklearn.model_selection import cross_val_scorefrom sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import StandardScalerrng = np.random.default_rng(0)n = 1000groups = rng.normal(size=(n, 4)).repeat(3, axis=1)  # 4 groups of 3 sensorssensors = groups + 0.1 * rng.normal(size=(n, 12))  # that move togethervibration = rng.normal(size=(n, 1))  # one quiet, independent readingfails = vibration[:, 0] + 0.5 * rng.normal(size=n) > 0X = np.hstack([sensors, vibration])model = make_pipeline(StandardScaler(), PCA(n_components=0.9),  # keep 90%                      LogisticRegression())score = cross_val_score(model, X, fails, cv=5).mean()kept = model.fit(X, fails)[1].n_components_print(f"PCA kept {kept} components, accuracy {score:.3f}")# PCA kept 4 components, accuracy 0.517

Even after scaling, each sensor group carries more variance than the vibration column, so a 90% cutoff keeps the four group directions and drops the one that matters. Accuracy: 0.517. with the same budget of four inputs asks the target which columns help. It keeps column 12, vibration, plus three sensors that fill the remaining slots, and reaches 0.844. A higher cutoff might have kept vibration on this data, but only by luck: the threshold is about variance, never about failures.

PCA isn't selection at all

Even when PCA works, it doesn't remove columns. scikit-learn's guide notes that its components are dense: each one has non-zero coefficients on all the original variables, which makes them hard to interpret. After PCA you still collect, clean and serve all 13 readings; you just can't name what the model uses anymore. Feature selection keeps a subset of real columns and lets you drop the rest from the pipeline.

Where PCA earns its place

PCA is a good tool for the jobs it was built for: compressing many correlated inputs to save time and memory, removing redundancy before distance-based methods like k-means, and drawing high-dimensional data in two dimensions. When the reduction should follow the target, partial least squares (PLSRegression) is its supervised cousin.

PCAFeature selection
Uses the targetNo, only the variance of XYes: tests, model weights or cross-validation
OutputNew columns, each a mix of all inputsA subset of the original columns
Production inputsStill needs every columnUnused columns can be dropped
InterpretationComponents are hard to nameColumns keep their meaning
Good forCompression, redundancy, visualizationRemoving irrelevant or costly inputs

How to apply it in your project

  1. Ask why you want PCAFor speed, memory, collinearity or a plot, PCA fits. To find the important features, use feature selection instead.
  2. Measure without PCA firstScore the plain model in cross-validation. That's the any reduction has to match.
  3. Tune the number of components against the targetPut the scaler and PCA inside the pipeline, and search n_components, including "passthrough", with GridSearchCV. A fixed "90% of variance" is a guess, not a measurement.
  4. Look at what you dropOn training data, check how each component relates to the target before discarding it. A low-variance component can carry the signal.
  5. Remove columns with selection, not projectionWhen the goal is fewer inputs in production, use SelectKBest, SelectFromModel or RFECV, or try PLSRegression for a supervised projection.

Key takeaways

  • PCA ranks directions by variance and never looks at the target; high variance isn't predictive power.
  • Correlated groups and large units inflate variance: here a 90% cutoff dropped the only useful signal, and accuracy fell to 0.517.
  • PCA doesn't select features: every component mixes all inputs, so production still needs every column.
  • Treat PCA as a hyperparameter tuned against the target, and use feature selection, which kept vibration and reached 0.844, when you want fewer columns.

📚 المصادر

  1. Decomposing signals in components: Principal component analysis (PCA) — scikit-learn User Guide
  2. Principal Component Regression vs Partial Least Squares Regression — scikit-learn examples
  3. Cross decomposition (PLS) — scikit-learn User Guide
  4. Feature selection — scikit-learn User Guide
  5. Pipelines and composite estimators — scikit-learn User Guide

التعليقات (0)