Skip to content
E
Educatum

Feature selection methods

Curated by Educatum AI teamLast updated:
Basic Concepts
Direct Answer

Feature selection identifies the most informative subset of original input features to improve generalization, interpretability, and computational efficiency. Methods fall into three families: filter methods (model-agnostic statistical scores such as ANOVA F-test, chi-squared, or mutual information), wrapper methods (model-in-the-loop search like RFE), and embedded methods (built into training, such as L1/Lasso penalties and tree splits).

Study Card: Feature Selection Methods

Direct Answer

Feature selection identifies a compact subset of relevant original features before or during model fitting to reduce overfitting, simplify interpretation, and speed up training and inference. The main approaches fall into three categories: filter methods, which score features independently of any downstream predictor; wrapper methods, which search over subsets using cross-validated performance of a chosen estimator; and embedded methods, which learn sparsity or feature importance jointly during model optimization. Choosing among them balances predictive performance, feature-interaction sensitivity, and compute budget.

Key Terms

  • Filter Methods: Fast, model-independent scoring using statistical tests or dependence metrics such as ANOVA F-test (f_classif), mutual information, Pearson/Spearman correlation, or χ2\chi^2 (specifically for non-negative count or categorical features).
  • Wrapper Methods: Iterative subset search guided by a downstream model’s validation score, such as Forward/Backward Stepwise Selection and Recursive Feature Elimination (RFE).
  • Embedded Methods: Incorporate feature selection directly into training—for example, L1/Lasso and Elastic Net penalties (which set irrelevant coefficients strictly to zero) and tree-based split selection. (Note: L2/Ridge shrinks coefficients continuously toward zero but does not zero them out for feature elimination.)
  • Mutual Information: Measures non-linear statistical dependence between each feature and the target.
  • Curse of Dimensionality: Sample sparsity and variance inflation when feature count pp is large relative to sample size nn.

Example

Suppose a churn model has 200 features spanning customer tenure, billing history, and noisy telemetry. A filter step (f_classif or mutual information) quickly discards uncorrelated telemetry in milliseconds. Next, an embedded L1 logistic regression (penalty='l1') or RFE wrapper narrows down the remaining candidates by accounting for correlations among correlated predictors like total_charges and tenure.

Code Implementation

import numpy as np
from sklearn.datasets import make_classification
from sklearn.feature_selection import RFE, SelectFromModel, SelectKBest, f_classif
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# Synthetic dataset with 10 informative and 10 pure-noise features
X, y = make_classification(
    n_samples=1000, n_features=20, n_informative=10, n_redundant=0, random_state=42
)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42, stratify=y
)

# Scale training features before linear modeling / RFE / L1 selection
scaler = StandardScaler()
X_train_s = scaler.fit_transform(X_train)
X_test_s = scaler.transform(X_test)

# 1. Filter Method: ANOVA F-test (use chi2 only for non-negative count/categorical features)
selector_filter = SelectKBest(f_classif, k=10).fit(X_train_s, y_train)
clf_filter = LogisticRegression(random_state=42).fit(selector_filter.transform(X_train_s), y_train)
print(f"Filter (ANOVA F) Selected: {np.where(selector_filter.get_support())[0]}")
print(f"Filter Test Accuracy:      {clf_filter.score(selector_filter.transform(X_test_s), y_test):.4f}")

# 2. Wrapper Method: Recursive Feature Elimination (RFE)
selector_rfe = RFE(LogisticRegression(random_state=42), n_features_to_select=10, step=1)
X_train_rfe = selector_rfe.fit_transform(X_train_s, y_train)
X_test_rfe = selector_rfe.transform(X_test_s)
clf_wrapper = LogisticRegression(random_state=42).fit(X_train_rfe, y_train)
print(f"Wrapper (RFE) Selected:    {np.where(selector_rfe.get_support())[0]}")
print(f"Wrapper Test Accuracy:     {clf_wrapper.score(X_test_rfe, y_test):.4f}")

# 3. Embedded Method: L1-penalized Logistic Regression (Lasso)
l1_model = LogisticRegression(penalty='l1', solver='liblinear', C=0.2, random_state=42)
l1_model.fit(X_train_s, y_train)
selector_l1 = SelectFromModel(l1_model, prefit=True)
X_train_l1 = selector_l1.transform(X_train_s)
X_test_l1 = selector_l1.transform(X_test_s)
clf_embedded = LogisticRegression(random_state=42).fit(X_train_l1, y_train)
print(f"Embedded (L1) Selected:    {np.where(selector_l1.get_support())[0]}")
print(f"Embedded Test Accuracy:    {clf_embedded.score(X_test_l1, y_test):.4f}")
  • Feature Selection vs. Dimensionality Reduction (PCA): Feature selection retains a subset of original interpretable features; PCA projects features into orthogonal linear combinations, losing individual feature interpretability.
  • Data Leakage Warning: Always fit feature selectors strictly inside each cross-validation training fold—running SelectKBest on the entire dataset before splitting leaks test labels and inflates accuracy estimates.
  • Permutation Importance: Evaluates importance by measuring drop in validation score when a feature column is randomly shuffled, avoiding tree-split bias toward high-cardinality numeric features.
  • Collinearity Handling: Under high multicollinearity, univariate filters keep all redundant copies, whereas L1/Elastic Net or hierarchical clustering of correlated features prunes duplicates cleanly.
View all →

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.