๐ฏ Wrapper Methods for Feature Selection
Machine Learning Tutorial with Mathematics, Dataset and Python
8 Features × 8 Records
1. What is a Wrapper Method?
A Wrapper Method selects features by repeatedly training and evaluating a machine-learning model using different subsets of features.
The important difference from a filter method is that the usefulness of a feature is judged according to the performance of a selected machine-learning algorithm.
{StudyHours}
{StudyHours, Attendance}
{StudyHours, Attendance, Assignment}
...
and compare the model performance for each subset.
2. Dataset: 8 Features × 8 Records
The following small dataset is used throughout this tutorial.
| Student | StudyHours | Attendance | Assignment | SleepHours | ScreenTime | PracticeTests | LibraryVisits | PreviousScore | Result |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 60 | 3 | 5 | 8 | 1 | 1 | 45 | Fail |
| 2 | 3 | 65 | 4 | 6 | 7 | 2 | 1 | 50 | Fail |
| 3 | 4 | 70 | 5 | 6 | 6 | 2 | 2 | 55 | Fail |
| 4 | 5 | 75 | 6 | 7 | 5 | 3 | 3 | 62 | Pass |
| 5 | 6 | 80 | 7 | 7 | 5 | 4 | 4 | 68 | Pass |
| 6 | 7 | 85 | 8 | 8 | 4 | 5 | 5 | 74 | Pass |
| 7 | 8 | 90 | 9 | 8 | 3 | 6 | 6 | 80 | Pass |
| 8 | 9 | 95 | 10 | 9 | 2 | 7 | 7 | 88 | Pass |
Number of input features = 8
Target = Result
3. Types of Wrapper Methods
➡️ Forward Selection
Starts with no features and adds features one by one.
⬅️ Backward Elimination
Starts with all features and removes features one by one.
๐ Sequential Forward Selection
Sequentially adds the feature that improves the selected evaluation score.
๐ Sequential Backward Selection
Sequentially removes the feature whose removal gives the best result.
๐ฏ RFE
Recursive Feature Elimination repeatedly removes the least important features.
๐ RFECV
RFE combined with cross-validation to determine the feature count.
๐งฎ Exhaustive Search
Evaluates possible feature subsets systematically.
๐ Best Subset Selection
Searches for the subset producing the chosen evaluation criterion.
4. Mathematical Idea Behind Wrapper Selection
Let the complete feature set be:
A subset S is selected from F:
A machine-learning model is trained using S:
The model produces an evaluation score:
The wrapper searches for a subset that optimizes the chosen criterion. For example, when maximizing accuracy:
For minimizing an error:
5. Model Evaluation
A wrapper method needs an evaluation criterion. For classification, common criteria include:
Accuracy
Precision
Recall
F1 Score
6. Forward Selection
Forward selection begins with an empty feature set. At each step, one feature is added.
Suppose the eight features are:
X₂ = Attendance
X₃ = Assignment
X₄ = SleepHours
X₅ = ScreenTime
X₆ = PracticeTests
X₇ = LibraryVisits
X₈ = PreviousScore
Step 1
Test each feature independently:
Select the feature giving the best validation score.
Step 2
Now test adding every remaining feature:
S₁ ∪ {X₃}
...
Select the addition producing the best score.
Process
Python
from mlxtend.feature_selection import (
SequentialFeatureSelector
)
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(
max_iter=1000
)
sfs = SequentialFeatureSelector(
model,
k_features=4,
forward=True,
floating=False,
scoring="accuracy",
cv=5
)
sfs.fit(X, y)
print(
sfs.k_feature_names_
)
7. Backward Elimination
Backward elimination starts with all features.
Now one feature is removed at a time.
Step 1
S₀ − {X₂}
...
S₀ − {X₈}
The subset producing the best evaluation score is retained.
Process
Python
sbs = SequentialFeatureSelector(
model,
k_features=4,
forward=False,
floating=False,
scoring="accuracy",
cv=5
)
sbs.fit(X, y)
print(
sbs.k_feature_names_
)
8. Sequential Forward Selection (SFS)
SFS is a systematic forward search procedure.
At each iteration:
Then:
The process continues until the required number of features is selected.
9. Sequential Backward Selection (SBS)
SBS begins with all features.
At each iteration, one feature is removed.
Then:
10. Recursive Feature Elimination — RFE
RFE starts with all features and repeatedly removes the least important features according to the selected estimator.
Train the model:
Determine feature importance:
Remove the least important feature:
Repeat until the desired number of features remains.
Python
from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(
max_iter=1000
)
rfe = RFE(
estimator=model,
n_features_to_select=4
)
rfe.fit(X, y)
for feature, rank, selected in zip(
feature_names,
rfe.ranking_,
rfe.support_
):
print(
feature,
"Rank:", rank,
"Selected:", selected
)
11. RFE Feature Importance
For a linear model, the absolute value of a coefficient can be used as an importance measure.
Suppose a model produces:
| Feature | Coefficient ฮฒ | |ฮฒ| |
|---|---|---|
| StudyHours | 1.82 | 1.82 |
| Attendance | 1.41 | 1.41 |
| Assignment | 1.22 | 1.22 |
| SleepHours | 0.31 | 0.31 |
| ScreenTime | -0.88 | 0.88 |
| PracticeTests | 1.15 | 1.15 |
| LibraryVisits | 0.76 | 0.76 |
| PreviousScore | 2.01 | 2.01 |
12. RFECV — Recursive Feature Elimination with Cross-Validation
RFECV extends RFE by using cross-validation to determine an appropriate number of features.
For example, RFECV can evaluate:
| Number of Features | Mean CV Score |
|---|---|
| 1 | 0.75 |
| 2 | 0.80 |
| 3 | 0.84 |
| 4 | 0.88 |
| 5 | 0.86 |
| 6 | 0.85 |
| 7 | 0.84 |
| 8 | 0.83 |
The cross-validation score is used to determine the useful feature count.
Python
from sklearn.feature_selection import RFECV
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold
model = LogisticRegression(
max_iter=1000
)
cv = StratifiedKFold(
n_splits=4,
shuffle=True,
random_state=42
)
rfecv = RFECV(
estimator=model,
step=1,
cv=cv,
scoring="accuracy",
min_features_to_select=1
)
rfecv.fit(X, y)
print(
"Selected feature count:",
rfecv.n_features_
)
print(
rfecv.support_
)
13. Cross-Validation in Wrapper Methods
A wrapper method can evaluate each feature subset using cross-validation.
For 5-fold cross-validation:
For example, suppose a feature subset produces:
Then:
Therefore the subset receives an average validation score of:
14. Exhaustive Feature Selection
Exhaustive search evaluates every possible subset of the features.
For n features, the number of all possible subsets is:
For our 8-feature dataset:
If the empty subset is excluded:
Number of subsets by size
| Features Selected | Number of Subsets |
|---|---|
| 1 | C(8,1) = 8 |
| 2 | C(8,2) = 28 |
| 3 | C(8,3) = 56 |
| 4 | C(8,4) = 70 |
| 5 | C(8,5) = 56 |
| 6 | C(8,6) = 28 |
| 7 | C(8,7) = 8 |
| 8 | C(8,8) = 1 |
| Total | 255 |
15. Best-Subset Selection
Suppose we specifically want four features. The number of four-feature subsets is:
Therefore, an exhaustive four-feature wrapper would train and evaluate 70 different models.
16. Exhaustive Search — Python
from itertools import combinations
from sklearn.model_selection import (
cross_val_score
)
from sklearn.linear_model import (
LogisticRegression
)
model = LogisticRegression(
max_iter=1000
)
best_score = -1
best_features = None
for combination in combinations(
feature_names,
4
):
X_subset = X[
list(combination)
]
scores = cross_val_score(
model,
X_subset,
y,
cv=4,
scoring="accuracy"
)
mean_score = scores.mean()
print(
combination,
mean_score
)
if mean_score > best_score:
best_score = mean_score
best_features = combination
print(
"\nBest Features:",
best_features
)
print(
"Best CV Score:",
best_score
)
17. Sequential Feature Selection with scikit-learn
from sklearn.feature_selection import (
SequentialFeatureSelector
)
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(
max_iter=1000
)
sfs = SequentialFeatureSelector(
model,
n_features_to_select=4,
direction="forward",
scoring="accuracy",
cv=4
)
sfs.fit(X, y)
selected = X.columns[
sfs.get_support()
]
print(
"Selected Features:"
)
print(
selected.tolist()
)
Backward Selection
sbs = SequentialFeatureSelector(
model,
n_features_to_select=4,
direction="backward",
scoring="accuracy",
cv=4
)
sbs.fit(X, y)
selected = X.columns[
sbs.get_support()
]
print(
selected.tolist()
)
18. Complete Python Dataset
import pandas as pd
data = {
"StudyHours":
[2,3,4,5,6,7,8,9],
"Attendance":
[60,65,70,75,80,85,90,95],
"Assignment":
[3,4,5,6,7,8,9,10],
"SleepHours":
[5,6,6,7,7,8,8,9],
"ScreenTime":
[8,7,6,5,5,4,3,2],
"PracticeTests":
[1,2,2,3,4,5,6,7],
"LibraryVisits":
[1,1,2,3,4,5,6,7],
"PreviousScore":
[45,50,55,62,68,74,80,88],
"Result":
[
"Fail",
"Fail",
"Fail",
"Pass",
"Pass",
"Pass",
"Pass",
"Pass"
]
}
df = pd.DataFrame(data)
print(df)
19. Comparison of Wrapper Methods
| Method | Starting Point | Operation | Uses Model? | Search Direction |
|---|---|---|---|---|
| Forward Selection | Empty set | Add feature | Yes | Forward |
| Backward Elimination | All features | Remove feature | Yes | Backward |
| SFS | Empty set | Sequential addition | Yes | Forward |
| SBS | All features | Sequential removal | Yes | Backward |
| RFE | All features | Remove least important | Yes | Backward |
| RFECV | All features | RFE + CV | Yes | Backward |
| Exhaustive Search | All possible subsets | Evaluate subsets | Yes | All directions |
| Best Subset | Fixed subset size | Compare combinations | Yes | Combinatorial |
20. Filter Method vs Wrapper Method
| Property | Filter | Wrapper |
|---|---|---|
| Model required? | Usually No | Yes |
| Uses statistical score? | Yes | May use model score |
| Model dependent? | Usually model independent | Model dependent |
| Computational cost | Usually lower | Usually higher |
| Feature interactions | May miss interactions | Can capture subset effects |
| Example | Chi-Square | RFE |
| Example | ANOVA | Forward Selection |
| Example | Mutual Information | Backward Elimination |
21. Complete Wrapper Workflow
Separate X and y.
Example: Logistic Regression.
Forward, backward, recursive or exhaustive search.
Train the model using the current subset.
Calculate cross-validation score.
Compare the current subset with candidate subsets.
Keep the subset according to the chosen criterion.
Train the final model using the selected features.
22. Computational Cost
Wrapper methods can become expensive because the machine-learning model may need to be trained many times.
Exhaustive Search
| Features | Non-empty subsets |
|---|---|
| 5 | 31 |
| 8 | 255 |
| 10 | 1,023 |
| 20 | 1,048,575 |
| 30 | 1,073,741,823 |
23. Practical Example: Select 4 of 8 Features
Suppose we want exactly four features from our eight features.
k = 4
Number of possible subsets = C(8,4)
= 70
An exhaustive wrapper can evaluate all 70 combinations.
For each combination:
Finally:
24. Advantages of Wrapper Methods
๐ฏ Model Specific
The selected features are evaluated according to actual model performance.
๐ Feature Interaction
Wrapper approaches can discover useful combinations of features.
๐ Performance Based
The selection criterion can directly use accuracy, F1, RMSE or another model metric.
๐ Subset Evaluation
The usefulness of a group of features can be evaluated together.
25. Limitations of Wrapper Methods
- They can be computationally expensive.
- Repeated model training can take considerable time.
- Exhaustive search becomes impractical for high-dimensional datasets.
- The selected features can depend on the chosen machine-learning model.
- Careless evaluation can cause overfitting.
- Cross-validation should be designed carefully to prevent information leakage.
26. Quick Revision
| Question | Answer |
|---|---|
| What is a wrapper method? | A feature-selection method that evaluates subsets using a machine-learning model. |
| Forward selection? | Starts with zero features and adds features. |
| Backward elimination? | Starts with all features and removes features. |
| SFS? | Sequential Forward Selection. |
| SBS? | Sequential Backward Selection. |
| RFE? | Recursive Feature Elimination. |
| RFECV? | RFE combined with cross-validation. |
| Exhaustive search? | Evaluates all possible feature subsets. |
| Number of subsets for 8 features? | 2⁸ = 256 including the empty subset; 255 non-empty subsets. |
| Number of 4-feature subsets from 8? | C(8,4) = 70. |
No comments:
Post a Comment