
PCA Isn't Feature Selection: High Variance Doesn't Mean Predictive
- كاتب موثّق
- scikit-learn 1.9
محتويات المقال
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
"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.
What actually happens
X. The target never enters.
PCA never sees the target
Variance comes from correlation and units, not relevance
The quiet direction can be the one that matters
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.517import numpy as npfrom sklearn.feature_selection import SelectKBest, f_classiffrom 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)sensors = groups + 0.1 * rng.normal(size=(n, 12))vibration = rng.normal(size=(n, 1))fails = vibration[:, 0] + 0.5 * rng.normal(size=n) > 0X = np.hstack([sensors, vibration])model = make_pipeline(StandardScaler(), SelectKBest(f_classif, k=4), LogisticRegression()) # same budget: 4 inputsscore = cross_val_score(model, X, fails, cv=5).mean()kept = model.fit(X, fails)[1].get_support(indices=True)print(f"Kept columns {kept}, accuracy {score:.3f}")# Kept columns [ 9 10 11 12], accuracy 0.844PCA isn't selection at all
Where PCA earns its place
PLSRegression) is its supervised cousin.
X | ||
How to apply it in your project
Ask why you want PCA For speed, memory, collinearity or a plot, PCA fits. To find the important features, use feature selection instead. Measure without PCA first Score the plain model in cross-validation. That's the any reduction has to match. Tune the number of components against the target Put the scaler and PCA inside the pipeline, and search n_components, including"passthrough", withGridSearchCV. A fixed "90% of variance" is a guess, not a measurement.Look at what you drop On training data, check how each component relates to the target before discarding it. A low-variance component can carry the signal. Remove columns with selection, not projection When the goal is fewer inputs in production, use SelectKBest,SelectFromModelorRFECV, or tryPLSRegressionfor 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.
📚 المصادر
Decomposing signals in components: Principal component analysis (PCA) — scikit-learn User Guide Principal Component Regression vs Partial Least Squares Regression — scikit-learn examples Cross decomposition (PLS) — scikit-learn User Guide Feature selection — scikit-learn User Guide Pipelines and composite estimators — scikit-learn User Guide
التعليقات (0)