class feature_engine.creation.RelativeFeatures(variables, reference, func, missing_values='ignore', drop_original=False)[source]#

RelativeFeatures() applies basic mathematical operations between a group of variables and one or more reference features. It adds the resulting features to the dataframe.

In other words, RelativeFeatures() adds, subtracts, multiplies, performs the division, true division, floor division, module or exponentiation of a group of features to / by a group of reference variables. The features resulting from these functions are added to the dataframe.

This transformer works only with numerical variables. It uses the pandas methods pd.DataFrme.add, pd.DataFrme.sub, pd.DataFrme.mul, pd.DataFrme.div, pd.DataFrme.truediv, pd.DataFrme.floordiv, pd.DataFrme.mod and pd.DataFrme.pow. Find out more in pandas documentation.

More details in the User Guide.

variables: list

The list of numerical variables to combine with the reference variables.

reference: list

The list of reference variables that will be added, subtracted, multiplied, used as denominator for division and module, or exponent for the exponentiation.

func: list

The list of functions to be used in the transformation. The list can contain one or more of the following strings: ‘add’, ‘mul’,’sub’, ‘div’, truediv, ‘floordiv’, ‘mod’, ‘pow’.

missing_values: string, default=’raise’

Indicates if missing values should be ignored or raised. If ‘raise’ the transformer will return an error if the the datasets to fit or transform contain missing values. If ‘ignore’, missing data will be ignored when learning parameters or performing the transformation.

drop_original: bool, default=False

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


List with the names of features seen during fit.


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


Although the transformer allows us to combine any feature with any function, we recommend its use to create domain knowledge variables. Typical examples within the financial sector are:

  • Ratio between income and debt to create the debt_to_income_ratio.

  • Subtraction of rent from income to obtain the disposable_income.



This transformer does not learn parameters.


Fit to data, then transform it.


Get output feature names for transformation.


Get parameters for this estimator.


Set the parameters of this estimator.


Create new features.

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

This transformer does not learn any parameter.

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, or np.array. 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.

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


Additional fit parameters.

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.


Get output feature names for transformation.

input_features: bool, default=None

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

feature_names_out: list

The feature names.


Get parameters for this estimator.

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.


Parameter names mapped to their values.


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.


Estimator parameters.

selfestimator instance

Estimator instance.


Add new features.

X: pandas dataframe of shape = [n_samples, n_features]

The data to transform.

X_new: Pandas dataframe

The input dataframe plus the new variables.