## RecipeTransformer


Wrap a [Recipe](Recipe.md#mlfeatures.Recipe) as a scikit-learn transformer.


Usage

``` python
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`:

``` python
from mlfeatures import RecipeTransformer

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

The wrapped recipe is untrained: [fit()](Step.fit.md#mlfeatures.Step.fit) trains it, and [transform()](Step.transform.md#mlfeatures.Step.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](Recipe.md#mlfeatures.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()](Step.transform.md#mlfeatures.Step.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()](Step.transform.md#mlfeatures.Step.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](StepPls.md#mlfeatures.StepPls) or [StepClassDistance](StepClassDistance.md#mlfeatures.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()](Step.fit.md#mlfeatures.Step.fit).

`feature_names_in_: list[str]`  
Column names of the `X` seen by [fit()](Step.fit.md#mlfeatures.Step.fit).

`n_features_in_: int`  
Number of columns of the `X` seen by [fit()](Step.fit.md#mlfeatures.Step.fit).

`feature_names_out_: list[str]`  
Column names of the output of [transform()](Step.transform.md#mlfeatures.Step.transform).


## Raises


`NotFittedError`  
If [transform()](Step.transform.md#mlfeatures.Step.transform) or `get_feature_names_out()` is called before [fit()](Step.fit.md#mlfeatures.Step.fit).


## Examples

Wrap a recipe and drive it with sklearn's [fit](Step.fit.md#mlfeatures.Step.fit) / [transform](Step.transform.md#mlfeatures.Step.transform) API:


``` python
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`:


``` python
fitted.get_feature_names_out()
```


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