PowerTransformer

API Reference

class feature_engine.transformation.PowerTransformer(exp=0.5, variables=None)[source]

The PowerTransformer() applies power or exponential transformations to numerical variables.

The PowerTransformer() works only with numerical variables.

A list of variables can be passed as an argument. Alternatively, the transformer will automatically select and transform all numerical variables.

Parameters
variableslist, default=None

The list of numerical variables that will be transformed. If None, the transformer will automatically find and select all numerical variables.

expfloat or int, default=0.5

The power (or exponent).

Methods

fit:

This transformer does not learn parameters.

transform:

Apply the power transformation to the variables.

fit_transform:

Fit to data, then transform it.

fit(X, y=None)[source]

This transformer does not learn parameters.

Parameters
Xpandas dataframe of shape = [n_samples, n_features]

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

ypandas Series, default=None

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

Returns
self
Raises
TypeError
  • If the input is not a Pandas DataFrame

  • If any of the user provided variables are not numerical

ValueError
  • If there are no numerical variables in the df or the df is empty

  • If the variable(s) contain null values

transform(X)[source]

Apply the power transformation to the variables.

Parameters
XPandas DataFrame of shape = [n_samples, n_features]

The data to be transformed.

Returns
Xpandas Dataframe

The dataframe with the power transformed variables.

rtype

DataFrame ..

Raises
TypeError

If the input is not a Pandas DataFrame

ValueError
  • If the variable(s) contain null values.

  • If the dataframe not of the same size as that used in fit().

Example

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

from feature_engine import transformation as vt

# Load dataset
data = data = pd.read_csv('houseprice.csv')

# Separate into train and test sets
X_train, X_test, y_train, y_test =  train_test_split(
            data.drop(['Id', 'SalePrice'], axis=1),
            data['SalePrice'], test_size=0.3, random_state=0)

# set up the variable transformer
tf = vt.PowerTransformer(variables = ['LotArea', 'GrLivArea'], exp=0.5)

# fit the transformer
tf.fit(X_train)

# transform the data
train_t= tf.transform(X_train)
test_t= tf.transform(X_test)

# un-transformed variable
X_train['LotArea'].hist(bins=50)
../_images/lotarearaw.png
# transformed variable
train_t['LotArea'].hist(bins=50)
../_images/lotareapower.png