RecursiveFeatureAddition#
- class feature_engine.selection.RecursiveFeatureAddition(estimator, scoring='roc_auc', cv=3, threshold=0.01, variables=None, confirm_variables=False)[source]#
RecursiveFeatureAddition() selects features following a recursive addition process.
The process is as follows:
Train an estimator using all the features.
Rank the features according to their importance derived from the estimator.
Train an estimator with the most important feature and determine performance.
Add the second most important feature and train a new estimator.
Calculate the difference in performance between estimators.
If the performance increases beyond the threshold, the feature is kept.
Repeat steps 4-6 until all features have been evaluated.
Model training and performance calculation are done with cross-validation.
More details in the User Guide.
- Parameters
- estimator: object
A Scikit-learn estimator for regression or classification. The estimator must have either a
feature_importances
or acoef_
attribute after fitting.- variables: str or list, default=None
The list of variables to evaluate. If None, the transformer will evaluate all numerical features in the dataset.
- scoring: str, default=’roc_auc’
Metric to evaluate the performance of the estimator. Comes from sklearn.metrics. See the model evaluation documentation for more options: https://scikit-learn.org/stable/modules/model_evaluation.html
- threshold: float, int, default = 0.01
The value that defines whether a feature will be selected. Note that for metrics like the roc-auc, r2, and the accuracy, the threshold will be a float between 0 and 1. For metrics like the mean squared error and the root mean squared error, the threshold can take any number. The threshold must be defined by the user. With bigger thresholds, fewer features will be selected.
- cv: int, cross-validation generator or an iterable, default=3
Determines the cross-validation splitting strategy. Possible inputs for cv are:
None, to use cross_validate’s default 5-fold cross validation
int, to specify the number of folds in a (Stratified)KFold,
CV splitter: (https://scikit-learn.org/stable/glossary.html#term-CV-splitter)
An iterable yielding (train, test) splits as arrays of indices.
For int/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. For more details check Scikit-learn’scross_validate
’s documentation.- confirm_variables: bool, default=False
If set to True, variables that are not present in the input dataframe will be removed from the list of variables. Only used when passing a variable list to the parameter
variables
. See parameter variables for more details.
- Attributes
- initial_model_performance_:
The model’s performance when trained with the original dataset.
- feature_importances_:
Pandas Series with the feature importance (comes from step 2)
- performance_drifts_:
Dictionary with the performance drift per examined feature (comes from step 5).
- features_to_drop_:
List with the features that will be removed.
- variables_:
The variables that will be considered for the feature selection procedure.
- feature_names_in_:
List with the names of features seen during
fit
.- n_features_in_:
The number of features in the train set used in fit.
Methods
fit:
Find the important features.
fit_transform:
Fit to data, then transform it.
get_feature_names_out:
Get output feature names for transformation.
get_params:
Get parameters for this estimator.
set_params:
Set the parameters of this estimator.
transform:
Reduce X to the selected features.
- fit(X, y)[source]#
Find the important features. Note that the selector trains various models at each round of selection, so it might take a while.
- Parameters
- X: pandas dataframe of shape = [n_samples, n_features]
The input dataframe
- y: array-like of shape (n_samples)
Target variable. Required to train the estimator.
- fit_transform(X, y=None, **fit_params)[source]#
Fit to data, then transform it.
Fits transformer to
X
andy
with optional parametersfit_params
and returns a transformed version ofX
.- Parameters
- Xarray-like of shape (n_samples, n_features)
Input samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None
Target values (None for unsupervised transformations).
- **fit_paramsdict
Additional fit parameters.
- Returns
- X_newndarray array of shape (n_samples, n_features_new)
Transformed array.
- get_feature_names_out(input_features=None)[source]#
Get output feature names for transformation.
- input_features: None
This parameter exists only for compatibility with the Scikit-learn pipeline, but has no functionality. You can pass a list of feature names or None.
- Returns
- feature_names_out: list
The feature names.
- :rtype:py:class:
~typing.List
- get_params(deep=True)[source]#
Get parameters for this estimator.
- Parameters
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns
- paramsdict
Parameter names mapped to their values.
- set_params(**params)[source]#
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.- Parameters
- **paramsdict
Estimator parameters.
- Returns
- selfestimator instance
Estimator instance.
- transform(X)[source]#
Return dataframe with selected features.
- Parameters
- X: pandas dataframe of shape = [n_samples, n_features].
The input dataframe.
- Returns
- X_new: pandas dataframe of shape = [n_samples, n_selected_features]
Pandas dataframe with the selected features.
- :rtype:py:class:
~pandas.core.frame.DataFrame