MeanNormalizationScaler#

class feature_engine.scaling.MeanNormalizationScaler(variables=None)[source]#

MeanNormalizationScaler() applies mean normalization, which consists of subtracting the mean of each feature and then dividing the result by the value range, that is, the difference between its maximum and minimum value. The method aims to center the variables at 0, and rescale the distribution between -1 and 1.

A list of variables can be passed as an argument. Alternatively, the transformer will automatmypy featureically select and transform all variables of type numeric.

Constant variables will raise an error due to division by zero.

More details in the User Guide.

Parameters
variables: list, default=None

The list of numerical variables to transform. If None, the transformer will automatically find and select all numerical variables.

Attributes
mean_:

Dictionary containing the mean of the variables.

range_:

Dictionary containing the value range of of the variables.

variables_:

The group of variables that will be transformed.

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.

Examples

>>> import numpy as np
>>> import pandas as pd
>>> from feature_engine.scaling import MeanNormalizationScaler
>>> np.random.seed(42)
>>> X = pd.DataFrame(dict(x = np.random.lognormal(size = 100)))
>>> mns = MeanNormalizationScaler()
>>> mns.fit(X)
>>> X = mns.transform(X)
>>> X.head()
        x
0  0.496714
1 -0.138264
2  0.647689
3  1.523030
4 -0.234153

Methods

fit:

Find variables’ mean and value range.

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.

inverse_transform:

Convert the data back to the original representation.

transform:

Scale the variables using mean normalization.

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

Finds the mean and value range of each variable.

Parameters
X: Pandas DataFrame of shape = [n_samples, n_features].

The training input samples. Can be the entire dataframe, not just the variables to transform.

y: pandas Series, default=None

It is not needed in this transformer. You can pass y or None.

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.

inverse_transform(X)[source]#

Convert the data back to the original representation.

Parameters
X: Pandas DataFrame of shape = [n_samples, n_features]

The data to be transformed.

Returns
X_tr: pandas dataframe

The dataframe with the transformed variables.

rtype

DataFrame ..

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

Transform the variables using mean normalization.

Parameters
X: Pandas DataFrame of shape = [n_samples, n_features]

The data to be transformed.

Returns
X_new: pandas dataframe

The dataframe with the transformed variables.

rtype

DataFrame ..