Skip to content

scale_to_range

ScaleToRange(*, features=None, lower_range=-1.0, upper_range=1.0) dataclass

Bases: Transform, FitOnce

Scale features to a fixed range using a linear mapping.

A sample \(x\) is scaled as:

\[ z = x \cdot m + b \]

where

  • \(m\) is the scaling factor
  • \(b\) is the offset.

When instantiating this transform directly, the scaling factor \(m\) and offset \(b\) for each feature are calculated during training from the data. To specify the scaling factor \(m\) and offset \(b\) directly, use the from_limits method.

Attributes:

Name Type Description
m dict[str, float] | None

The scaling factor \(m\) of each feature.

b dict[str, float] | None

The offset \(b\) of each feature.

Source code in src/flowcean/polars/transforms/scale_to_range.py
43
44
45
46
47
48
49
50
51
52
def __init__(
    self,
    *,
    features: list[str] | None = None,
    lower_range: float = -1.0,
    upper_range: float = 1.0,
) -> None:
    self.features = features
    self.lower_range = lower_range
    self.upper_range = upper_range

from_limits(feature_limits, *, lower_range=-1.0, upper_range=1.0) classmethod

Creates a new ScaleToRange transform based on the given limits.

Parameters:

Name Type Description Default
feature_limits dict[str, tuple[float, float]]

A dictionary mapping each features name to its (min_value, max_value) tuple.

required
lower_range float

The lower bound of the range to scale to.

-1.0
upper_range float

The upper bound of the range to scale to.

1.0
Source code in src/flowcean/polars/transforms/scale_to_range.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
@classmethod
def from_limits(
    cls,
    feature_limits: dict[str, tuple[float, float]],
    *,
    lower_range: float = -1.0,
    upper_range: float = 1.0,
) -> Self:
    """Creates a new ScaleToRange transform based on the given limits.

    Args:
        feature_limits: A dictionary mapping each features name to its
            (min_value, max_value) tuple.
        lower_range: The lower bound of the range to scale to.
        upper_range: The upper bound of the range to scale to.

    """
    transform = cls(
        features=list(feature_limits.keys()),
        lower_range=lower_range,
        upper_range=upper_range,
    )

    transform.m = {
        feature: (upper_range - lower_range) / (max_value - min_value)
        for feature, (max_value, min_value) in feature_limits.items()
    }

    transform.b = {
        feature: min_value
        * (upper_range - lower_range)
        / (max_value - min_value)
        + lower_range
        for feature, (max_value, min_value) in feature_limits.items()
    }

    return transform