Recursive feature elimination selectors.
Classes
Recursive feature elimination.
Constructor
__init__( self, estimator: Any = None, n_features_to_select: int | None = None, step: float = 1, )
Parameters
estimator
object
A supervised estimator exposing
fit and either feature_importances_ or coef_ after fitting.
n_features_to_select
int
Number of features to retain. If
None, half of the input features are retained.
step
int or float
= 1
Number of features to remove at each iteration, or the fraction of features to remove when a float is supplied.
Attributes
n_features_to_select_
int
Number of features selected after fitting.
support_
ndarray of bool
Boolean mask indicating selected features.
ranking_
ndarray of int
Feature ranking. Selected features have rank 1.
python
>>> import numpy as np
>>> from tuiml.features.selection import RFE
>>> from tuiml.algorithms.trees.random_forest import RandomForestClassifier
>>> X = np.random.RandomState(0).randn(30, 5)
>>> y = np.random.RandomState(0).randint(0, 2, 30)
>>> selector = RFE(
... estimator=RandomForestClassifier(n_estimators=5, random_state=0),
... n_features_to_select=2,
... )
>>> _ = selector.fit(X, y)
>>> int(selector.support_.sum())
2
Methods
Recursive feature elimination with cross-validation.
Constructor
__init__( self, estimator: Any = None, step: float = 1, cv: Any = 5, scoring: Any | None = None, random_state: int | None = None, )
Parameters
estimator
object
A supervised estimator exposing
fit, predict and either feature_importances_ or coef_ after fitting.
step
int or float
= 1
Number of features to remove at each iteration, or the fraction of features to remove when a float is supplied.
cv
int or splitter
= 5
Number of folds when an integer is supplied, or a splitter exposing
split(X, y).
scoring
str or callable
Scoring strategy. If
None, classification uses accuracy and regression uses R².
random_state
int
Random seed used when
cv is an integer.
Attributes
n_features_to_select_
int
Number of features selected by cross-validation.
support_
ndarray of bool
Boolean mask indicating selected features.
ranking_
ndarray of int
Feature ranking.
cv_results_
dict
Cross-validation results for each tested feature count.
python
>>> import numpy as np
>>> from tuiml.features.selection import RFECV
>>> from tuiml.algorithms.trees.random_forest import RandomForestClassifier
>>> X = np.random.RandomState(0).randn(40, 5)
>>> y = np.random.RandomState(1).randint(0, 2, 40)
>>> selector = RFECV(
... estimator=RandomForestClassifier(n_estimators=5, random_state=0),
... cv=3,
... )
>>> _ = selector.fit(X, y)
>>> selector.n_features_to_select_ >= 1
True
A splitter instance can also be supplied:
python
>>> from tuiml.evaluation.splitting import KFold
>>> cv = KFold(n_splits=3, shuffle=True, random_state=0)
>>> selector = RFECV(
... estimator=RandomForestClassifier(n_estimators=5, random_state=0),
... cv=cv,
... )
>>> _ = selector.fit(X, y)