Skip to main content

Composing steps

Steps run in order, and each one selects from the data frame as the previous step left it. That is what lets a later step pick up columns an earlier step created, and mlfeatures tracks which step produced each column so a selector can ask for exactly those.

  • Steps operate on the output of the previous step.
  • s.has_source(step_type=StepDummy) selects the columns a step produced; s.has_ancestor() follows that lineage through later steps.
  • s.has_source("some_id") selects by step id; a second step of the same type is numbered automatically (dummy_2), or name it with id=.

Setup

from mlfeatures import Recipe, StepDummy, StepImputeMode, StepNormalize
from mlfeatures import Selectors as s
from mlfeatures.data import credit

Each step sees the previous step’s output

A selector is resolved against the data frame as it exists when its step runs, so a step placed after StepDummy can select the indicator columns it created. Here the normalize step’s s.numeric_features() matches the original numerics and every new indicator column:

recipe = (
    Recipe(data=credit, target="Status")
    .add(StepImputeMode(s.nominal_features()))
    .add(StepDummy(s.nominal_features()))
    .add(StepNormalize(s.numeric_features()))
)
fitted = recipe.fit(credit)
fitted
Recipe (trained)
  Inputs: 23 columns (22 features, 1 target)
  Steps:
    step_impute_mode(Home, Marital, Records, Job)                                                                                                                                                                                                                            [trained]
    step_dummy(Home, Marital, Records, Job → Home_other, Home_owner, Home_parents, Home_priv, Home_rent, Marital_married, Marital_separated, Marital_single, Marital_widow, Records_yes, Job_freelance, Job_others, Job_partime)                                             [trained]
    step_normalize(Seniority, Time, Age, Expenses, Income, Assets, Debt, Amount, Price, Home_other, Home_owner, Home_parents, Home_priv, Home_rent, Marital_married, Marital_separated, Marital_single, Marital_widow, Records_yes, Job_freelance, Job_others, Job_partime)  [trained]

Selecting by source step

Use s.has_source(step_type=SomeStep) to select only the columns a step produced. This normalizes the indicator columns and leaves the original numerics as they were:

recipe = (
    Recipe(data=credit, target="Status")
    .add(StepImputeMode(s.nominal_features()))
    .add(StepDummy(s.nominal_features()))
    .add(StepNormalize(s.has_source(step_type=StepDummy)))
)
fitted = recipe.fit(credit)
fitted
Recipe (trained)
  Inputs: 23 columns (22 features, 1 target)
  Steps:
    step_impute_mode(Home, Marital, Records, Job)                                                                                                                                                                                 [trained]
    step_dummy(Home, Marital, Records, Job → Home_other, Home_owner, Home_parents, Home_priv, Home_rent, Marital_married, Marital_separated, Marital_single, Marital_widow, Records_yes, Job_freelance, Job_others, Job_partime)  [trained]
    step_normalize(Home_other, Home_owner, Home_parents, Home_priv, Home_rent, Marital_married, Marital_separated, Marital_single, Marital_widow, Records_yes, Job_freelance, Job_others, Job_partime)                            [trained]

s.has_source() matches the step that produced a column. A step that rewrites its columns in place, such as StepNormalize, does not take over as their producer, so the indicators above still count as StepDummy’s output after they are normalized. s.has_ancestor() goes further: it also matches new columns that a later step derived from the step’s output, such as the product column StepInteract builds from two indicators.

Selecting by step id

s.has_source("some_id") selects the output of one step by its id. Every step has one: it defaults to the step’s short name ("dummy"), a second step of the same type is numbered ("dummy_2"), and id= lets you name a step yourself.

recipe = (
    Recipe(data=credit, target="Status")
    .add(StepImputeMode(s.nominal_features()))
    .add(StepDummy(s.col("Home")))  # id "dummy"
    .add(StepDummy(s.col("Marital")))  # id "dummy_2"
    .add(StepNormalize(s.has_source("dummy_2")))
)
fitted = recipe.fit(credit)
fitted
Recipe (trained)
  Inputs: 21 columns (20 features, 1 target)
  Steps:
    step_impute_mode(Home, Marital, Records, Job)                                            [trained]
    step_dummy(Home → Home_other, Home_owner, Home_parents, Home_priv, Home_rent)            [trained]
    step_dummy(Marital → Marital_married, Marital_separated, Marital_single, Marital_widow)  [trained]
    step_normalize(Marital_married, Marital_separated, Marital_single, Marital_widow)        [trained]