๐ Filter Methods for Feature Selection
Machine Learning Tutorial with Mathematics, Dataset and Python
8 Features × 8 Records
1. What is a Filter Method?
A Filter Method is a feature-selection technique that evaluates features using statistical or mathematical properties of the dataset before the machine-learning model is trained.
The basic idea is:
Unlike wrapper methods, a filter method does not repeatedly train a machine-learning model for every possible feature subset.
A filter method normally calculates an independent score for each feature. Features with poor scores can then be removed before model training.
2. Dataset: 8 Features × 8 Records
We use a small student-performance dataset. There are 8 input features, 8 observations, and one categorical target called Result.
| 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 |
StudyHours, Attendance, Assignment, SleepHours, ScreenTime, PracticeTests, LibraryVisits, PreviousScore
Target = Result
Fail / Pass
3. Major Filter Methods
๐ Variance Threshold
Removes features with very small variance.
๐ Pearson Correlation
Measures linear relationship between numerical variables.
๐ Spearman Correlation
Measures monotonic relationship using ranks.
ฯ² Chi-Square
Measures dependence between categorical variables.
๐ ANOVA F-Test
Measures differences between class groups.
๐ง Mutual Information
Measures information shared between feature and target.
๐ฏ Information Gain
Measures reduction in entropy after observing a feature.
⚖️ Fisher Score
Measures separation between classes.
๐ Redundancy Analysis
Identifies highly correlated features that provide similar information.
4. Filter Method 1 — Variance Threshold
Variance measures how much the values of a feature change around their mean.
Example: StudyHours
StudyHours = 2, 3, 4, 5, 6, 7, 8, 9
Therefore:
A feature with variance close to zero may be removed.
Python
from sklearn.feature_selection import VarianceThreshold
selector = VarianceThreshold(
threshold=1
)
X_selected = selector.fit_transform(X)
print(selector.get_support())
5. Filter Method 2 — Pearson Correlation
Pearson correlation measures the strength and direction of a linear relationship between two numerical variables.
Interpretation
| Correlation | Interpretation |
|---|---|
| +1 | Perfect positive relationship |
| +0.7 to +1 | Strong positive relationship |
| 0 | No linear relationship |
| -0.7 to -1 | Strong negative relationship |
| -1 | Perfect negative relationship |
Example
StudyHours and PreviousScore increase together in this dataset. Therefore their Pearson correlation is expected to be strongly positive.
import pandas as pd
correlation =
df.corr(numeric_only=True)
print(
correlation["PreviousScore"]
)
6. Filter Method 3 — Spearman Rank Correlation
Spearman correlation first converts values into ranks and then calculates correlation between those ranks.
Example
For a feature with ranks:
and target ranks:
Then:
Python
from scipy.stats import spearmanr
rho, p_value = spearmanr(
df["StudyHours"],
df["PreviousScore"]
)
print("Spearman:", rho)
print("P-value:", p_value)
Pearson → linear relationship.
Spearman → monotonic relationship based on ranks.
7. Filter Method 4 — Chi-Square Test
The Chi-Square test evaluates whether two categorical variables are statistically independent.
where:
- O = Observed frequency
- E = Expected frequency
Expected Frequency
Simple Example
Suppose we convert StudyHours into two categories:
| Study Level | Fail | Pass |
|---|---|---|
| Low | 3 | 0 |
| High | 0 | 5 |
The expected frequency for Low–Fail is:
The Chi-Square statistic is obtained by summing:
Python
from sklearn.feature_selection import (
SelectKBest,
chi2
)
selector = SelectKBest(
score_func=chi2,
k=4
)
X_selected = selector.fit_transform(
X,
y
)
print(selector.scores_)
chi2 feature score requires
non-negative feature values. It is commonly used with classification
problems.
8. Filter Method 5 — ANOVA F-Test
ANOVA evaluates whether the means of a numerical feature differ between target classes.
Between-Group Mean Square
Within-Group Mean Square
where:
- N = total number of observations
- k = number of classes
Interpretation
Python
from sklearn.feature_selection import (
SelectKBest,
f_classif
)
selector = SelectKBest(
score_func=f_classif,
k=4
)
X_selected = selector.fit_transform(
X,
y
)
print(selector.scores_)
9. Filter Method 6 — Mutual Information
Mutual Information measures the amount of information shared between a feature X and target Y.
If X and Y are independent:
A higher value means stronger statistical dependence.
Python — Classification
from sklearn.feature_selection import (
mutual_info_classif
)
scores = mutual_info_classif(
X,
y,
random_state=42
)
for feature, score in zip(
feature_names,
scores
):
print(feature, score)
10. Filter Method 7 — Information Gain
Information Gain measures how much uncertainty about the target is reduced after observing a feature.
Entropy
In our dataset:
- Fail = 3
- Pass = 5
- Total = 8
Conditional Entropy
Information Gain
Python Concept
from sklearn.feature_selection import (
mutual_info_classif
)
information_gain = mutual_info_classif(
X,
y,
random_state=42
)
print(information_gain)
11. Filter Method 8 — Fisher Score
Fisher Score evaluates how well a feature separates different classes. A feature is useful when its class means are far apart while its within-class variation is small.
where:
- nโ = number of observations in class k
- ฮผโ = mean of class k
- ฮผ = overall mean
- ฯโ² = variance within class k
Python Concept
import numpy as np
def fisher_score(feature, y):
classes = np.unique(y)
overall_mean = np.mean(feature)
numerator = 0
denominator = 0
for c in classes:
group = feature[y == c]
n = len(group)
mean_c = np.mean(group)
var_c = np.var(group)
numerator += n * (
mean_c - overall_mean
) ** 2
denominator += n * var_c
return numerator / denominator
for i, feature in enumerate(
feature_names
):
score = fisher_score(
X[:, i],
y
)
print(feature, score)
12. Filter Method 9 — Redundancy Analysis
Two features may individually have strong relationships with the target but contain almost the same information.
For example:
If two features are highly correlated with each other, retaining both may provide little additional information for some models.
Feature-Feature Correlation
corr = df[
feature_names
].corr()
print(corr)
13. Ranking Features Using Filter Scores
After calculating a statistical score, features can be ranked.
| Feature | Variance | Pearson | ANOVA | Mutual Information |
Decision |
|---|---|---|---|---|---|
| StudyHours | High | High | High | High | Candidate |
| Attendance | High | High | High | High | Candidate |
| Assignment | High | High | High | High | Candidate |
| SleepHours | Moderate | Moderate | Moderate | Evaluate | Candidate |
| ScreenTime | High | Negative | High | High | Candidate |
| PracticeTests | High | High | High | High | Candidate |
| LibraryVisits | High | High | High | High | Candidate |
| PreviousScore | High | High | High | High | Candidate |
14. Select Top-K Features
Suppose we want only four features from the original eight.
The filter procedure is:
Python
from sklearn.feature_selection import (
SelectKBest,
f_classif
)
selector = SelectKBest(
score_func=f_classif,
k=4
)
X_new = selector.fit_transform(
X,
y
)
print(
selector.get_support()
)
15. Complete Python Implementation
import pandas as pd
from sklearn.feature_selection import (
VarianceThreshold,
SelectKBest,
chi2,
f_classif,
mutual_info_classif
)
from scipy.stats import spearmanr
# ============================================
# DATASET
# ============================================
df = pd.DataFrame({
"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"
]
})
features = [
"StudyHours",
"Attendance",
"Assignment",
"SleepHours",
"ScreenTime",
"PracticeTests",
"LibraryVisits",
"PreviousScore"
]
X = df[features]
y = df["Result"]
# ============================================
# 1. VARIANCE
# ============================================
print("\nVARIANCE")
print(
X.var()
)
# ============================================
# 2. PEARSON
# ============================================
print("\nPEARSON")
numeric_df = df.copy()
numeric_df["ResultCode"] = (
numeric_df["Result"]
.map({
"Fail":0,
"Pass":1
})
)
print(
numeric_df[
features + ["ResultCode"]
].corr()["ResultCode"]
)
# ============================================
# 3. SPEARMAN
# ============================================
print("\nSPEARMAN")
for feature in features:
rho, p = spearmanr(
df[feature],
df["Result"].map({
"Fail":0,
"Pass":1
})
)
print(
feature,
"rho =",
rho,
"p =",
p
)
# ============================================
# 4. CHI-SQUARE
# ============================================
print("\nCHI-SQUARE")
y_code = y.map({
"Fail":0,
"Pass":1
})
chi_selector = SelectKBest(
score_func=chi2,
k="all"
)
chi_selector.fit(
X,
y_code
)
for feature, score, p in zip(
features,
chi_selector.scores_,
chi_selector.pvalues_
):
print(
feature,
"Chi2 =",
score,
"p =",
p
)
# ============================================
# 5. ANOVA
# ============================================
print("\nANOVA")
anova_selector = SelectKBest(
score_func=f_classif,
k="all"
)
anova_selector.fit(
X,
y_code
)
for feature, score, p in zip(
features,
anova_selector.scores_,
anova_selector.pvalues_
):
print(
feature,
"F =",
score,
"p =",
p
)
# ============================================
# 6. MUTUAL INFORMATION
# ============================================
print("\nMUTUAL INFORMATION")
mi = mutual_info_classif(
X,
y_code,
random_state=42
)
for feature, score in zip(
features,
mi
):
print(
feature,
"MI =",
score
)
# ============================================
# 7. SELECT TOP FEATURES
# ============================================
selector = SelectKBest(
score_func=f_classif,
k=4
)
X_selected = selector.fit_transform(
X,
y_code
)
print("\nSELECTED FEATURES")
for feature, selected in zip(
features,
selector.get_support()
):
if selected:
print(feature)
16. Complete Filter Method Workflow
17. Comparison of Filter Methods
| Method | Target Type | Mathematical Basis | Main Purpose |
|---|---|---|---|
| Variance Threshold | Any | Variance | Remove low-variation features |
| Pearson | Numerical | Correlation coefficient | Linear relationship |
| Spearman | Ordinal/Numerical | Rank correlation | Monotonic relationship |
| Chi-Square | Categorical | ฯ² statistic | Categorical dependence |
| ANOVA | Categorical | F statistic | Compare class means |
| Mutual Information | Classification/Regression | Information theory | Statistical dependency |
| Information Gain | Classification | Entropy | Reduction in uncertainty |
| Fisher Score | Classification | Between/within class variation | Class separation |
| Redundancy Analysis | Numerical | Feature-feature correlation | Remove duplicate information |
18. Advantages of Filter Methods
⚡ Fast
Most filter techniques are computationally inexpensive compared with large wrapper searches.
๐ Model Independent
The feature score can usually be calculated without selecting a particular machine-learning estimator.
๐ Dimensionality Reduction
Unimportant variables can be removed before model training.
๐ง Easy to Interpret
Statistical scores provide an understandable basis for ranking features.
19. Limitations of Filter Methods
- A filter method may evaluate features individually and therefore may not fully capture feature interactions.
- A feature with a weak individual score may become useful when combined with another feature.
- Different statistical measures can produce different rankings.
- A high statistical score does not guarantee improved test accuracy.
- Feature selection must be performed carefully to avoid data leakage.
20. Quick Revision
| Question | Answer |
|---|---|
| What is a filter method? | A model-independent statistical feature-selection approach. |
| Variance Threshold? | Removes features with low variance. |
| Pearson correlation? | Measures linear association. |
| Spearman correlation? | Measures rank-based monotonic association. |
| Chi-Square? | Measures dependence between categorical variables. |
| ANOVA? | Compares between-group and within-group variation. |
| Mutual Information? | Measures statistical information shared by variables. |
| Information Gain? | Measures reduction in entropy. |
| Fisher Score? | Measures separation between classes. |
| Why rank features? | To select the most useful features according to a chosen criterion. |
No comments:
Post a Comment