Filter wrapper and embedded selection
Quick answer
Feature selection keeps a subset of existing feature columns, unlike dimensionality reduction which can create new combinations. It can reduce cost and overfitting, but must be evaluated as part of the learning procedure.
Filter constant features
from sklearn.feature_selection import VarianceThresholdX = [[1,0,2],[1,1,4],[1,0,6]]s = VarianceThreshold().fit(X)print(s.get_support().tolist())print(s.transform(X).tolist())Expected output:
[False, True, True][[0, 2], [1, 4], [0, 6]]The first column is constant and the remaining two vary.
How it works
Filter methods rank columns by a criterion such as variance or univariate association with the target; they may miss interactions. Wrapper methods repeatedly train and evaluate candidate subsets, such as recursive feature elimination (RFE), and can be expensive. Embedded methods learn selection during model fitting, such as L1-regularized regression producing zero coefficients; the result depends on scaling, regularization and model assumptions. SelectKBest with f_classif ranks features using an ANOVA F statistic for classification; use an appropriate score for the task, not classification scores on continuous regression targets. Choose k within training validation, not after looking at the final test. A Pipeline fits the selector inside each cross-validation training fold when the whole pipeline is evaluated. Hyperparameter search needs an outer evaluation or untouched test for a less biased performance estimate. A high score on random features selected using all labels is a leakage warning, not proof of signal.
Supervised filter on training only
from sklearn.feature_selection import SelectKBest, f_classifX = [[0,0],[1,1],[2,0],[7,0],[8,1],[9,0]]y = [0,0,0,1,1,1]s = SelectKBest(f_classif, k=1).fit(X,y)print(s.get_support().tolist())print(s.transform([[5,9]]).tolist())Expected output:
[True, False][[5]]The first column separates the training classes more strongly; new rows cannot change the selected index.
Common mistakes
Do not select features on the full labeled dataset and then cross-validate only the predictor. The held-out labels already influenced the selected columns. Do not interpret selected features as causal or guaranteed stable across samples.
Practice
Rank scores [0.2, 0.8, 0.8, 0.1] using a declared tie policy. Explain how a wrapper differs from ranking once and why selector fitting belongs inside every fold. Complete the chapter exercise and knowledge check. Scientific examples are local scikit-learn 1.8.0 static snippets; ByteMind execution support is not verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Filter wrapper and embedded selection — knowledge check
Apply feature engineering and leakage controls to synthetic scenarios.
Which method ranks each feature without fitting candidate subset models?
What is a wrapper cost?
When does a pipeline fit a selector in cross-validation?
What can L1 regularization do?
A univariate filter misses two jointly useful features. Why?