class feature_engine.creation.MathFeatures(variables, func, new_variables_names=None, missing_values='raise', drop_original=False)[source]#

MathFeatures(() applies functions across multiple features returning one or more additional features as a result. It uses pandas.agg() to create the features, setting axis=1.

For supported aggregation functions, see pandas documentation.

Note that if some of the variables have missing data and missing_values='ignore', the value will be ignored in the computation. To be clear, if variables A, B and C, have values 10, 20 and NA, and we perform the sum, the result will be A + B = 30.

More details in the User Guide.

variables: list

The list of input variables. Variables must be numerical and there must be at least 2 different variables in the list.

func: function, string, list

Functions to use for aggregating the data. Same functionality as parameter func in pandas.agg(). If a function, it must either work when passed a DataFrame or when passed to DataFrame.apply. Accepted combinations are:

  • function

  • string function name

  • list of functions and/or function names, e.g. [np.sum, ‘mean’]

Each function will result in a new variable that will be added to the transformed dataset.

new_variables_names: list, default=None

Names of the new variables. If passing a list with names (recommended), enter one name per function. If None, the transformer will assign arbitrary names, starting with the function and followed by the variables separated by _.

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.


The group of variables that will be transformed.


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 features with any functions, we recommend using it to create features based on domain knowledge. Typical examples in finance are:

  • Sum debt across financial products, i.e., credit cards, to obtain the total debt.

  • Take the average payments to various financial products.

  • Find the minimum payment done at any one month.

In insurance, we can sum the damage to various parts of a car to obtain the total damage.


>>> import pandas as pd
>>> from feature_engine.creation import MathFeatures
>>> X = pd.DataFrame(dict(x1 = [1,2,3], x2 = [4,5,6]))
>>> mf = MathFeatures(variables = ["x1","x2"], func = "sum")
>>> mf.transform(X)
   x1  x2  sum_x1_x2
0   1   4          5
1   2   5          7
2   3   6          9
>>> mf = MathFeatures(variables = ["x1","x2"], func = "prod")
>>> mf.transform(X)
   x1  x2  prod_x1_x2
0   1   4           4
1   2   5          10
2   3   6          18
>>> mf = MathFeatures(variables = ["x1","x2"], func = "mean")
>>> mf.transform(X))
   x1  x2  mean_x1_x2
0   1   4         2.5
1   2   5         3.5
2   3   6         4.5



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

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

The training input samples.

y: pandas Series, or np.array. Defaults to 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. In other words, returns the variable names of transformed dataframe.

input_featuresarray or list, default=None

This parameter exits only for compatibility with the Scikit-learn pipeline.

  • If None, then feature_names_in_ is used as feature names in.

  • If an array or list, then input_features must match feature_names_in_.

feature_names_out: list

Transformed feature names.


List[Union[str, int]] ..


Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.


A MetadataRequest encapsulating routing information.


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.


Create and add new variables.

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

The data to transform.

X_new: Pandas dataframe, shape = [n_samples, n_features + n_operations]

The input dataframe plus the new variables.


DataFrame ..