Most steps take the same handful of arguments: columns, keep_original_cols, role, and id. Each step’s reference page lists them briefly. This page goes into more detail on the ideas behind them: what a selector resolves to and when, what dropping a source column does to a recipe’s column lineage, what a role is for, and why a step has an identifier at all.
Every example below uses this data frame.
import pandas as pd
from mlfeatures import Recipe, StepDropCorr, StepDummy, StepNormalize, StepPca
from mlfeatures import Selectors as s
df = pd.DataFrame(
{
"price": [200.0, 250.0, 310.0, 275.0],
"sqft": [1.0, 2.0, 3.0, 2.5],
"beds": [2.0, 3.0, 4.0, 3.0],
"city": ["LA", "SF", "LA", "SD"],
}
)
df
|
price |
sqft |
beds |
city |
| 0 |
200.0 |
1.0 |
2.0 |
LA |
| 1 |
250.0 |
2.0 |
3.0 |
SF |
| 2 |
310.0 |
3.0 |
4.0 |
LA |
| 3 |
275.0 |
2.5 |
3.0 |
SD |
Positional and keyword-only arguments
Every step follows the same calling convention. Required arguments are positional; everything optional is keyword-only. So a threshold or a degree is always named at the call site:
StepDropCorr(s.numeric_features(), threshold=0.95)
step_drop_corr(select(role='feature', type=NumericType)) [untrained]
Passing an option positionally is a TypeError rather than a silent mis-assignment. Reading a step is never a matter of counting commas, and options can be reordered or added without changing what existing calls mean.
A few required arguments are keyword-only too, such as the reference point of StepGeoDistance (ref_lat, ref_lon), where two coordinates in a row would be easy to swap.
columns
columns says which columns a step applies to. It accepts any of:
- a single column name:
"sqft"
- a list of names:
["sqft", "beds"]
- a mlfeatures selector:
s.numeric_features()
- a narwhals or polars selector:
narwhals.selectors.numeric(), polars.selectors.numeric()
A selector is what you will end up using the most. Selectors say what the step is for rather than naming today’s columns, so they keep working when the data frame changes and when an earlier step in the recipe has already added or removed columns. Prefer the mlfeatures selectors over narwhals or polars ones: they know about roles and column lineage (s.numeric_features() leaves out the target, s.has_source("dummy") picks out one step’s output), while narwhals and polars selectors only see names and dtypes. See Selectors for the full set.
recipe = (
Recipe(data=df, target="price")
.add(StepNormalize(s.numeric_features()))
)
fitted = recipe.fit(df)
fitted.transform(df)
|
price |
sqft |
beds |
city |
| 0 |
200.0 |
-1.317465 |
-1.224745 |
LA |
| 1 |
250.0 |
-0.146385 |
0.000000 |
SF |
| 2 |
310.0 |
1.024695 |
1.224745 |
LA |
| 3 |
275.0 |
0.439155 |
0.000000 |
SD |
Selectors are resolved during fit(), one step at a time. As the recipe is fitted, each step’s selector is resolved against the data frame as the steps before it have left it, so a step sees the columns earlier steps added and does not see the ones they removed. A fitted step holds the concrete column names it resolved to, and transform() applies itself to exactly those columns; it never re-resolves the selector against new data. This is what a fitted recipe’s repr shows in place of the selector:
Recipe (trained)
Inputs: 4 columns (3 features, 1 target)
Steps:
step_normalize(sqft, beds) [trained]
Because resolution follows the recipe step by step, the same selector can mean different columns at different points. Here s.numeric_features() appears twice. The first resolves to the original numeric columns; the second also picks up the indicator columns that the dummy step created in between:
recipe = (
Recipe(data=df, target="price")
.add(StepNormalize(s.numeric_features()))
.add(StepDummy(s.nominal_features()))
.add(StepNormalize(s.numeric_features()))
)
fitted = recipe.fit(df)
fitted
Recipe (trained)
Inputs: 5 columns (4 features, 1 target)
Steps:
step_normalize(sqft, beds) [trained]
step_dummy(city → city_SD, city_SF) [trained]
step_normalize(sqft, beds, city_SD, city_SF) [trained]
A selection that matches nothing is not an error. The step is fitted on zero columns: it stays in the fitted recipe, shown with no columns, and changes nothing, which makes it the identity transform.
This is deliberate. Whether a selector matches depends on the data, and a recipe is written before it sees any, so a step like StepNormalize(s.numeric_features()) should be valid for every data frame, including one with no numeric features:
recipe = (
Recipe(data=df, target="price")
.add(StepNormalize(s.numeric_features()))
)
nominal_only = df[["price", "city"]]
fitted = recipe.fit(nominal_only)
fitted
Recipe (trained)
Inputs: 2 columns (1 feature, 1 target)
Steps:
step_normalize(<empty selection>) [trained]
fitted.transform(nominal_only)
|
price |
city |
| 0 |
200.0 |
LA |
| 1 |
250.0 |
SF |
| 2 |
310.0 |
LA |
| 3 |
275.0 |
SD |
The same holds inside a recipe, because selectors resolve step by step: an earlier step can remove every column a later selector would have matched, and the recipe should not become invalid because of what the steps before it did. The recipe handles the empty case once, for every step, so no step has to guard against it.
The cost is that a typo in a name-based selector, such as s.starts_with("prce"), silently does nothing. When a step seems to have had no effect, check the fitted recipe’s repr for an empty selection. Selectors that depend only on the recipe, Selectors.has_source and Selectors.has_ancestor, raise instead when given a step id that names no earlier step, since that is a typo whatever the data.
Steps that derive new columns take keep_original_cols. It controls whether the columns the step consumed stay in the data frame alongside the columns it produced. The default is False: the source columns are dropped.
Dropping is the default because the derived columns usually stand in for the sources: StepPca replaces its inputs with components that are linear combinations of all of them, and StepDummy replaces a nominal column with the indicator columns encoding it. Most of the time a model needs only one of the two forms, but not always, which is why the choice is yours.
recipe = (
Recipe(data=df, target="price")
.add(StepPca(s.numeric_features(), num_comp=2))
)
fitted = recipe.fit(df)
fitted.transform(df)
|
price |
city |
PC1 |
PC2 |
| 0 |
200.0 |
LA |
-1.504260 |
-0.053176 |
| 1 |
250.0 |
SF |
-0.090434 |
-0.086294 |
| 2 |
310.0 |
LA |
1.323392 |
-0.119413 |
| 3 |
275.0 |
SD |
0.271302 |
0.258883 |
Set it to True when you want the sources too, for instance to compare a derived column against what it came from, or because a later step still needs the original scale:
recipe = (
Recipe(data=df, target="price")
.add(StepPca(s.numeric_features(), num_comp=2, keep_original_cols=True))
)
fitted = recipe.fit(df)
fitted.transform(df)
|
price |
sqft |
beds |
city |
PC1 |
PC2 |
| 0 |
200.0 |
1.0 |
2.0 |
LA |
-1.504260 |
-0.053176 |
| 1 |
250.0 |
2.0 |
3.0 |
SF |
-0.090434 |
-0.086294 |
| 2 |
310.0 |
3.0 |
4.0 |
LA |
1.323392 |
-0.119413 |
| 3 |
275.0 |
2.5 |
3.0 |
SD |
0.271302 |
0.258883 |
What dropping does to the lineage
A fitted recipe maintains a column-lineage DAG (see Lineage), and dropping a source column does not erase it from that DAG. The node is archived rather than deleted, with a record of which step removed it, so a derived column’s provenance still resolves through columns that no longer exist:
recipe = (
Recipe(data=df, target="price")
.add(StepPca(s.numeric_features(), num_comp=2))
)
fitted = recipe.fit(df)
fitted.lineage.summary()
shape: (4, 5)| name | dtype | roles | source | sparse |
|---|
| str | str | str | str | bool |
| "price" | "float" | "target" | "original" | false |
| "city" | "str" | "feature" | "original" | false |
| "PC1" | "float" | "feature" | "pca" | false |
| "PC2" | "float" | "feature" | "pca" | false |
summary() lists only the live columns, and sqft and beds are gone from it. Ancestry still finds them:
fitted.lineage.ancestors("PC1")
Pass include_removed=True to list the archived columns too, along with which step dropped each:
fitted.lineage.summary(include_removed=True)
shape: (6, 7)| name | dtype | roles | source | sparse | parents | removed_by |
|---|
| str | str | str | str | bool | str | str |
| "price" | "float" | "target" | "original" | false | "" | null |
| "city" | "str" | "feature" | "original" | false | "" | null |
| "PC1" | "float" | "feature" | "pca" | false | "sqft, beds" | null |
| "PC2" | "float" | "feature" | "pca" | false | "sqft, beds" | null |
| "sqft" | "float" | "feature" | "original" | false | "" | "pca" |
| "beds" | "float" | "feature" | "original" | false | "" | "pca" |
Selectors, though, no longer see the dropped columns. A dropped source column is not a feature any more, because it is no longer a column, so a later s.numeric_features() in the same recipe will not pick it up (the archived record keeps the roles the column had, for provenance only). When keep_original_cols=True, the sources survive with their roles intact and later selectors do see them.
role
A role is what a column is for. The roles mlfeatures understands natively are "target" for the column being predicted, "feature" for a column used as model input, and "case_weights" for per-sample weights. Any other string you choose is a custom role. Roles are how selectors like s.numeric_features() know what to select, and how a fitted recipe knows which columns to require at transform time. See Roles.
Steps that produce new columns take role, which assigns a role to those columns. The default is "feature", because a step’s whole purpose is normally to produce model input, and a newly created column with no role would be invisible to every role-based selector downstream:
recipe = (
Recipe(data=df, target="price")
.add(StepPca(s.numeric_features(), num_comp=2))
)
fitted = recipe.fit(df)
fitted.lineage.summary()
shape: (4, 5)| name | dtype | roles | source | sparse |
|---|
| str | str | str | str | bool |
| "price" | "float" | "target" | "original" | false |
| "city" | "str" | "feature" | "original" | false |
| "PC1" | "float" | "feature" | "pca" | false |
| "PC2" | "float" | "feature" | "pca" | false |
Pass something else when the new columns are not model input. This replaces the default rather than adding to it, so the components below are not features and a later s.numeric_features() will skip them:
recipe = (
Recipe(data=df, target="price")
.add(StepPca(s.numeric_features(), num_comp=2, role="group"))
)
fitted = recipe.fit(df)
fitted.lineage.summary()
shape: (4, 5)| name | dtype | roles | source | sparse |
|---|
| str | str | str | str | bool |
| "price" | "float" | "target" | "original" | false |
| "city" | "str" | "feature" | "original" | false |
| "PC1" | "float" | "group" | "pca" | false |
| "PC2" | "float" | "group" | "pca" | false |
Two related recipe-level APIs are worth knowing about. Recipe.set_roles assigns roles to columns that already exist in the input data, which is the other half of the same picture: role is for columns a step creates, set_roles for columns that arrived with the data. To change a role part way through the pipeline, Recipe.add_role, Recipe.update_role and Recipe.remove_role take effect from where they are written. Recipe.update_role_requirements controls whether a role’s columns must be present when FittedRecipe.transform is called on new data, which is where a custom role usually needs a decision: an identifier column you carry through the pipeline is required at transform time, an auxiliary column used only during fitting is not.
id
Every step takes id, a short string naming that step within the recipe. It defaults to the step type’s short name: "normalize" for StepNormalize, "pca" for StepPca, "dummy" for StepDummy.
The id exists because a step needs a name that does not change when you edit the recipe around it. Positions shift the moment you insert a step; the id does not, so everything that refers to a step refers to it by id:
- Column provenance. Each column the DAG knows about records the id of the step that produced it, which is the
source column in Lineage.summary() above (pca, not “step 1”). Lineage.columns_from_source looks columns up by that id.
- Selectors. Selectors.has_source and Selectors.has_ancestor select columns by which step produced them, letting a later step operate on exactly one earlier step’s output.
- Errors. When a step fails, the raised error names the id (a StepError for a failure inside the step, a
TypeError naming the step for a column of an unsupported dtype), so a failure deep in a long pipeline says which step broke.
Here a normalize step is pointed at the dummy step’s output, by id, without naming any of the columns that step will generate:
recipe = (
Recipe(data=df, target="price")
.add(StepDummy(s.nominal_features()))
.add(StepNormalize(s.has_source("dummy")))
)
fitted = recipe.fit(df)
fitted.transform(df)
|
price |
sqft |
beds |
city_SD |
city_SF |
| 0 |
200.0 |
1.0 |
2.0 |
-0.5 |
-0.5 |
| 1 |
250.0 |
2.0 |
3.0 |
-0.5 |
1.5 |
| 2 |
310.0 |
3.0 |
4.0 |
-0.5 |
-0.5 |
| 3 |
275.0 |
2.5 |
3.0 |
1.5 |
-0.5 |
That works because an id names exactly one step. The default is per type, not per step, so two steps of the same type would collide; the recipe resolves that as you add them, by suffixing the second one:
recipe = (
Recipe(data=df, target="price")
.add(StepNormalize(s.numeric_features()))
.add(StepNormalize(s.numeric_features()))
)
fitted = recipe.fit(df)
[step.id for step in fitted.steps]
['normalize', 'normalize_2']
The suffix keeps the two addressable, but it does not make them readable: which one is normalize_2 depends on the order you added them, and it shifts if you insert another normalize step earlier. So give steps explicit ids as soon as a recipe has two of a type you want to refer to individually. An id you pass yourself is never renamed, and passing one that a step already in the recipe uses raises instead.
recipe = (
Recipe(data=df, target="price")
.add(StepDummy(s.nominal_features(), id="dummy_city"))
.add(StepNormalize(s.has_source("dummy_city"), id="scale_city"))
)
fitted = recipe.fit(df)
[step.id for step in fitted.steps]
['dummy_city', 'scale_city']