

Picture by Editor
# Introduction
In any machine studying challenge, characteristic choice could make or break your mannequin. Deciding on the optimum subset of options reduces noise, prevents overfitting, enhances interpretability, and sometimes improves accuracy. With too many irrelevant or redundant variables, fashions turn out to be bloated and more durable to coach. With too few, they danger lacking essential indicators.
To sort out this problem, we experimented with three fashionable characteristic choice methods on an actual dataset. The objective was to find out which strategy would supply the very best steadiness of efficiency, interpretability, and effectivity. On this article, we share our expertise testing three characteristic choice methods and reveal which one labored finest for our dataset.
# Why Characteristic Choice Issues
When constructing machine studying fashions, particularly on high-dimensional datasets, not all options contribute equally. A leaner, extra informative set of inputs provides a number of benefits:
- Lowered overfitting – Eliminating irrelevant variables helps fashions generalize higher to unseen information.
- Sooner Coaching – Fewer options imply sooner coaching and decrease computational value.
- Higher Interpretability – With a compact set of predictors, it’s simpler to elucidate what drives mannequin selections.
# The Dataset
For this experiment, we used the Diabetes dataset from scikit-learn. It comprises 442 affected person information with 10 baseline options corresponding to physique mass index (BMI), blood strain, a number of serum measurements, and age. The goal variable is a quantitative measure of illness development one 12 months after baseline.
Let’s load the dataset and put together it:
import pandas as pd
from sklearn.datasets import load_diabetes
# Load dataset
information = load_diabetes(as_frame=True)
df = information.body
X = df.drop(columns=['target'])
y = df['target']
print(df.head())
Right here, X
comprises the options, and y
comprises the goal. We now have all the things prepared to use completely different characteristic choice strategies.
# Filter Methodology
Filter strategies rank or get rid of options primarily based on statistical properties quite than by coaching a mannequin. They’re easy, quick, and provides a fast strategy to take away apparent redundancies.
For this dataset, we checked for extremely correlated options and dropped any that exceeded a correlation threshold of 0.85.
import numpy as np
corr = X.corr()
threshold = 0.85
higher = corr.abs().the place(np.triu(np.ones(corr.form), ok=1).astype(bool))
to_drop = [col for col in upper.columns if any(upper[col] > threshold)]
X_filter = X.drop(columns=to_drop)
print("Remaining options after filter:", X_filter.columns.tolist())
Output:
Remaining options after filter: ['age', 'sex', 'bmi', 'bp', 's1', 's3', 's4', 's5', 's6']
Just one redundant characteristic was eliminated, so the dataset retained 9 of the ten predictors. This exhibits the Diabetes dataset is comparatively clear when it comes to correlation.
# Wrapper Methodology
Wrapper strategies consider subsets of options by really coaching fashions and checking efficiency. One fashionable method is Recursive Characteristic Elimination (RFE).
RFE begins with all options, matches a mannequin, ranks them by significance, and recursively removes the least helpful ones till the specified variety of options stays.
from sklearn.linear_model import LinearRegression
from sklearn.feature_selection import RFE
lr = LinearRegression()
rfe = RFE(lr, n_features_to_select=5)
rfe.match(X, y)
selected_rfe = X.columns[rfe.support_]
print("Chosen by RFE:", selected_rfe.tolist())
Chosen by RFE: ['bmi', 'bp', 's1', 's2', 's5']
RFE chosen 5 options out of 10. The trade-off is that this strategy is extra computationally costly because it requires a number of rounds of mannequin becoming.
# Embedded Methodology
Embedded strategies combine characteristic choice into the mannequin coaching course of. Lasso Regression (L1 regularization) is a basic instance. It penalizes characteristic weights, shrinking much less vital ones to zero.
from sklearn.linear_model import LassoCV
lasso = LassoCV(cv=5, random_state=42).match(X, y)
coef = pd.Collection(lasso.coef_, index=X.columns)
selected_lasso = coef[coef != 0].index
print("Chosen by Lasso:", selected_lasso.tolist())
Chosen by Lasso: ['age', 'sex', 'bmi', 'bp', 's1', 's2', 's4', 's5', 's6']
Lasso retained 9 options and eradicated one which contributed little predictive energy. Not like filter strategies, nonetheless, this resolution was primarily based on mannequin efficiency, not simply correlation.
# Outcomes Comparability
To judge every strategy, we skilled a Linear Regression mannequin on the chosen characteristic units. We used 5-fold cross-validation and measured efficiency utilizing R² rating and Imply Squared Error (MSE).
from sklearn.model_selection import cross_val_score, KFold
from sklearn.linear_model import LinearRegression
# Helper analysis perform
def evaluate_model(X, y, mannequin):
cv = KFold(n_splits=5, shuffle=True, random_state=42)
r2_scores = cross_val_score(mannequin, X, y, cv=cv, scoring="r2")
mse_scores = cross_val_score(mannequin, X, y, cv=cv, scoring="neg_mean_squared_error")
return r2_scores.imply(), -mse_scores.imply()
# 1. Filter Methodology outcomes
lr = LinearRegression()
r2_filter, mse_filter = evaluate_model(X_filter, y, lr)
# 2. Wrapper (RFE) outcomes
X_rfe = X[selected_rfe]
r2_rfe, mse_rfe = evaluate_model(X_rfe, y, lr)
# 3. Embedded (Lasso) outcomes
X_lasso = X[selected_lasso]
r2_lasso, mse_lasso = evaluate_model(X_lasso, y, lr)
# Print outcomes
print("=== Outcomes Comparability ===")
print(f"Filter Methodology -> R2: {r2_filter:.4f}, MSE: {mse_filter:.2f}, Options: {X_filter.form[1]}")
print(f"Wrapper (RFE) -> R2: {r2_rfe:.4f}, MSE: {mse_rfe:.2f}, Options: {X_rfe.form[1]}")
print(f"Embedded (Lasso)-> R2: {r2_lasso:.4f}, MSE: {mse_lasso:.2f}, Options: {X_lasso.form[1]}")
=== Outcomes Comparability ===
Filter Methodology -> R2: 0.4776, MSE: 3021.77, Options: 9
Wrapper (RFE) -> R2: 0.4657, MSE: 3087.79, Options: 5
Embedded (Lasso)-> R2: 0.4818, MSE: 2996.21, Options: 9
The Filter methodology eliminated just one redundant characteristic and gave good baseline efficiency. The Wrapper (RFE) lower the characteristic set in half however barely diminished accuracy. The Embedded (Lasso) retained 9 options and delivered the very best R² and lowest MSE. Total, Lasso provided the very best steadiness of accuracy, effectivity, and interpretability.
# Conclusion
Characteristic choice will not be merely a preprocessing step however a strategic resolution that shapes the general success of a machine studying pipeline. Our experiment bolstered that whereas easy filters and exhaustive wrappers every have their place, embedded strategies like Lasso typically present the candy spot.
On the Diabetes dataset, Lasso regularization emerged because the clear winner. It helped us construct a sooner, extra correct, and extra interpretable mannequin with out the heavy computation of wrapper strategies or the oversimplification of filters.
For practitioners, the takeaway is that this: don’t depend on a single methodology blindly. Begin with fast filters to prune apparent redundancies, attempt wrappers for those who want exhaustive exploration, however all the time contemplate embedded strategies like Lasso for a sensible steadiness.
Jayita Gulati is a machine studying fanatic and technical author pushed by her ardour for constructing machine studying fashions. She holds a Grasp’s diploma in Laptop Science from the College of Liverpool.