SklearnTransformerWrapper#

class feature_engine.wrappers.SklearnTransformerWrapper(transformer, variables=None)[source]#

Wrapper to apply Scikit-learn transformers to a selected group of variables. It works with transformers like the SimpleImputer, OrdinalEncoder, OneHotEncoder, all the scalers and also the transformers for feature selection.

More details in the User Guide.

Parameters
transformer: sklearn transformer

The desired Scikit-learn transformer.

variables: list, default=None

The list of variables to be transformed. If None, the wrapper will select all variables of type numeric for all transformers, except the SimpleImputer, OrdinalEncoder and OneHotEncoder, in which case, it will select all variables in the dataset.

Attributes
transformer_:

The fitted Scikit-learn transformer.

variables_:

The group of variables that will be transformed.

n_features_in_:

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

See also

sklearn.compose.ColumnTransformer

Notes

This transformer offers similar functionality to the ColumnTransformer from Scikit-learn, but it allows entering the transformations directly into a Pipeline.

Methods

fit:

Fit Scikit-learn transformer

transform:

Transform data with the Scikit-learn transformer

fit_transform:

Fit to data, then transform it.

fit(X, y=None)[source]#

Fits the Scikit-learn transformer to the selected variables.

Parameters
X: Pandas DataFrame

The dataset to fit the transformer.

y: pandas Series, default=None

The target variable.

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]#

Apply the transformation to the dataframe. Only the selected variables will be modified.

Note

If the Scikit-learn transformer is the OneHotEncoder, the dummy features will be concatenated to the input dataset. Note that the original categorical variables will not be removed from the dataset after encoding. If this is the desired effect, please use Feature-engine’s OneHotEncoder instead.

Parameters
X: Pandas DataFrame

The data to transform

Returns
X_new: Pandas DataFrame

The transformed dataset.

:rtype:py:class:~pandas.core.frame.DataFrame