Skip to content

MaxAbsScaler

yohou.preprocessing.sklearn_wrappers.MaxAbsScaler

Bases: SklearnScaler

Scale each feature by its maximum absolute value.

This estimator scales and translates each feature individually such that the maximal absolute value of each feature in the training set will be 1.0. It does not shift/center the data, and thus does not destroy any sparsity.

MaxAbsScaler doesn't reduce the effect of outliers; it only linearly scales them down.

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

Parameters

Name Type Description Default
clip bool

Set to True to clip transformed values of held-out data to [-1, 1].

False

Attributes

Name Type Description
instance_ MaxAbsScaler

The fitted sklearn MaxAbsScaler instance.

scale_ ndarray of shape (n_features,)

Per feature relative scaling of the data.

max_abs_ ndarray of shape (n_features,)

Per feature maximum absolute value.

Examples

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

See Also

  • MinMaxScaler : Scale features to a given range.
  • StandardScaler : Standardize features by removing mean and scaling to unit variance.

Source Code

Show/Hide source
class MaxAbsScaler(SklearnScaler):
    """Scale each feature by its maximum absolute value.

    This estimator scales and translates each feature individually such that
    the maximal absolute value of each feature in the training set will be 1.0.
    It does not shift/center the data, and thus does not destroy any sparsity.

    ``MaxAbsScaler`` doesn't reduce the effect of outliers; it only linearly
    scales them down.

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

    Parameters
    ----------
    clip : bool, default=False
        Set to True to clip transformed values of held-out data to [-1, 1].

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

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

    max_abs_ : ndarray of shape (n_features,)
        Per feature maximum absolute value.

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

    See Also
    --------
    - [`MinMaxScaler`][yohou.preprocessing.sklearn_wrappers.MinMaxScaler] : Scale features to a given range.
    - [`StandardScaler`][yohou.preprocessing.sklearn_wrappers.StandardScaler] : Standardize features by removing mean and scaling to unit variance.

    """

    _estimator_default_class = sklearn_MaxAbsScaler

    def __init__(self, copy=True, clip=False, **kwargs):
        params = _filter_estimator_params(sklearn_MaxAbsScaler, {"copy": copy, "clip": clip})
        super().__init__(**params, **kwargs)

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

    @property
    def max_abs_(self) -> np.ndarray:
        """Per feature maximum absolute value."""
        check_is_fitted(self, ["instance_"])
        return self.instance_.max_abs_

Methods

scale_ property

Per feature relative scaling of the data.

max_abs_ property

Per feature maximum absolute value.