## StepOneHot


Expand discrete columns into *k* binary indicator columns.


Usage

``` python
StepOneHot(
    columns,
    *,
    name_fn=None,
    keep_original_cols=False,
    role="feature",
    id=None
)
```


Each selected column is replaced by *k* indicator columns where *k* is the number of unique non-null levels seen during [fit()](Step.fit.md#mlfeatures.Step.fit). Indicators are `nw.Int8` (0/1); a column that contains null-source or unseen-level rows is `nw.Float64` instead (0.0/1.0/NaN), since pandas' `Int8` cannot hold the missing values. Unlike [StepDummy](StepDummy.md#mlfeatures.StepDummy), no reference level is dropped.

See [Dummy Encoding](https://feaz-book.com/categorical-dummy) in *Feature Engineering A to Z* for background on this technique.

See [Arguments shared across steps](../user-guide/common-step-arguments.md) for the arguments this step shares with the others.


## Parameters


`columns: Selector`  
Columns to expand. Accepts a single column name, a list of names, a mlfeatures selector such as [Selectors.nominal_features()](Selectors.md#mlfeatures.Selectors.nominal_features) (see [Selectors](Selectors.md#mlfeatures.Selectors) for the full set), or a narwhals or polars selector (`narwhals.selectors.string()`, `polars.selectors.string()`). Selectors are resolved against the data frame's schema when the recipe is fitted, so the same selector works on any backend. Only discrete columns (string, categorical, enum, boolean, and integer) are meaningful here.

`name_fn: Callable[[str, str], str] | None = None`  
Callable mapping `(source_column, level_as_str)` to the indicator column's name. Default `None`, which names them `"{column}_{level}"`.

`keep_original_cols: bool = ``False`  
If `True`, the source columns are kept alongside the indicators instead of being dropped. Default `False`.

`role: str | None = ``"feature"`  
Role assigned to the indicator columns (see [Roles](Roles.md#mlfeatures.Roles)). Default `"feature"`.

`id: str | None = None`  
Stable step identifier. Defaults to `"one_hot"`; a second step of this type in the same recipe becomes `"one_hot_2"`, and passing an `id` already used in the recipe is an error.


## Examples

Each categorical column is replaced by one indicator per level, with no reference level dropped (contrast [StepDummy](StepDummy.md#mlfeatures.StepDummy)):


``` python
df = pd.DataFrame({"color": ["red", "green", "blue", "red"]})
df
```


|     | color |
|-----|-------|
| 0   | red   |
| 1   | green |
| 2   | blue  |
| 3   | red   |


Specifying the step touches no data:


``` python
recipe = Recipe().add(StepOneHot(s.nominal_features()))
recipe
```


    Recipe (untrained)
      Inputs: no roles assigned
      Steps:
        step_one_hot(select(role='feature', type=(String, Categorical, Enum)))  [untrained]


[fit()](Step.fit.md#mlfeatures.Step.fit) records the levels of each selected column:


``` python
fitted = recipe.fit(df)
fitted
```


    Recipe (trained)
      Inputs: 3 columns (3 features)
      Steps:
        step_one_hot(color → color_blue, color_green, color_red)  [trained]


[transform()](Step.transform.md#mlfeatures.Step.transform) replaces the column with one indicator per level:


``` python
fitted.transform(df)
```


|     | color_blue | color_green | color_red |
|-----|------------|-------------|-----------|
| 0   | 0          | 0           | 1         |
| 1   | 0          | 1           | 0         |
| 2   | 1          | 0           | 0         |
| 3   | 0          | 0           | 1         |


[keep_original_cols](Step.keep_original_cols.md#mlfeatures.Step.keep_original_cols) keeps the source column alongside them:


``` python
recipe = Recipe().add(StepOneHot(s.nominal_features(), keep_original_cols=True))
fitted = recipe.fit(df)
fitted.transform(df)
```


|     | color | color_blue | color_green | color_red |
|-----|-------|------------|-------------|-----------|
| 0   | red   | 0          | 0           | 1         |
| 1   | green | 0          | 1           | 0         |
| 2   | blue  | 1          | 0           | 0         |
| 3   | red   | 0          | 0           | 1         |
