Skip to main content

RecipeTransformer

Wrap a Recipe as a scikit-learn transformer.

Usage

Source

RecipeTransformer(recipe)

Use it to put a recipe in front of any scikit-learn model, so the feature engineering is trained, cross-validated, and tuned together with the model instead of being applied by hand beforehand.

Usable as a single step in a sklearn.pipeline.Pipeline:

from mlfeatures import RecipeTransformer

pipe = Pipeline([
    ("recipe", RecipeTransformer(recipe)),
    ("model", LogisticRegression()),
])
pipe.fit(X_train, y_train)

The wrapped recipe is untrained: fit() trains it, and transform() applies the trained result. The recipe is stored verbatim so that sklearn.base.clone() (and therefore cross-validation / grid search) round-trips correctly; Recipe is immutable, so this is safe.

Step hyperparameters are tunable through sklearn’s nested parameter names, keyed on step id: recipe__<step id>__<parameter>, for example GridSearchCV(pipe, {"recipe__recipe__corr__threshold": [0.8, 0.9]}) when the transformer is named "recipe" in the pipeline. A step’s id and columns are not tunable.

It implements get_feature_names_out() and therefore composes inside a sklearn.compose.ColumnTransformer and supports set_output(transform="pandas" | "polars").

Target handling. sklearn keeps X and y apart, while a recipe identifies the target by its "target" role. At fit time y is joined onto X under the recipe’s target column name, and transform() returns the feature columns only, with the target column removed. Passing y to a recipe with no "target" role raises a ValueError, as does a recipe with more than 1 target column. A step that applies to the target column as one of its input columns (for example StepNormalize(["target"])) also raises a ValueError at fit time, because sklearn’s transform() never receives y; transform the target outside the pipeline instead (for example with sklearn.compose.TransformedTargetRegressor). A step that uses the target only at fit time, such as StepPls or StepClassDistance, is fine. With no y (an unsupervised recipe), X is used as given.

Parameters

recipe: Recipe
The untrained recipe to wrap. Required.

Attributes

recipe: Recipe

The untrained recipe passed to the constructor.

fitted_: FittedRecipe

The trained recipe; set by fit().

feature_names_in_: list[str]

Column names of the X seen by fit().

n_features_in_: int

Number of columns of the X seen by fit().

feature_names_out_: list[str]
Column names of the output of transform().

Raises

NotFittedError
If transform() or get_feature_names_out() is called before fit().

Examples

Wrap a recipe and drive it with sklearn’s fit / transform API:

X =pd.DataFrame(
    {"age": [25, 41, 33, 52], "city": ["NYC", "LA", "NYC", "SF"]}
)
rec = (
    Recipe()
    .add(StepNormalize(s.numeric_features()))
    .add(StepDummy(s.nominal_features()))
)
fitted = RecipeTransformer(rec).fit(X)
fitted.transform(X)
age city_NYC city_SF
0 -1.105912 1 0
1 0.281899 0 0
2 -0.412006 1 0
3 1.236019 0 1

get_feature_names_out() reports the produced feature names, so the transformer composes inside a sklearn.compose.ColumnTransformer:

fitted.get_feature_names_out()
array(['age', 'city_NYC', 'city_SF'], dtype=object)