df = pd.DataFrame({"x": [1.0, 2.0, 3.0], "const": [5.0, 5.0, 5.0]})
df| x | const | |
|---|---|---|
| 0 | 1.0 | 5.0 |
| 1 | 2.0 | 5.0 |
| 2 | 3.0 | 5.0 |
Remove columns that have a single unique value (zero variance).
Usage
The selected columns whose observed values are all the same (or all missing) are dropped, and no new columns are produced. For a looser rule that also drops columns dominated by one value, see StepDropNearZeroVariance.
See Zero Variance Filter 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 consider for removal. Accepts a single column name, a list of names, a mlfeatures selector such as Selectors.features() (see Selectors for the full set), or a narwhals or polars selector (narwhals.selectors.numeric(), polars.selectors.numeric()). Selectors are resolved against the data frame’s schema when the recipe is fitted, so the same selector works on any backend. Any dtype is accepted.
id: str | None = None"zv"; a second step of this type in the same recipe becomes "zv_2", and passing an id already used in the recipe is an error.
const has a single unique value, so it is dropped; x is kept:
| x | const | |
|---|---|---|
| 0 | 1.0 | 5.0 |
| 1 | 2.0 | 5.0 |
| 2 | 3.0 | 5.0 |
Specifying the step touches no data:
Recipe (untrained)
Inputs: no roles assigned
Steps:
step_drop_zero_variance(select(role='feature', type=NumericType)) [untrained]
fit() counts the unique values in each column:
Recipe (trained)
Inputs: 1 columns (1 feature)
Steps:
step_drop_zero_variance(x, const) [trained]
transform() drops the constant one: