## StepRange


Scale numeric columns to a fixed range.


Usage

``` python
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](StepNormalize.md#mlfeatures.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](Step.transform.md#mlfeatures.Step.transform) untouched. You can get rid of these warnings by adding a [StepDropZeroVariance](StepDropZeroVariance.md#mlfeatures.StepDropZeroVariance) before this step.

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

See [Range Scaling](https://feaz-book.com/numeric-range) in *Feature Engineering A to Z* for background on this technique.

See [Arguments shared across steps](../user-guide/common-step-arguments.md) 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()](Selectors.md#mlfeatures.Selectors.numeric_features) (see [Selectors](Selectors.md#mlfeatures.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:


``` python
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:


``` python
recipe = Recipe().add(StepRange(s.numeric_features()))
recipe
```


    Recipe (untrained)
      Inputs: no roles assigned
      Steps:
        step_range(select(role='feature', type=NumericType))  [untrained]


[fit()](Step.fit.md#mlfeatures.Step.fit) learns each column's smallest and largest value:


``` python
fitted = recipe.fit(df)
fitted
```


    Recipe (trained)
      Inputs: 1 columns (1 feature)
      Steps:
        step_range(x)  [trained]


[transform()](Step.transform.md#mlfeatures.Step.transform) applies them in place:


``` python
fitted.transform(df)
```


|     | x    |
|-----|------|
| 0   | 0.00 |
| 1   | 0.25 |
| 2   | 0.50 |
| 3   | 1.00 |


`min` and `max` set the target range:


``` python
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:


``` python
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]`:


``` python
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:


``` python
Recipe().add(StepRange(s.numeric_features(), clipping=False)).fit(
    df
).transform(new_df)
```


|     | x     |
|-----|-------|
| 0   | -0.25 |
| 1   | 2.00  |
