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 |
Scale numeric columns to a fixed range.
Usage
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.
columns: SelectorColumns 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.0Lower end of the target range. Defaults to 0.0.
max: float = 1.0Upper end of the target range. Defaults to 1.0, and must be greater than min.
clipping: bool = TrueWhether values outside the training bounds are clamped into [min, max] at transform time. Defaults to True.
id: str | None = None"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.
Each selected column is replaced in place by its rescaled values:
Specifying the step touches no data:
Recipe (untrained)
Inputs: no roles assigned
Steps:
step_range(select(role='feature', type=NumericType)) [untrained]
fit() learns each column’s smallest and largest value:
Recipe (trained)
Inputs: 1 columns (1 feature)
Steps:
step_range(x) [trained]
transform() applies them in place:
min and max set the target range:
| x | |
|---|---|
| 0 | -1.0 |
| 1 | -0.5 |
| 2 | 0.0 |
| 3 | 1.0 |
New data can fall outside the training bounds:
With the default clipping=True those values are clamped into [min, max]:
| x | |
|---|---|
| 0 | 0.0 |
| 1 | 1.0 |
With clipping=False they extrapolate past the target range instead: