🧠 Feature Selection in Machine Learning
Feature selection is the process of choosing the most relevant features (columns) from
your dataset that contribute the most to your prediction target. It helps improve model
accuracy, speed, and interpretability.
✅ Why Feature Selection is Important
🧽 Reduces overfitting
🚀 Speeds up training
🧮 Improves model accuracy
📉 Reduces noise and redundancy
🔧 Types of Feature Selection Methods
1. Filter Methods
These are statistical tests applied before training the model.
Method Use When Example
Correlation matrix Numeric features Drop highly correlated columns
Chi-Square test Categorical + classification target chi2()
ANOVA (F-test) Continuous feature + classification f_classif()
Mutual Information General use mutual_info_classif()
python
CopyEdit
from sklearn.feature_selection import SelectKBest, f_classif
selector = SelectKBest(score_func=f_classif, k=5)
X_selected = selector.fit_transform(X, y)
2. Wrapper Methods
Use a machine learning model to select features based on performance.
Method Description
Forward Selection Add one feature at a time
Backward Elimination Start with all features, remove one by one
Recursive Feature Elimination (RFE) Remove least important recursively
python
CopyEdit
from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
selector = RFE(model, n_features_to_select=5)
X_selected = selector.fit_transform(X, y)
3. Embedded Methods
Feature selection happens during model training.
Algorithm Built-in Selection
Lasso Regression (L1) Shrinks coefficients of unimportant features to 0
Tree-based models Use feature_importances_
python
CopyEdit
from [Link] import RandomForestClassifier
model = RandomForestClassifier()
[Link](X, y)
importances = model.feature_importances_
📊 Feature Importance with Tree-Based Models
python
CopyEdit
import [Link] as plt
import seaborn as sns
importances = model.feature_importances_
feat_names = [Link]
[Link](x=importances, y=feat_names)
[Link]("Feature Importances")
[Link]()
💡 Dimensionality Reduction (Alternative)
Technique Description Use When
PCA (Principal Component Converts features into principal For visualization or high-
Analysis) components dimensional data
Not used for prediction
t-SNE / UMAP For visualization of clusters
directly
python
CopyEdit
from [Link] import PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)
🔁 Feature Selection Strategy
1. Remove low-variance features
2. Remove highly correlated features
3. Use filter + wrapper or embedded method
4. Visualize feature importance
🧪 Feature Selection Libraries
Library Use For
sklearn.feature_selection Basic methods
mlxtend Forward/Backward selection
BorutaPy All-relevant selection using random forests
Yellowbrick Visualizations
🚀 Real-World Tip
Feature selection is dataset- and model-specific. There’s no universal best method. Try
different methods and validate using cross-validation or grid search.