feature_encoders.compose package

Module contents

class feature_encoders.compose._compose.FeatureComposer(model_structure: feature_encoders.compose._compose.ModelStructure)[source]

Generate linear features and pairwise interactions.

Parameters

model_structure (ModelStructure) – The structure of a linear regression model.

property component_matrix

Dataframe indicating which columns of the feature matrix correspond to which components.

Returns

feature_cols – in that component.

Return type

A binary indicator dataframe. Entry is 1 if that column is used

fit(X, y=None)[source]
transform(X)[source]
class feature_encoders.compose._compose.ModelStructure(structure: Optional[Dict] = None, feature_map: Optional[Dict] = None)[source]

Capture the structure of a linear regression model.

The class validates and stores the details of a linear regression model: features, main effects and interactions.

Parameters
  • structure (Dict, optional) –

    A dictionary that includes information about the model. Example:

    {'add_features':
        {'time':
            { 'ds': None,
            'remainder': 'passthrough',
            'replace': False,
            'subset': ['month', 'hourofweek']
            }
        },
    'main_effects':
        {'month':
            { 'feature': 'month',
            'max_n_categories': None,
            'encode_as': 'onehot',
            'interaction_only': False
            },
        'tow':
            { 'feature': 'hourofweek',
            'max_n_categories': 60,
            'encode_as': 'onehot',
            'interaction_only': False
            },
        'lin_temperature':
            { 'feature': 'temperature',
            'include_bias': False,
            'interaction_only': False
            }
        },
    }
    

    Defaults to None.

  • feature_map (Dict, optional) –

    A mapping between a feature generator name and the classes for its validation and creation. Example:

    {'datetime':
        'validate': 'validate.DatetimeSchema'
        'generate': 'generate.DatetimeFeatures'
    }
    

    Defaults to None.

add_interaction(*, lenc_name: str, renc_name: str, lenc_type: Union[str, object], renc_type: Union[str, object], **kwargs)[source]

Add a pairwise interaction.

Parameters
  • lenc_name (str) – A name for the first part of the interaction pair.

  • renc_name (str) – A name for the second part of the interaction pair.

  • lenc_type (str or encoder object) – The type of the feature encoder to apply on the first part of the interaction pair.

  • renc_type (str or encoder object) – The type of the feature encoder to apply on the second part of the interaction pair.

  • **kwargs – Keyword arguments to be passed during the feature encoders’ initialization.

Raises

ValueError – If an interaction with the same name (lenc_name, renc_name) has already been added.

Returns

The updated ModelStructure instance.

Return type

ModelStructure

Example:

model = ModelStructure().add_interaction(
    lenc_name="is_Monday",
    renc_name="daily_seasonality",
    lenc_type="categorical",
    renc_type="linear",
    **{
        is_Monday: {"feature": "is_Monday", "encode_as": "onehot"},
        daily_seasonality: {"feature": "daily", "as_filter": True},
    },
)
add_main_effect(*, name: str, enc_type: Union[str, sklearn.base.BaseEstimator], **kwargs)[source]

Add a main effect.

Parameters
  • name (str) – A name for the main effect.

  • enc_type (str or encoder object) – The type of the feature encoder to apply on the main effect.

  • **kwargs – Keyword arguments to be passed during the feature encoder initialization. Ignored if enc_type is not a string.

Raises

ValueError – If an encoder with the same name has already been added.

Returns

The updated ModelStructure instance.

Return type

ModelStructure

add_new_feature(*, name: str, fgen_type: Union[str, sklearn.base.BaseEstimator], **kwargs)[source]

Add a feature generator.

Feature generators are applied on the input dataframe with the same order that they were added.

Parameters
  • name (str) – A name for the feature generator.

  • fgen_type (str or sklearn-compatible transformer) – The feature generator to add. If it is a string, the corresponding class will be loaded based on the relevant entry in the feature_map dictionary.

  • **kwargs – Keyword arguments to be passed during the feature generator initialization. Ignored if fgen is not a string.

Raises

ValueError – If a feature generator with the same name has already been added.

Returns

The updated ModelStructure instance.

Return type

ModelStructure

property components
classmethod from_config(config: Dict, feature_map: Optional[Dict] = None)[source]

Create a ModelStructure instance from a configuration file.

Parameters
  • config (Dict) – A dictionary that includes information about the model.

  • feature_map (Dict, optional) – A mapping between a feature generator name and the classes for its validation and creation. Defaults to None.

Returns

A populated ModelStructure instance.

Return type

ModelStructure