Skip to main content

Recipe

A declarative, untrained pipeline of feature-engineering steps.

Usage

Source

Recipe(
    steps=None,
    *,
    data=None,
    target=None,
    features=None,
    case_weights=None,
)

A recipe is specified in two parts: the columns’ roles are declared first (the target, features and case_weights arguments, plus set_roles()), then steps are added with add(). Specifying a recipe never touches data; nothing is estimated until fit(), which returns a FittedRecipe, and FittedRecipe.transform() applies it to new data.

Each add() returns a new recipe, so steps chain into a pipeline:

recipe = (
    Recipe(data=credit, target="Status")
    .add(StepDropCorr(s.numeric_features(), threshold=0.9))
    .add(StepNormalize(s.numeric_features()))
)

The untrained recipe shows the roles it carries and its steps with their selectors unresolved:

recipe
Recipe (untrained)
  Inputs: 14 columns (1 target)
  Steps:
    step_drop_corr(select(role='feature', type=NumericType))  [untrained]
    step_normalize(select(role='feature', type=NumericType))  [untrained]

set_roles() declares roles on the input columns, so it can be applied anywhere among the steps, including after them, with the same result (once a role operation is written before the first step, set_roles() keeps the order it was written in with it, see set_roles()):

late = (
    Recipe(data=credit)
    .add(StepDropCorr(s.numeric_features(), threshold=0.9))
    .add(StepNormalize(s.numeric_features()))
    .set_roles(target="Status")
)
late
Recipe (untrained)
  Inputs: 14 columns (1 target)
  Steps:
    step_drop_corr(select(role='feature', type=NumericType))  [untrained]
    step_normalize(select(role='feature', type=NumericType))  [untrained]

To change a role part way through the pipeline instead, use add_role(), update_role() or remove_role(), which take effect from where they are written.

Parameters

steps: list[Step] | None = None

Steps to start from, equivalent to chaining add() once per step. Default None (no steps).

data: DataFrame | None = None

A data frame whose column names are used to check role assignments as soon as they are made, and which backs summary() before fitting. Nothing is estimated from it and it is never modified, so a small sample with the right columns is enough. Default None.

target: str | list[str] | None = None

Column name or list of names to give the "target" role. Default None.

features: str | list[str] | None = None

Column name or list of names to give the "feature" role. With the default, None, every column that is not a target or a case weight becomes a feature when the recipe is fitted, including columns given a custom role with set_roles(). Pass names to choose the features yourself and keep such columns out.

case_weights: str | list[str] | None = None
Column name or list of names to give the "case_weights" role. Default None.