LogTransformer

class feature_engine.transformation.LogTransformer(variables=None, base='e')[source]

The LogTransformer() applies the natural logarithm or the base 10 logarithm to numerical variables. The natural logarithm is the logarithm in base e.

The LogTransformer() only works with positive values. If the variable contains a zero or a negative value the transformer will return an error.

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

More details in the User Guide.

Parameters
variables: list, default=None

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

base: string, default=’e’

Indicates if the natural or base 10 logarithm should be applied. Can take values ‘e’ or ‘10’.

Attributes
variables_:

The group of variables that will be transformed.

n_features_in_:

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

Methods

fit:

This transformer does not learn parameters.

transform:

Transform the variables using the logarithm.

fit_transform:

Fit to data, then transform it.

inverse_transform:

Convert the data back to the original representation.

fit(X, y=None)[source]

This transformer does not learn parameters.

Selects the numerical variables and determines whether the logarithm can be applied on the selected variables, i.e., it checks that the variables are positive.

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_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 with the logarithm.

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