## StepOther


Collapse infrequent categorical levels into a single `"other"` level.


Usage

``` python
StepOther(
    columns,
    *,
    threshold=None,
    min_count=None,
    other_level=DEFAULT_OTHER_LEVEL,
    id=None
)
```


Each selected categorical column keeps the levels that are frequent enough in the training data and has every other level replaced in place by `other_level`. Levels that never appeared during fitting are pooled into `other_level` as well, so new data can never introduce a level the rest of the recipe has not seen. Nulls stay null; use [StepUnknown](StepUnknown.md#mlfeatures.StepUnknown) first when missingness should become a level of its own.

See [Collapsing Categories](https://feaz-book.com/categorical-collapse) 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 collapse. 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 categorical columns (string, categorical, and enum) are meaningful here.

`threshold: float | None = None`  
Smallest share of a column's non-null rows a level may hold and still be kept, as a proportion strictly between 0 and 1. Default `None`; mutually exclusive with `min_count`, and when neither is given a proportion of `0.05` is used.

`min_count: int | None = None`  
Smallest number of rows a level may appear in and still be kept, as a whole number of 1 or more. Default `None`; mutually exclusive with `threshold`.

`other_level: str = DEFAULT_OTHER_LEVEL`\  
The level that pooled levels are replaced with. Default `"other"`. A `UserWarning` is emitted if a selected column already contains this level, since the pooled rows then become indistinguishable from real ones.

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


## Examples

The rare levels are pooled in place into a single `"other"` level:


``` python
df = pd.DataFrame(
    {"city": ["NYC"] * 5 + ["LA"] * 4 + ["Reno", "Provo"]}
)
df
```


|     | city  |
|-----|-------|
| 0   | NYC   |
| 1   | NYC   |
| 2   | NYC   |
| 3   | NYC   |
| 4   | NYC   |
| 5   | LA    |
| 6   | LA    |
| 7   | LA    |
| 8   | LA    |
| 9   | Reno  |
| 10  | Provo |


Specifying the step touches no data:


``` python
recipe = Recipe().add(StepOther(s.nominal_features(), threshold=0.2))
recipe
```


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


[fit()](Step.fit.md#mlfeatures.Step.fit) records which levels are frequent enough to keep:


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


    Recipe (trained)
      Inputs: 1 columns (1 feature)
      Steps:
        step_other(city)  [trained]


[transform()](Step.transform.md#mlfeatures.Step.transform) pools everything else:


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


|     | city  |
|-----|-------|
| 0   | NYC   |
| 1   | NYC   |
| 2   | NYC   |
| 3   | NYC   |
| 4   | NYC   |
| 5   | LA    |
| 6   | LA    |
| 7   | LA    |
| 8   | LA    |
| 9   | other |
| 10  | other |


`min_count` states the cutoff as a number of rows instead of a share:


``` python
recipe = Recipe().add(StepOther(s.nominal_features(), min_count=5))
fitted = recipe.fit(df)
fitted.transform(df)
```


|     | city  |
|-----|-------|
| 0   | NYC   |
| 1   | NYC   |
| 2   | NYC   |
| 3   | NYC   |
| 4   | NYC   |
| 5   | other |
| 6   | other |
| 7   | other |
| 8   | other |
| 9   | other |
| 10  | other |


`other_level` names the pooled level:


``` python
recipe = Recipe().add(
    StepOther(s.nominal_features(), threshold=0.2, other_level="rare")
)
fitted = recipe.fit(df)
fitted.transform(df)
```


|     | city |
|-----|------|
| 0   | NYC  |
| 1   | NYC  |
| 2   | NYC  |
| 3   | NYC  |
| 4   | NYC  |
| 5   | LA   |
| 6   | LA   |
| 7   | LA   |
| 8   | LA   |
| 9   | rare |
| 10  | rare |
