LogCpTransformer¶
API Reference¶
- class feature_engine.transformation.LogCpTransformer(variables=None, base='e', C='auto')[source]¶
The LogCpTransformer() applies the transformation log(x + C), where C is a positive constant, to the input variable. It applies the natural logarithm or the base 10 logarithm, where the natural logarithm is logarithm in base e.
The logarithm can only be applied to numerical non-negative values. If the variable contains a zero or a negative value after adding a constant C, 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.
- Parameters
- variables: list, default=None
The list of numerical variables to transform. If None, the transformer will find and select all numerical variables. If C is a dictionary, then this parameter is ignored and the variables to transform are selected from the dictionary keys.
- base: string, default=’e’
Indicates if the natural or base 10 logarithm should be applied. Can take values ‘e’ or ‘10’.
- C: “auto”, int or dict, default=”auto”
The constant C to add to the variable before the logarithm, i.e., log(x + C).
If int, then log(x + C)
If “auto”, then C = abs(min(x)) + 1
If dict, dictionary mapping the constant C to apply to each variable.
Note, when C is a dictionary, the parameter
variables
is ignored.
Attributes
variables_:
The group of variables that will be transformed.
C_:
The constant C to add to each variable. If C = “auto” a dictionary with C = abs(min(variable)) + 1.
n_features_in_:
The number of features in the train set used in fit.
Methods
fit:
Learn the constant C.
transform:
Transform the variables with the logarithm of x plus C.
fit_transform:
Fit to data, then transform it.
inverse_transform:
Convert the data back to the original representation.
- fit(X, y=None)[source]¶
Learn the constant C to add to the variable before the logarithm transformation if C=”auto”.
Select the numerical variables or check that the variables entered by the user are numerical. Then check that the selected variables are positive after addition of C.
- 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.
- 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
If some variables contain zero or negative values after adding C
- 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: Pandas dataframe
The dataframe with the transformed variables.
- rtype
DataFrame
..
- Raises
- TypeError
If the input is not a Pandas DataFrame
- ValueError
If the variable(s) contain null values
If the df has different number of features than the df used in fit()
- transform(X)[source]¶
Transform the variables with the logarithm of x plus a constant C.
- Parameters
- X: Pandas DataFrame of shape = [n_samples, n_features]
The data to be transformed.
- Returns
- X: pandas dataframe
The dataframe with the transformed variables.
- rtype
DataFrame
..
- Raises
- TypeError
If the input is not a Pandas DataFrame
- ValueError
If the variable(s) contain null values
If the df has different number of features than the df used in fit()
If some variables contains zero or negative values after adding C
Example¶
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_boston
from feature_engine import transformation as vt
# Load dataset
X, y = load_boston(return_X_y=True)
X = pd.DataFrame(X)
# Separate into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
# set up the variable transformer
tf = vt.LogCpTransformer(variables = [7, 12], C="auto")
# fit the transformer
tf.fit(X_train)
# transform the data
train_t= tf.transform(X_train)
test_t= tf.transform(X_test)
# learned constant C
tf.C_
{7: 2.1742, 12: 2.73}
# un-transformed variable
X_train[12].hist()

# transformed variable
train_t[12].hist()
