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.

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.


List with the features to remove from the dataset.


Dictionary with the performance proxy per feature.


The variables to consider for the feature selection.


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


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



Miller, et al. “Predicting customer behaviour: The University of Melbourne’s KDD Cup report”. JMLR Workshop and Conference Proceeding. KDD 2009



Find the important features.


Reduce X to the selected features.


Fit to data, then transform it.

fit(X, y)[source]

Find the important features.

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.

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


Additional fit parameters.

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.


Get parameters for this estimator.

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.


Parameter names mapped to their values.


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.

selfestimator instance

Estimator instance.


Return dataframe with selected features.

X: pandas dataframe of shape = [n_samples, n_features].

The input dataframe.

X_new: pandas dataframe of shape = [n_samples, n_selected_features]

Pandas dataframe with the selected features.


DataFrame ..