LogCpTransformer#
- class feature_engine.transformation.LogCpTransformer(variables=None, base='e', C='auto')[source]#
LogCpTransformer() applies the transformation log(x + C), where x is the variable to transform and C is a positive constant. It can apply the natural logarithm or the base 10 logarithm, where the natural logarithm is logarithm in base e.
As the logarithm can only be applied to numerical non-negative values, LogCpTransformer() extends the functionality of LogTransformer, by adding a constant to shift the distribution of the variables towards positive values.
Note that if the variable contains a zero or a negative value after adding a constant C, the transformer will return an error. This can occur if the values of the variables in the test set are smaller than those seen during
fit().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. 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
variablesis 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. For strictly positive variables, C = 0.
- 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 pandas as pd >>> from feature_engine.transformation import LogCpTransformer >>> X = pd.DataFrame(dict( >>> vara=[0, 1, 2, 3], >>> varb=[5, 5, 6, 7], >>> varc=[-2, -1, 0, 4], >>> vard=[-3, -2, -1, -5], >>> vare=["a", "b", "c", "d"])) >>> lct = LogCpTransformer() >>> lct.fit(X) >>> X = lct.transform(X) >>> X vara varb varc vard vare 0 0.000000 1.609438 0.000000 1.098612 a 1 0.693147 1.609438 0.693147 1.386294 b 2 1.098612 1.791759 1.098612 1.609438 c 3 1.386294 1.945910 1.945910 0.000000 d
Methods
fit:
Learn the constant C.
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:
Transform the variables with the logarithm of x plus C.
- 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.
- fit_transform(X, y=None, **fit_params)[source]#
Fit to data, then transform it.
Fits transformer to
Xandywith optional parametersfit_paramsand returns a transformed version ofX.- 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. Pass only if the estimator accepts additional params in its
fitmethod.
- 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, thenfeature_names_in_is used as feature names in.If an array or list, then
input_featuresmust matchfeature_names_in_.
- Returns
- feature_names_out: list
Transformed feature names.
- get_metadata_routing()[source]#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns
- routingMetadataRequest
A
MetadataRequestencapsulating 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.