Sequential feature selection methods.
This module provides sequential/greedy feature selection methods that iteratively add or remove features based on model performance.
- SequentialFeatureSelector: greedy forward/backward stepwise search.
- BestFirstSelector: best-first search with backtracking.
Classes
class features.selection.sequential.SequentialFeatureSelector(FeatureSelector, SelectorMixin)
Sequential feature selector (forward or backward selection).
__init__( self, estimator: Any = None, n_features_to_select: Union[int, float, str] = 'auto', direction: Literal['forward', 'backward'] = 'forward', scoring: Optional[Any] = None, cv: int = 5, tol: float = 0.0, random_state: Optional[int] = None, )
Overview
Process
- Forward Selection: Start with zero features. In each step, evaluate all
- Backward Selection: Start with all features. In each step, try removing
The process continues until the desired number of features is reached or no improvement above tol is found.
Parameters
estimator
fit and predict methods.
n_features_to_select
Number of features to select:
- •
int: Select exactly this many features. - •
float: Select this fraction of total features (0 < x < 1). - •
"auto": Stop when the score doesn't improve by at leasttol.
direction
scoring
cv
tol
tol.
random_state
Attributes
n_features_to_select_
support_
Notes
- Roughly O(n_{features} \cdot n_{select} \cdot CV) model fits.
- More expensive than filter methods but captures feature interactions.
- For small to medium datasets where feature interactions are important.
- When you want to find a sparse set of highly predictive features.
- Computationally expensive for many features.
- Greedy search may get stuck in local optima.
References
See Also
Select 2 features forward using a simple estimator:
>>> from tuiml.features.selection import SequentialFeatureSelector
>>> from tuiml.algorithms.linear import LogisticRegression
>>> import numpy as np
>>> X, y = np.random.randn(20, 5), np.random.randint(0, 2, 20)
>>> selector = SequentialFeatureSelector(
... estimator=LogisticRegression(),
... n_features_to_select=2,
... direction='forward'
... )
>>> X_new = selector.fit_transform(X, y)
>>> print(selector.n_features_to_select_)
2
Methods
class features.selection.sequential.BestFirstSelector(FeatureSelector, SelectorMixin)
Best-first feature selector with backtracking.
__init__( self, estimator: Any = None, direction: Literal['forward', 'backward', 'bidirectional'] = 'forward', search_termination: int = 5, cv: int = 5, random_state: Optional[int] = None, )
Overview
Search Process
- Maintain a list of "open" nodes (feature subsets) ranked by their CV score.
- Expand the best node by adding/removing one feature.
-
If the best score hasn't improved for
search_terminationexpansions,
This strategy balances between greedy search and exhaustive exploration.
Parameters
estimator
fit and predict methods.
direction
Search direction:
- •
"forward": Start with no features. - •
"backward": Start with all features. - •
"bidirectional": Can add or remove features at any step.
search_termination
cv
random_state
Attributes
n_features_selected_
Notes
-
Can be highly variable depending on
search_termination. -
Generally more expensive than
SequentialFeatureSelectorbut potentially
- When greedy selection fails to find a good subset.
- When you have moderate number of features and computational budget.
Perform best-first search for 10 nodes:
>>> from tuiml.features.selection import BestFirstSelector
>>> from tuiml.algorithms.trees import DecisionTreeClassifier
>>> import numpy as np
>>> X, y = np.random.randn(20, 8), np.random.randint(0, 2, 20)
>>> selector = BestFirstSelector(
... estimator=DecisionTreeClassifier(),
... direction='forward',
... search_termination=3
... )
>>> X_new = selector.fit_transform(X, y)
>>> print(f"Selected {selector.n_features_selected_} features")