CyclicalFeatures

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

CyclicalFeatures() 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…

CyclicalFeatures() 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 using 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 feature’s maximum values.

variables_:

The group of variables that will be transformed.

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.

References

https://ianlondon.github.io/blog/encoding-cyclical-features-24hour-time/ https://towardsdatascience.com/cyclical-features-encoding-its-about-time-ce23581845ca http://blog.davidkaleko.com/feature-engineering-cyclical-features.html

Methods

fit:

Learns the variable’s maximum values.

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.

transform:

Adds new features.

fit(X, y=None)[source]

Learns the maximum value of each 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_feature_names_out(input_features=None)[source]

Get output feature names for transformation.

Parameters
input_features: list, default=None

Input features. If input_features is None, then the names of all the variables in the transformed dataset (original + new variables) is returned. Alternatively, only the names for the new features derived from input_features will be returned.

Returns
feature_names_out: list

The feature names.

:rtype:py:class:~typing.List
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 transformations.

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.