SelectByTargetMeanPerformance#

class feature_engine.selection.SelectByTargetMeanPerformance(variables=None, scoring='roc_auc_score', threshold=0.5, bins=5, strategy='equal_width', cv=3, random_state=None)[source]#

SelectByTargetMeanPerformance() uses the mean value of the target per category, or interval if the variable is numerical, as proxy for target estimation. With this proxy and the real target, the selector determines a performance metric for each feature, and then selects them based on this performance metric.

SelectByTargetMeanPerformance() works with numerical and categorical variables. First, it eparates the training set into train and test sets. Then it works as follows:

For each categorical variable:

  1. Determines the mean target value per category using the train set.

  2. Replaces the categories in the test set by the target mean values.

  3. Using the encoded variables and the real target calculates the roc-auc or r2.

  4. Selects the features which roc-auc or r2 is bigger than the threshold.

For each numerical variable:

  1. Discretises the variable into intervals of equal width or equal frequency.

  2. Determines the mean value of the target per interval using the train set.

  3. Replaces the intervals in the test set, by the target mean values.

  4. Using the transformed variable and the real target calculates the roc-auc or r2.

  5. Selects the features which roc-auc or r2 is bigger than the threshold.

More details in the User Guide.

Parameters
variables: list, default=None

The list of variables to evaluate. If None, the transformer will evaluate all variables in the dataset.

scoring: string, default=’roc_auc_score’

This indicates the metrics score to perform the feature selection. The current implementation supports ‘roc_auc_score’ and ‘r2_score’.

threshold: float, default = None

The performance threshold above which a feature will be selected.

bins: int, default = 5

If the dataset contains numerical variables, the number of bins into which the values will be sorted.

strategy: str, default = ‘equal_width’

Whether to create the bins for discretization of numerical variables of equal width (‘equal_width’) or equal frequency (‘equal_frequency’).

cv: int, default=3

Desired cross-validation strategy to fit the estimator.

random_state: int, default=0

The random state used to split the data into train and test.

Attributes
features_to_drop_:

List with the features to remove from the dataset.

feature_performance_:

Dictionary with the performance proxy per feature.

variables_:

The variables to consider for the feature selection.

n_features_in_:

The number of features in the train set used in fit.

Notes

Replacing the categories or intervals by the target mean is the equivalent to target mean encoding.

References

1

Miller, et al. “Predicting customer behaviour: The University of Melbourne’s KDD Cup report”. JMLR Workshop and Conference Proceeding. KDD 2009 http://proceedings.mlr.press/v7/miller09/miller09.pdf

Methods

fit:

Find the important features.

transform:

Reduce X to the selected features.

fit_transform:

Fit to data, then transform it.

fit(X, y)[source]#

Find the important features.

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 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.

Returns
X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

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