SequentialFeatureSelector
- class sklearn.feature_selection.SequentialFeatureSelector(estimator, *, n_features_to_select='auto', tol=None, direction='forward', scoring=None, cv=5,
Transformer that performs Sequential Feature Selection.
This Sequential Feature Selector adds (forward selection) or removes (backward selection) features to form a feature subset in a greedy fashion. At each stage, this estimator chooses the best feature to add or remove based on the cross-validation score of an estimator. In the case of unsupervised learning, this Sequential Feature Selector looks only at the features (X), not the desired outputs (y).
Read more in the User Guide.
An unfitted estimator.
If "auto", the behaviour depends on the tol parameter:
if tol is not None, then features are selected while the score
change does not exceed tol.
otherwise, half of the features are selected.
If integer, the parameter is the absolute number of features to select. If float between 0 and 1, it is the fraction of features to select.
If the score is not incremented by at least tol between two
consecutive feature additions or removals, stop adding or removing.
tol can be negative when removing features using direction="backward".
tol is required to be strictly positive when doing forward selection.
It can be useful to reduce the number of features at the cost of a small
decrease in the score.
tol is enabled only when n_features_to_select is "auto".
Whether to perform forward selection or backward selection.
Scoring method to use for cross-validation. Options:
str: see String name scorers for options.
callable: a scorer callable object (e.g., function) with signature
scorer(estimator, X, y) that returns a single value.
See Callable scorers for details.
None: the estimator’s
default evaluation criterion is used.
Determines the cross-validation splitting strategy. Possible inputs for cv are:
None, to use the default 5-fold cross validation,
integer, to specify the number of folds in a (Stratified)KFold,
an iterable yielding (train, test) splits as arrays of indices.
For integer/None inputs, if the estimator is a classifier and y is
either binary or multiclass,
StratifiedKFold is used. In all other
cases, KFold is used. These splitters
are instantiated with shuffle=False so the splits will be the same
across calls.
Number of jobs to run in parallel. When evaluating a new feature to
add or remove, the cross-validation procedure is parallel over the
folds.
None means 1 unless in a joblib.parallel_backend context.
-1 means using all processors. See Glossary
for more details.
>>> from sklearn.feature_selection import SequentialFeatureSelector
>>> from sklearn.neighbors import KNeighborsClassifier
>>> from sklearn.datasets import load_iris
>>> X, y = load_iris(return_X_y=True)
>>> knn = KNeighborsClassifier(
Learn the features to select from X.
Training vectors, where n_samples is the number of samples and
n_features is the number of predictors.
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params
and returns a transformed version of X.
Mask feature names according to selected features.
Input features.
If input_features is None, then feature_names_in_ is
used as feature names in. If is not defined,
then the following input feature names are generated:
.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Get a mask, or integer index, of the features selected.
If True, the return value will be an array of integers, rather than a boolean mask.
An index that selects the retained features from a feature vector.
If indices is False, this is a boolean array of shape
[# input features], in which an element is True iff its
corresponding feature is selected for retention. If is
True, this is an integer array of shape [# output features] whose
values are indices into the input feature vector.
Reverse the transformation operation.
Set output container.
Refer to the user guide for more details and Introducing the set_output API for an example on how to use the API.
Configure output of transform and .
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as Pipeline). The latter have
parameters of the form <component>__<parameter> so that it’s
possible to update each component of a nested object.
Estimator parameters.
Reduce X to the selected features.
The input samples.
The input samples with only the selected features.
Refer User Guide for the various cross-validation strategies that can be used here.
Added in version 1.0.
The number of features that were selected.
The mask of selected features.
SelectFromModelFeature selection based on thresholds of importance weights.
Target values. This parameter may be ignored for unsupervised learning.
Parameters to be passed to the underlying estimator, cv
and scorer objects.
Returns the instance itself.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Pass only if the estimator accepts additional params in its fit method.
Transformed array.
["x0", "x1", ..., "x(n_features_in_ - 1)"]If input_features is an array-like, then input_features must
match feature_names_in_ if feature_names_in_ is defined.
Transformed feature names.
indicestransform"default": Default output format of a transformer
"pandas": DataFrame output
"polars": Polars output
None: Transform configuration is unchanged
Estimator instance.
Estimator instance.