tubular.numeric.InteractionTransformer

class tubular.numeric.InteractionTransformer(columns, min_degree=2, max_degree=2, **kwargs)[source]

Bases: tubular.base.BaseTransformer

Transformer that generates interaction features. Transformer generates a new column for all combinations from the selected columns up to the maximum degree provided. (For sklearn version higher than 1.0.0>, only interaction of a degree higher or equal to the minimum degree would be computed). Each interaction column consists of the product of the specific combination of columns. Ex: with 3 columns provided [“a”,”b”,”c”], if max degree is 3, the total possible combinations are : - of degree 1 : [“a”,”b”,”c”] - of degree 2 : [“a b”,”b c”,”a c”] - of degree 3 : [“a b c”].

Parameters
  • columns (None or list or str) – Columns to apply the transformer to. If a str is passed this is put into a list. Value passed in columns is saved in the columns attribute on the object. Note this has no default value so the user has to specify the columns when initialising the transformer. This is avoid likely when the user forget to set columns, in this case all columns would be picked up when super transform runs.

  • min_degree (int) – minimum degree of interaction features to be considered. For example if min_degree=3, only interaction columns from at least 3 columns would be generated. NB- only applies if sklearn version is 1.0.0>=

  • max_degree (int) – maximum degree of interaction features to be considered. For example if max_degree=3, only interaction columns from up to 3 columns would be generated.

min_degree

minimum degree of interaction features to be considered

Type

int

max_degree

maximum degree of interaction features to be considered

Type

int

nb_features_to_interact

number of selected columns from which interactions should be computed. (=len(columns))

Type

int

nb_combinations

number of new interaction features

Type

int

interaction_colname

names of each new interaction feature. The name of an interaction feature is the combinations of previous column names joined with a whitespace. Interaction feature of [“col1”,”col2”,”col3] would be “col1 col2 col3”.

Type

list

nb_feature_out

number of total columns of transformed dataset, including new interaction features

Type

int

__init__(columns, min_degree=2, max_degree=2, **kwargs)None[source]

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__(columns[, min_degree, max_degree])

Initialize self.

check_is_fitted(attribute)

Check if particular attributes are on the object.

check_weights_column(X, weights_column)

Helper method for validating weights column in dataframe.

classname()

Method that returns the name of the current class when called.

columns_check(X)

Method to check that the columns attribute is set and all values are present in X.

columns_set_or_check(X)

Function to check or set columns attribute.

fit(X[, y])

Base transformer fit method, checks X and y types.

fit_transform(X[, y])

Fit to data, then transform it.

get_params([deep])

Get parameters for this estimator.

set_params(**params)

Set the parameters of this estimator.

transform(X)

Generate from input pandas DataFrame (X) new interaction features using the “product” pandas.DataFrame method

check_is_fitted(attribute)

Check if particular attributes are on the object. This is useful to do before running transform to avoid trying to transform data without first running the fit method.

Wrapper for utils.validation.check_is_fitted function.

Parameters

attributes (List) – List of str values giving names of attribute to check exist on self.

static check_weights_column(X, weights_column)

Helper method for validating weights column in dataframe.

X (pd.DataFrame): df containing weight column weights_column (str): name of weight column

classname()

Method that returns the name of the current class when called.

columns_check(X)

Method to check that the columns attribute is set and all values are present in X.

Parameters

X (pd.DataFrame) – Data to check columns are in.

columns_set_or_check(X)

Function to check or set columns attribute.

If the columns attribute is None then set it to all columns in X. Otherwise run the columns_check method.

Parameters

X (pd.DataFrame) – Data to check columns are in.

fit(X, y=None)

Base transformer fit method, checks X and y types. Currently only pandas DataFrames are allowed for X and DataFrames or Series for y.

Fit calls the columns_set_or_check method which will set the columns attribute to all columns in X, if it is None.

Parameters
  • X (pd.DataFrame) – Data to fit the transformer on.

  • y (None or pd.DataFrame or pd.Series, default = None) – Optional argument only required for the transformer to work with sklearn pipelines.

fit_transform(X, y=None, **fit_params)

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
  • X (array-like of shape (n_samples, n_features)) – Input samples.

  • y (array-like of shape (n_samples,) or (n_samples, n_outputs), default=None) – Target values (None for unsupervised transformations).

  • **fit_params (dict) – Additional fit parameters.

Returns

X_new – Transformed array.

Return type

ndarray array of shape (n_samples, n_features_new)

get_params(deep=True)

Get parameters for this estimator.

Parameters

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns

params – Parameter names mapped to their values.

Return type

dict

set_params(**params)

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

**params (dict) – Estimator parameters.

Returns

self – Estimator instance.

Return type

estimator instance

transform(X)[source]
Generate from input pandas DataFrame (X) new interaction features using the “product” pandas.DataFrame method

and add this column or columns in X.

Parameters

X (pd.DataFrame) – Data to transform.

Returns

X – Input X with additional column or columns (self.interaction_colname) added. These contain the output of running the product pandas DataFrame method on identified combinations.

Return type

pd.DataFrame