sklearn.preprocessing.PowerTransformer

class sklearn.preprocessing.PowerTransformer(method=’box-cox’, standardize=True, copy=True)[source]

Apply a power transform featurewise to make data more Gaussian-like.

Power transforms are a family of parametric, monotonic transformations that are applied to make data more Gaussian-like. This is useful for modeling issues related to heteroscedasticity (non-constant variance), or other situations where normality is desired.

Currently, PowerTransformer supports the Box-Cox transform. Box-Cox requires input data to be strictly positive. The optimal parameter for stabilizing variance and minimizing skewness is estimated through maximum likelihood.

By default, zero-mean, unit-variance normalization is applied to the transformed data.

Read more in the User Guide.

Parameters:
method : str, (default=’box-cox’)

The power transform method. Currently, ‘box-cox’ (Box-Cox transform) is the only option available.

standardize : boolean, default=True

Set to True to apply zero-mean, unit-variance normalization to the transformed output.

copy : boolean, optional, default=True

Set to False to perform inplace computation during transformation.

Attributes:
lambdas_ : array of float, shape (n_features,)

The parameters of the power transformation for the selected features.

See also

power_transform
Equivalent function without the estimator API.
QuantileTransformer
Maps data to a standard normal distribution with the parameter output_distribution=’normal’.

Notes

For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py.

References

G.E.P. Box and D.R. Cox, “An Analysis of Transformations”, Journal of the Royal Statistical Society B, 26, 211-252 (1964).

Examples

>>> import numpy as np
>>> from sklearn.preprocessing import PowerTransformer
>>> pt = PowerTransformer()
>>> data = [[1, 2], [3, 2], [4, 5]]
>>> print(pt.fit(data))
PowerTransformer(copy=True, method='box-cox', standardize=True)
>>> print(pt.lambdas_)  
[ 1.051... -2.345...]
>>> print(pt.transform(data))  
[[-1.332... -0.707...]
 [ 0.256... -0.707...]
 [ 1.076...  1.414...]]

Methods

fit(X[, y]) Estimate the optimal parameter for each feature.
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
inverse_transform(X) Apply the inverse power transformation using the fitted lambdas.
set_params(**params) Set the parameters of this estimator.
transform(X) Apply the power transform to each feature using the fitted lambdas.
__init__(method=’box-cox’, standardize=True, copy=True)[source]
fit(X, y=None)[source]

Estimate the optimal parameter for each feature.

The optimal parameter for minimizing skewness is estimated on each feature independently. If the method is Box-Cox, the lambdas are estimated using maximum likelihood.

Parameters:
X : array-like, shape (n_samples, n_features)

The data used to estimate the optimal transformation parameters.

y : Ignored
Returns:
self : object

Returns self.

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:
X : numpy array of shape [n_samples, n_features]

Training set.

y : numpy array of shape [n_samples]

Target values.

Returns:
X_new : numpy array of shape [n_samples, n_features_new]

Transformed array.

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:
deep : boolean, optional

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

Returns:
params : mapping of string to any

Parameter names mapped to their values.

inverse_transform(X)[source]

Apply the inverse power transformation using the fitted lambdas.

The inverse of the Box-Cox transformation is given by:

if lambda == 0:
    X = exp(X_trans)
else:
    X = (X_trans * lambda + 1) ** (1 / lambda)
Parameters:
X : array-like, shape (n_samples, n_features)

The transformed data.

set_params(**params)[source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Returns:
self
transform(X)[source]

Apply the power transform to each feature using the fitted lambdas.

Parameters:
X : array-like, shape (n_samples, n_features)

The data to be transformed using a power transformation.