Skip to main content

StepNormalize

Center and scale numeric columns to zero mean and unit variance.

Usage

Source

StepNormalize(
    columns,
    *,
    id=None,
)

Each selected numeric column x is replaced in place by (x - mean) / std, using the column’s mean and standard deviation. To scale onto a fixed interval instead, see StepRange; to reshape a skewed distribution, see StepOrderNorm.

Constant (zero-variance) columns among the selection cannot be scaled to unit variance; they are dropped from the normalization (with a warning) and pass through transform untouched. You can get rid of these warnings by adding a StepDropZeroVariance before this step.

See Normalization 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 normalize. Accepts a single column name, a list of names, a mlfeatures selector such as Selectors.numeric_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. Only numeric columns are meaningful here.

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

Examples

Each selected column is replaced in place by its centered, unit-variance values:

df = pd.DataFrame({"x": [1.0, 2.0, 3.0, 4.0]})
df
x
0 1.0
1 2.0
2 3.0
3 4.0

Specifying the step touches no data:

recipe = Recipe().add(StepNormalize(s.numeric_features()))
recipe
Recipe (untrained)
  Inputs: no roles assigned
  Steps:
    step_normalize(select(role='feature', type=NumericType))  [untrained]

fit() learns each column’s mean and standard deviation:

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

transform() applies them in place:

fitted.transform(df)
x
0 -1.161895
1 -0.387298
2 0.387298
3 1.161895