Skip to main content

StepRange

Scale numeric columns to a fixed range.

Usage

Source

StepRange(
    columns,
    *,
    min=0.0,
    max=1.0,
    clipping=True,
    id=None,
)

Each selected numeric column x is replaced in place by (x - low) / (high - low) * (max - min) + min, where low and high are the column’s smallest and largest values in the training data. On the training data itself the result therefore spans exactly min to max. To center and scale to zero mean and unit variance instead, see StepNormalize.

New values falling outside the training bounds would land outside the target range; by default (clipping=True) they are clamped back into the range.

Constant columns (zero variance) among the selection have a zero range. They are dropped from the rescaling (with a warning) and pass through transform untouched. You can get rid of these warnings by adding a StepDropZeroVariance before this step.

A column containing an infinite value yields NaN with a warning.

See Range Scaling 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 rescale. 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.

min: float = 0.0

Lower end of the target range. Defaults to 0.0.

max: float = 1.0

Upper end of the target range. Defaults to 1.0, and must be greater than min.

clipping: bool = True

Whether values outside the training bounds are clamped into [min, max] at transform time. Defaults to True.

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

Examples

Each selected column is replaced in place by its rescaled values:

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

Specifying the step touches no data:

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

fit() learns each column’s smallest and largest value:

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

transform() applies them in place:

fitted.transform(df)
x
0 0.00
1 0.25
2 0.50
3 1.00

min and max set the target range:

Recipe().add(StepRange(s.numeric_features(), min=-1.0, max=1.0)).fit(
    df
).transform(df)
x
0 -1.0
1 -0.5
2 0.0
3 1.0

New data can fall outside the training bounds:

new_df = pd.DataFrame({"x": [0.0, 9.0]})
new_df
x
0 0.0
1 9.0

With the default clipping=True those values are clamped into [min, max]:

Recipe().add(StepRange(s.numeric_features(), clipping=True)).fit(
    df
).transform(new_df)
x
0 0.0
1 1.0

With clipping=False they extrapolate past the target range instead:

Recipe().add(StepRange(s.numeric_features(), clipping=False)).fit(
    df
).transform(new_df)
x
0 -0.25
1 2.00