Importance of feature selection
Feature selection retains the most informative predictors and discards irrelevant or redundant inputs before or during modeling. Doing so reduces overfitting and model variance in high-dimensional datasets, improves human interpretability, lowers training and inference costs, and mitigates multicollinearity.
Study Card: Importance of Feature Selection
Direct Answer
Feature selection is a core modeling step that identifies and retains the most predictive original features while discarding noisy, irrelevant, or redundant ones. Removing uninformative inputs mitigates the curse of dimensionality and reduces overfitting (lower variance), improves model interpretability by focusing on meaningful signals, stabilizes linear models against multicollinearity, and reduces data collection, memory, training, and serving latency in production systems.
Key Terms
- Feature Selection: Choosing a subset of original input features without transforming them into combined latent axes.
- Overfitting: Memorizing spurious correlations in noisy features, degrading out-of-sample accuracy.
- Curse of Dimensionality: Exponential growth in feature-space volume as dimensionality increases, making training samples sparse relative to parameter count.
- Multicollinearity: High correlation among redundant predictors, causing unstable coefficient estimates in linear/logistic regression.
- Generalization: Predictive accuracy on unseen test data.
Example
Consider predicting customer churn from a warehouse table with 300 features including demographics, billing events, and noisy clickstream flags. Selecting the 15 strongest predictors—such as tenure, contract_type, monthly_charges, and support_tickets_30d—cuts inference memory and latency, eliminates spurious correlations with random telemetry, and yields clear feature weights that product teams can actually interpret and act on.
Code Implementation
import pandas as pd
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import RFE, SelectFromModel
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Generate dataset with 20 features: only 5 informative, 5 redundant, 10 noise
X, y = make_classification(
n_samples=1000, n_features=20, n_informative=5, n_redundant=5, random_state=42
)
X = pd.DataFrame(X, columns=[f"f{i}" for i in range(20)])
y = pd.Series(y)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
# Standardize features so linear model coefficients (and RFE rankings) are scale-consistent
scaler = StandardScaler()
X_train_s = scaler.fit_transform(X_train)
X_test_s = scaler.transform(X_test)
# 1. Embedded/Tree-Importance Selection via Random Forest
rf = RandomForestClassifier(random_state=42).fit(X_train, y_train)
sfm = SelectFromModel(rf, threshold='median', prefit=True)
clf_rf_sel = LogisticRegression(random_state=42).fit(sfm.transform(X_train_s), y_train)
acc_rf_sel = accuracy_score(y_test, clf_rf_sel.predict(sfm.transform(X_test_s)))
print(f"Accuracy with RF Feature Selection ({sfm.transform(X_train_s).shape[1]} feats): {acc_rf_sel:.4f}")
# 2. Wrapper Selection via Recursive Feature Elimination (RFE)
rfe = RFE(LogisticRegression(random_state=42), n_features_to_select=5, step=1)
X_train_rfe = rfe.fit_transform(X_train_s, y_train)
X_test_rfe = rfe.transform(X_test_s)
clf_rfe = LogisticRegression(random_state=42).fit(X_train_rfe, y_train)
acc_rfe = accuracy_score(y_test, clf_rfe.predict(X_test_rfe))
print(f"Accuracy with RFE (5 feats): {acc_rfe:.4f}")
# 3. Baseline without feature selection (all 20 features)
clf_all = LogisticRegression(random_state=42).fit(X_train_s, y_train)
acc_all = accuracy_score(y_test, clf_all.predict(X_test_s))
print(f"Accuracy WITHOUT feature selection (20 feats): {acc_all:.4f}")
Related Concepts
- Feature Selection vs. Feature Extraction: Feature selection preserves original features (keeping domain interpretability), whereas feature extraction (PCA, autoencoders) maps inputs into lower-dimensional synthetic combinations.
- Filter, Wrapper, and Embedded Methods: Filter methods use fast statistical scores; wrapper methods search subsets using validation performance; embedded methods (like L1/Lasso) integrate selection into optimization.
- MDI vs. Permutation Importance: Random Forest Mean Decrease in Impurity (MDI) can overrate high-cardinality continuous features and split importance across correlated copies; permutation importance on a held-out validation fold provides a less biased check.
- Cross-Validation Hygiene: Perform feature selection inside CV training folds rather than on the full dataset before splitting to prevent optimistic leakage.