Skip to content

MinMaxScaler

yohou.preprocessing.sklearn_wrappers.MinMaxScaler

Bases: SklearnScaler

Transform features by scaling each feature to a given range.

This estimator scales and translates each feature individually such that it is in the given range on the training set, e.g. between zero and one.

The transformation is given by::

X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
X_scaled = X_std * (max - min) + min

where min, max = feature_range.

This transformation is often used as an alternative to zero mean, unit variance scaling.

MinMaxScaler doesn't reduce the effect of outliers, but it linearly scales them down into a fixed range, where the largest occurring data point corresponds to the maximum value and the smallest one corresponds to the minimum value.

This is a Yohou wrapper that preserves the polars DataFrame structure and "time" column.

Parameters

Name Type Description Default
feature_range tuple(min, max)

Desired range of transformed data.

(0, 1)
clip bool

Set to True to clip transformed values of held-out data to the provided feature_range.

False

Attributes

Name Type Description
instance_ MinMaxScaler

The fitted sklearn MinMaxScaler instance.

min_ ndarray of shape (n_features,)

Per feature adjustment for minimum.

scale_ ndarray of shape (n_features,)

Per feature relative scaling of the data.

data_min_ ndarray of shape (n_features,)

Per feature minimum seen in the data.

data_max_ ndarray of shape (n_features,)

Per feature maximum seen in the data.

data_range_ ndarray of shape (n_features,)

Per feature range (data_max_ - data_min_) seen in the data.

Examples

>>> import polars as pl
>>> from datetime import datetime
>>> from yohou.preprocessing import MinMaxScaler
>>> X = pl.DataFrame({
...     "time": [datetime(2024, 1, i) for i in range(1, 6)],
...     "value": [10.0, 20.0, 30.0, 40.0, 50.0],
... })
>>> scaler = MinMaxScaler()
>>> scaler.fit(X)
MinMaxScaler(...)
>>> X_scaled = scaler.transform(X)
>>> # Values are scaled to [0, 1]
>>> X_scaled["value"].min()
0.0
>>> X_scaled["value"].max()
1.0

See Also

  • StandardScaler : Standardize features by removing mean and scaling to unit variance.
  • MaxAbsScaler : Scale each feature by its maximum absolute value.

Source Code

Show/Hide source
class MinMaxScaler(SklearnScaler):
    """Transform features by scaling each feature to a given range.

    This estimator scales and translates each feature individually such that it
    is in the given range on the training set, e.g. between zero and one.

    The transformation is given by::

        X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
        X_scaled = X_std * (max - min) + min

    where ``min, max = feature_range``.

    This transformation is often used as an alternative to zero mean, unit
    variance scaling.

    ``MinMaxScaler`` doesn't reduce the effect of outliers, but it linearly
    scales them down into a fixed range, where the largest occurring data point
    corresponds to the maximum value and the smallest one corresponds to the
    minimum value.

    This is a Yohou wrapper that preserves the polars DataFrame structure and
    "time" column.

    Parameters
    ----------
    feature_range : tuple (min, max), default=(0, 1)
        Desired range of transformed data.

    clip : bool, default=False
        Set to True to clip transformed values of held-out data to the provided
        ``feature_range``.

    Attributes
    ----------
    instance_ : sklearn.preprocessing.MinMaxScaler
        The fitted sklearn MinMaxScaler instance.

    min_ : ndarray of shape (n_features,)
        Per feature adjustment for minimum.

    scale_ : ndarray of shape (n_features,)
        Per feature relative scaling of the data.

    data_min_ : ndarray of shape (n_features,)
        Per feature minimum seen in the data.

    data_max_ : ndarray of shape (n_features,)
        Per feature maximum seen in the data.

    data_range_ : ndarray of shape (n_features,)
        Per feature range ``(data_max_ - data_min_)`` seen in the data.

    Examples
    --------
    >>> import polars as pl
    >>> from datetime import datetime
    >>> from yohou.preprocessing import MinMaxScaler
    >>> X = pl.DataFrame({
    ...     "time": [datetime(2024, 1, i) for i in range(1, 6)],
    ...     "value": [10.0, 20.0, 30.0, 40.0, 50.0],
    ... })
    >>> scaler = MinMaxScaler()
    >>> scaler.fit(X)  # doctest: +ELLIPSIS
    MinMaxScaler(...)
    >>> X_scaled = scaler.transform(X)
    >>> # Values are scaled to [0, 1]
    >>> X_scaled["value"].min()
    0.0
    >>> X_scaled["value"].max()
    1.0

    See Also
    --------
    - [`StandardScaler`][yohou.preprocessing.sklearn_wrappers.StandardScaler] : Standardize features by removing mean and scaling to unit variance.
    - [`MaxAbsScaler`][yohou.preprocessing.sklearn_wrappers.MaxAbsScaler] : Scale each feature by its maximum absolute value.

    """

    _estimator_default_class = sklearn_MinMaxScaler

    def __init__(self, feature_range=(0, 1), copy=True, clip=False, **kwargs):
        super().__init__(feature_range=feature_range, copy=copy, clip=clip, **kwargs)

    @property
    def min_(self) -> np.ndarray:
        """Per feature adjustment for minimum."""
        check_is_fitted(self, ["instance_"])
        return self.instance_.min_

    @property
    def scale_(self) -> np.ndarray:
        """Per feature relative scaling of the data."""
        check_is_fitted(self, ["instance_"])
        return self.instance_.scale_

    @property
    def data_min_(self) -> np.ndarray:
        """Per feature minimum seen in the data."""
        check_is_fitted(self, ["instance_"])
        return self.instance_.data_min_

    @property
    def data_max_(self) -> np.ndarray:
        """Per feature maximum seen in the data."""
        check_is_fitted(self, ["instance_"])
        return self.instance_.data_max_

    @property
    def data_range_(self) -> np.ndarray:
        """Per feature range seen in the data."""
        check_is_fitted(self, ["instance_"])
        return self.instance_.data_range_

Methods

min_ property

Per feature adjustment for minimum.

scale_ property

Per feature relative scaling of the data.

data_min_ property

Per feature minimum seen in the data.

data_max_ property

Per feature maximum seen in the data.

data_range_ property

Per feature range seen in the data.

Tutorials

The following example notebooks use this component:

  • How to Use Scikit-learn Scalers


    Data-Features

    Wrap sklearn scalers (StandardScaler, MinMaxScaler, RobustScaler, PowerTransformer, PolynomialFeatures) for polars DataFrames with inverse transforms.

    View · Open in marimo