A Recipe is a pre-processing pipeline with three phases. You specify it as a sequence of steps, fit it to training data, and transform any data frame with what it learned.
NoteKey points
Steps operate on data frames, using mlfeatures selectors (or polars selectors) to choose the columns to operate on.
mlfeatures extends the selection process with roles.
Each step applies to the “output” data of the previous step.
mlfeatures fits and transforms the training data, but only transforms test data.
mlfeatures handles pre-processing, and plugs in to scikit pipelines for final model fitting.
Setup
The credit data has a categorical target, Status, and a mix of numeric and string features with a few missing values in each. Hold out a test set before anything is estimated.
Start a recipe with Recipe(data=training_data, target="SomeColumn") and chain steps on with .add(), each choosing its columns with a selector. Nothing is computed yet: the recipe is a specification, and every step shows its selector as written.
target= gives Status the target role, and every other column is a feature. That is what keeps Status out of s.nominal_features(), so it is not imputed or turned into indicator columns with the real string features, and it is what lets a scikit-learn pipeline hand the recipe its y later. data= is optional: the recipe only reads its column names, to check role assignments such as target= straight away and to show the columns before fitting. Nothing is estimated from it, and a step’s columns are not checked until the recipe is fitted.
Fitting training data
recipe.fit(training_data) runs the steps in order, and each step estimates what it needs from the data as it goes: the medians, the modes, the levels of each string column, the means and standard deviations. Fitting also transforms the training data frame step by step, so each selector is resolved against the output of the step before it.
Every step is now [trained] and lists the concrete columns it resolved to. The dummy step produced indicator columns from Home, Marital, Records, and Job, and the normalize step’s numeric_features() selector, resolved after it, picked those up along with the original numerics.
Transforming test data
fitted.transform(data) applies the stored estimates to any data frame that has the training features, with the training dtypes, and re-estimates nothing: the test set is filled with the training medians and modes and scaled by the training means.
result = fitted.transform(credit_test)result.head()
shape: (5, 23)
Status
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
str
f64
f64
f64
f64
f64
f64
f64
f64
f64
f64
f64
f64
f64
f64
f64
f64
f64
f64
f64
f64
f64
f64
"good"
0.256525
0.932128
-0.814945
1.162917
2.353534
-0.481696
2.10856
2.560203
1.250825
-0.278202
-0.946206
-0.467881
-0.23793
1.893895
0.623887
-0.172001
-0.542203
-0.122003
-0.462644
-0.553287
-0.193851
-0.346977
"bad"
-0.355733
-0.302985
-0.449476
0.237705
-0.574492
0.050584
0.362355
1.396303
1.408383
-0.278202
1.056536
-0.467881
-0.23793
-0.527854
0.623887
-0.172001
-0.542203
-0.122003
-0.462644
-0.553287
-0.193851
-0.346977
"good"
-0.355733
0.932128
-1.180413
-1.047312
-0.625637
-0.483297
-0.272628
-0.021538
-0.630322
-0.278202
-0.946206
2.136656
-0.23793
-0.527854
-1.602375
-0.172001
1.843776
-0.122003
-0.462644
-0.553287
-0.193851
-0.346977
"good"
2.705559
0.520424
1.560598
-1.047312
1.062134
1.296306
-0.272628
0.126595
0.221128
-0.278202
1.056536
-0.467881
-0.23793
-0.527854
-1.602375
-0.172001
-0.542203
8.194093
-0.462644
1.806839
-0.193851
-0.346977
"good"
-0.84554
-1.538097
-1.454514
-1.047312
-0.651209
-0.483297
-0.272628
-0.296642
-0.249954
-0.278202
-0.946206
2.136656
-0.23793
-0.527854
-1.602375
-0.172001
1.843776
-0.122003
-0.462644
-0.553287
-0.193851
2.881169
Wrap the recipe in RecipeTransformer to make it the first step of a scikit-learn Pipeline, so the model gets the same guarantee. scikit-learn keeps X and y apart while mlfeatures finds the target by role, so the adapter re-joins y under the target’s name at fit time and hands the model only the features.
pipe.fit fits the recipe and then the model on the training split; pipe.score only transforms the test split with the stored estimates before scoring it. Cross-validation, tuning, and feature names are covered in With scikit-learn pipelines.