Skip to main content

StepDropZeroVariance

Remove columns that have a single unique value (zero variance).

Usage

Source

StepDropZeroVariance(
    columns,
    *,
    id=None,
)

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.

Parameters

columns: Selector

Columns 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
Stable step identifier. Defaults to "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.

Examples

const has a single unique value, so it is dropped; x is kept:

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

Specifying the step touches no data:

recipe = Recipe().add(StepDropZeroVariance(s.numeric_features()))
recipe
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:

fitted = recipe.fit(df)
fitted
Recipe (trained)
  Inputs: 1 columns (1 feature)
  Steps:
    step_drop_zero_variance(x, const)  [trained]

transform() drops the constant one:

fitted.transform(df)
x
0 1.0
1 2.0
2 3.0