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 |
Collapse infrequent categorical levels into a single "other" level.
Usage
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 first when missingness should become a level of its own.
See Collapsing Categories in Feature Engineering A to Z for background on this technique.
See Arguments shared across steps for the arguments this step shares with the others.
columns: SelectorColumns to collapse. Accepts a single column name, a list of names, a mlfeatures selector such as Selectors.nominal_features() (see 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 = NoneSmallest 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 = NoneSmallest 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"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.
The rare levels are pooled in place into a single "other" level:
| 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:
Recipe (untrained)
Inputs: no roles assigned
Steps:
step_other(select(role='feature', type=(String, Categorical, Enum))) [untrained]
fit() records which levels are frequent enough to keep:
Recipe (trained)
Inputs: 1 columns (1 feature)
Steps:
step_other(city) [trained]
transform() pools everything else:
min_count states the cutoff as a number of rows instead of a share:
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: