SelectByInformationValue#

class feature_engine.selection.SelectByInformationValue(variables=None, bins=5, strategy='equal_width', threshold=0.2, confirm_variables=False)[source]#

SelectByInformationValue() selects features based on their information value (IV). The IV is calculated as:

\[IV = ∑ (fraction of positive cases - fraction of negative cases) * WoE\]

where:

  • the fraction of positive cases is the proportion of observations of class 1,

    from the total class 1 observations.

  • the fraction of negative cases is the proportion of observations of class 0,

    from the total class 0 observations.

  • WoE is the weight of the evidence.

SelectByInformationValue() is only suitable to select features for binary classification.

SelectByInformationValue() can determine the IV for numerical and categorical variables. For numerical variables, it first sorts the variables into intervals, and then determines the IV.

You can pass a list of variables to examine. Alternatively, the transformer will examine all variables.

The IV allows you to assess each variable’s independent contribution to the target variable. The transformer selects those variables whose IV is higher 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 (except datetime).

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 the bins should be of equal width (‘equal_width’) or equal frequency (‘equal_frequency’).

threshold: float, int, default = 0.2.

The threshold to drop a feature. If the IV for a feature is < threshold, the feature will be dropped.

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
variables_:

The group of variables that will be transformed.

information_values_:

A dictionary with the information values for each feature.

features_to_drop_:

List with the features that will be removed.

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

Weight of evidence and information value explained https://www.listendata.com/2015/03/weight-of-evidence-woe-and-information.html

2

WoE and IV for continuous variables https://www.listendata.com/2019/08/WOE-IV-Continuous-Dependent.html

Examples

>>> import pandas as pd
>>> from feature_engine.selection import SelectByInformationValue
>>> X = pd.DataFrame(dict(x1 = [1,1,1,1,1,1],
>>>                     x2 = [3,2,2,3,3,2],
>>>                     x3 = ["a","b","c","a","c","b"]))
>>> y = pd.Series([1,1,1,0,0,0])
>>> iv = SelectByInformationValue()
>>> iv.fit_transform(X, y)
    x2
0   3
1   2
2   2
3   3
4   3
5   2

Methods

fit:

Find features with high information value.

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:

Remove features with low information value.

fit(X, y)[source]#

Learn the information value. Find features with IV above the threshold.

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

The training input samples.

y: pandas series of shape = [n_samples, ]

Target, must be binary.

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