ProbeFeatureSelection#

class feature_engine.selection.ProbeFeatureSelection(estimator, variables=None, collective=True, scoring='roc_auc', n_probes=1, distribution='normal', n_categories=10, threshold='mean', cv=5, groups=None, random_state=0, confirm_variables=False)[source]#

ProbeFeatureSelection() generates one or more probe (i.e., random) features based on a user-selected distribution. The distribution options are ‘normal’, ‘binomial’, ‘uniform’, ‘discrete_uniform’, ‘poisson’, or ‘all’. ‘all’ creates n_probes of each of the five aforementioned distributions.

Using cross validation, ProbeFeatureSelection() fits a Scikit-learn estimator to the provided variables plus the probe features. Next, it derives the feature importance for each variable and probe feature from the fitted model.

Alternatively, ProbeFeatureSelection() fits a Scikit-learn estimator per feature and probe feature (single feature models), and then determines the performance returned by that model using a metric of choice.

Finally, ProbeFeatureSelection() selects the features whose importance is greater than those of the probes. In the case of there being more than one probe feature, ProbeFeatureSelection() can take the average, maximum, or mean plus 3 std feature importance of all the probe features as threshold for the feature selection.

The variables whose feature importance is smaller than the feature importance of the probe feature(s) are dropped from the dataset.

More details in the User Guide.

Parameters
estimator: object

A Scikit-learn estimator for regression or classification. If collective=True, the estimator must have either a feature_importances_ or a coef_ 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.

collective: bool, default=True

Whether the feature importance should be derived from an estimator trained on the entire dataset (True), or trained using individual features (False).

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

n_probes: int, default=1

Number of probe features to create per distribution.

distribution: str, list, default=’normal’

The distribution used to create the probe features. The options are ‘normal’, ‘binomial’, ‘uniform’, ‘discrete_uniform’, ‘poisson’ and ‘all’. ‘all’ creates n_probes features per distribution type, i.e., normal, binomial, uniform, discrete_uniform and poisson. The remaining options create n_probes features per selected distributions.

n_categories: int, default=10

If distribution is ‘discrete_uniform’ then integers are sampled from 0 to n_categories. If distribution is ‘poisson’, then samples are taken from np.random.poisson(n_categories, n_obs).

threshold: str, default=’mean’

Indicates how to combine the importance of the probe features as threshold for the feature selection. If ‘mean’, then features are selected if their importance is greater than the mean of the probes. If ‘max’, then features are selected if their importance is greater than the maximun importance of all probes. If ‘mean_plus_std’, then features are selected if their importance is greater than the mean plus three times the standard deviation of the probes.

cv: int, cross-validation generator or an iterable, default=3

Determines the cross-validation splitting strategy. Possible inputs for cv are:

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’s cross_validate’s documentation.

groups: array-like of shape (n_samples,), default=None

Group labels for the samples used while splitting the dataset into train/test set. Only used in conjunction with a “Group” cv instance (e.g., GroupKFold).

Attributes
probe_features_:

A dataframe comprised of the pseudo-randomly generated features based on the selected distribution.

feature_importances_:

Pandas Series with the feature importance. If collective=True, the feature importance is given by the coefficients of linear models or the importance derived from tree-based models. If collective=False, the feature importance is given by a performance metric returned by a model trained using that individual feature.

feature_importances_std_:

Pandas Series with the standard deviation of the feature importance.

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.

References

1

Stoppiglia, et al. “Ranking a Random Feature for Variable and Feature Selection”. JMLR: 1399-1414, 2003 https://dl.acm.org/doi/pdf/10.5555/944919.944980

Examples

>>> from sklearn.datasets import load_breast_cancer
>>> from sklearn.linear_model import LogisticRegression
>>> from feature_engine.selection import ProbeFeatureSelection
>>> X, y = load_breast_cancer(return_X_y=True, as_frame=True)
>>> sel = ProbeFeatureSelection(
>>>     estimator=LogisticRegression(max_iter=1000000),
>>>     scoring="roc_auc",
>>>     n_probes=3,
>>>     distribution="normal",
>>>     cv=3,
>>>     random_state=150,
>>> )
>>> X_tr = sel.fit_transform(X, y)
>>> print(X.shape, X_tr.shape)
(569, 30) (569, 19)

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.

get_support:

Get a mask, or integer index, of the features selected.

transform:

Reduce X to the selected features.

fit(X, y)[source]#

Find the important features.

Parameters
X: pandas dataframe of shape = [n_samples, n_features]
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 and y with optional parameters fit_params and returns a transformed version of X.

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. Pass only if the estimator accepts additional params in its fit method.

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. In other words, returns the variable names of transformed dataframe.

Parameters
input_featuresarray or list, default=None

This parameter exits only for compatibility with the Scikit-learn pipeline.

  • If None, then feature_names_in_ is used as feature names in.

  • If an array or list, then input_features must match feature_names_in_.

Returns
feature_names_out: list

Transformed feature names.

rtype

List[Union[str, int]] ..

get_metadata_routing()[source]#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns
routingMetadataRequest

A MetadataRequest encapsulating routing information.

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.

get_support(indices=False)[source]#

Get a mask, or integer index, of the features selected.

Parameters
indicesbool, default=False

If True, the return value will be an array of integers, rather than a boolean mask.

Returns
supportarray

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 if its corresponding feature is selected for retention. If indices is True, this is an integer array of shape [# output features] whose values are indices into the input feature vector.

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

DataFrame ..