

Picture by Editor
# Introduction
In any machine studying mission, function 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 grow to be bloated and tougher to coach. With too few, they danger lacking essential alerts.
To deal with this problem, we experimented with three in style function choice strategies on an actual dataset. The purpose 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 function choice strategies 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 affords a number of benefits:
- Diminished overfitting – Eliminating irrelevant variables helps fashions generalize higher to unseen knowledge.
- Sooner Coaching – Fewer options imply sooner coaching and decrease computational value.
- Higher Interpretability – With a compact set of predictors, it’s simpler to clarify what drives mannequin choices.
# The Dataset
For this experiment, we used the Diabetes dataset from scikit-learn. It comprises 442 affected person information with 10 baseline options equivalent to physique mass index (BMI), blood stress, a number of serum measurements, and age. The goal variable is a quantitative measure of illness development one yr after baseline.
Let’s load the dataset and put together it:
import pandas as pd
from sklearn.datasets import load_diabetes
# Load dataset
knowledge = load_diabetes(as_frame=True)
df = knowledge.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 the whole lot prepared to use totally different function choice strategies.
# Filter Methodology
Filter strategies rank or get rid of options based mostly on statistical properties relatively than by coaching a mannequin. They’re easy, quick, and provides a fast method 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), okay=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 function was eliminated, so the dataset retained 9 of the ten predictors. This exhibits the Diabetes dataset is comparatively clear by way of correlation.
# Wrapper Methodology
Wrapper strategies consider subsets of options by really coaching fashions and checking efficiency. One in style approach 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 function choice into the mannequin coaching course of. Lasso Regression (L1 regularization) is a traditional instance. It penalizes function weights, shrinking much less necessary ones to zero.
from sklearn.linear_model import LassoCV
lasso = LassoCV(cv=5, random_state=42).match(X, y)
coef = pd.Sequence(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 based mostly on mannequin efficiency, not simply correlation.
# Outcomes Comparability
To judge every strategy, we skilled a Linear Regression mannequin on the chosen function 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 operate
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 technique eliminated just one redundant function and gave good baseline efficiency. The Wrapper (RFE) reduce the function 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 is just not merely a preprocessing step however a strategic resolution that shapes the general success of a machine studying pipeline. Our experiment strengthened that whereas easy filters and exhaustive wrappers every have their place, embedded strategies like Lasso usually 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 technique blindly. Begin with fast filters to prune apparent redundancies, strive wrappers in case you 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.