
More Features Can Make a Model Worse: Noise and the Curse of Dimensionality
- كاتب موثّق
- scikit-learn 1.9
محتويات المقال
The common misconception
A model can always ignore a useless feature by giving it a weight of zero, so extra columns look free. "More data is better" feels like it should apply to columns as well as rows. Some models really are fairly tolerant: tree ensembles and regularized linear models often shrug off a few weak features, which reinforces the instinct.
What actually happens
A model has to learn what to ignore
Distances add up every column
StandardScaler, every column counts the same, so with 30 real columns and 100 unrelated ones, most of each distance comes from columns that say nothing about the tumor. The "nearest" neighbors are increasingly chosen by noise.
import numpy as npfrom sklearn.datasets import load_breast_cancerfrom sklearn.model_selection import StratifiedKFold, cross_val_scorefrom sklearn.neighbors import KNeighborsClassifierfrom sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import StandardScalerX, y = load_breast_cancer(return_X_y=True) # 30 real measurementsextra = np.random.default_rng(0).normal(size=(len(X), 100)) # unrelatedX_all = np.hstack([X, extra]) # "the model will figure out what's useful"cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)knn = make_pipeline(StandardScaler(), KNeighborsClassifier())for name, data in [("30 features", X), ("130 features", X_all)]: score = cross_val_score(knn, data, y, cv=cv).mean() print(f"{name:<12} accuracy {score:.3f}")# 30 features accuracy 0.965# 130 features accuracy 0.917import numpy as npfrom sklearn.datasets import load_breast_cancerfrom sklearn.feature_selection import SelectKBest, f_classiffrom sklearn.model_selection import GridSearchCV, StratifiedKFoldfrom sklearn.model_selection import cross_val_scorefrom sklearn.neighbors import KNeighborsClassifierfrom sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import StandardScalerX, y = load_breast_cancer(return_X_y=True)extra = np.random.default_rng(0).normal(size=(len(X), 100))X_all = np.hstack([X, extra])cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)pipe = make_pipeline(StandardScaler(), SelectKBest(f_classif), KNeighborsClassifier())search = GridSearchCV(pipe, {"selectkbest__k": [10, 20, 40, 80, 130]}, cv=5)score = cross_val_score(search, X_all, y, cv=cv).mean() # k picked per foldprint(f"130 features, selection inside CV: accuracy {score:.3f}")kept = search.fit(X_all, y).best_estimator_[1].get_support()print(f"kept {kept.sum()} columns, {kept[30:].sum()} of them noise")# 130 features, selection inside CV: accuracy 0.963# kept 20 columns, 0 of them noiseThe curse of dimensionality
It's not only KNN
Selection belongs inside the pipeline
SelectKBest with an ANOVA F-test and lets GridSearchCV choose how many columns to keep, inside each training fold. It keeps 20 columns, none of them noise, and accuracy returns to 0.963. scikit-learn also offers L1-based SelectFromModel, RFECV and SequentialFeatureSelector; the user guide recommends running any of them in a Pipeline.
How to apply it in your project
Start from a baseline you trust Measure a model on the features you understand, then add new columns in groups and keep only the groups that improve the cross-validated score. Put selection in the pipeline Use SelectKBest,SelectFromModelorRFECVas a pipeline step, and tune how many features to keep withGridSearchCV.Watch the ratio of features to rows Google's rule of thumb for a thousand rows is about a dozen features. Hundreds of columns on that much data asks the model to find signal in noise. Match the model to the width For wide data, prefer regularized linear models or tree ensembles over plain KNN, or reduce dimensions first, for example with NeighborhoodComponentsAnalysisin front of KNN.Delete what you don't use The Rules of ML call unused features technical debt. Remove them from the pipeline and the data feeds, not just from the model.
Key takeaways
Extra features aren't free: the model must learn to ignore each one, and finite data makes that imperfect. Distance-based models suffer most: 100 unrelated columns took KNN from 0.965 to 0.917 accuracy. The data you need grows with the number of features, the core of the curse of dimensionality. Select features inside cross-validation; here it kept 20 real columns and restored 0.963.
التعليقات (0)