CyclicalTransformer#

class feature_engine.creation.CyclicalTransformer(variables=None, max_values=None, drop_original=False)[source]#

The CyclicalTransformer() applies cyclical transformations to numerical variables, returning 2 new features per variable, according to:

  • var_sin = sin(variable * (2. * pi / max_value))

  • var_cos = cos(variable * (2. * pi / max_value))

where max_value is the maximum value in the variable, and pi is 3.14…

The CyclicalTransformer() works only with numerical variables. A list of variables to transform can be passed as an argument. Alternatively, the transformer will automatically select and transform all numerical variables.

Missing data should be imputed before applying this transformer.

More details in the User Guide.

Parameters
variables: list, default=None

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

max_values: dict, default=None

A dictionary with the maximum value of each variable to transform. Useful when the maximum value is not present in the dataset. If None, the transformer will automatically find the maximum value of each variable.

drop_original: bool, default=False

If True, the original variables to transform will be dropped from the dataframe.

Attributes
max_values_:

The maximum value of the cyclical feature.

variables_:

The group of variables that will be transformed.

n_features_in_:

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

References

http://blog.davidkaleko.com/feature-engineering-cyclical-features.html

Methods

fit:

Learns the maximum values of the cyclical features.

transform:

Applies the cyclical transformation.

fit_transform:

Fit to data, then transform it.

fit(X, y=None)[source]#

Learns the maximum value of each cyclical variable.

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.

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]#

Creates new features using the cyclical transformation.

Parameters
X: Pandas DataFrame of shame = [n_samples, n_features]

The data to be transformed.

Returns
X_new: Pandas dataframe.

The original dataframe plus the additional features.