Skip to content

SklearnScaler

yohou.preprocessing.sklearn_base.SklearnScaler

Bases: SklearnTransformer

Wrapper to integrate sklearn scalers into the Yohou pipeline.

Preserves the polars DataFrame structure and "time" column while applying sklearn scaling transformations to numeric columns.

This class can be used to:

  1. Wrap any sklearn-compatible scaler for use in yohou pipelines
  2. Serve as a base class for creating yohou scaler extensions

Parameters

Name Type Description Default
scaler type

The sklearn scaler class to wrap. Must be a subclass of sklearn.base.TransformerMixin. If not provided, _estimator_default_class is used (subclasses define this).

None
**params dict

Parameters passed to the underlying sklearn scaler constructor. See the documentation of the specific scaler for available parameters.

{}

Attributes

Name Type Description
instance_ TransformerMixin

The fitted sklearn scaler instance (created by BaseClassWrapper).

Examples

>>> import polars as pl
>>> from datetime import datetime
>>> from sklearn.preprocessing import StandardScaler as SklearnStandardScaler
>>> from yohou.preprocessing import SklearnScaler
>>> 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 = SklearnScaler(scaler=SklearnStandardScaler, with_mean=True)
>>> scaler.fit(X)
SklearnScaler(...)
>>> X_scaled = scaler.transform(X)
>>> "time" in X_scaled.columns
True

See Also

  • StandardScaler : Pre-configured wrapper for sklearn's StandardScaler.
  • MinMaxScaler : Pre-configured wrapper for sklearn's MinMaxScaler.
  • RobustScaler : Pre-configured wrapper for sklearn's RobustScaler.
  • MaxAbsScaler : Pre-configured wrapper for sklearn's MaxAbsScaler.

Source Code

Show/Hide source
class SklearnScaler(SklearnTransformer):
    """Wrapper to integrate sklearn scalers into the Yohou pipeline.

    Preserves the polars DataFrame structure and "time" column while applying
    sklearn scaling transformations to numeric columns.

    This class can be used to:

    1. Wrap any sklearn-compatible scaler for use in yohou pipelines
    2. Serve as a base class for creating yohou scaler extensions

    Parameters
    ----------
    scaler : type, default=None
        The sklearn scaler class to wrap. Must be a subclass of
        ``sklearn.base.TransformerMixin``. If not provided,
        ``_estimator_default_class`` is used (subclasses define this).

    **params : dict
        Parameters passed to the underlying sklearn scaler constructor.
        See the documentation of the specific scaler for available parameters.

    Attributes
    ----------
    instance_ : TransformerMixin
        The fitted sklearn scaler instance (created by ``BaseClassWrapper``).

    Examples
    --------
    >>> import polars as pl
    >>> from datetime import datetime
    >>> from sklearn.preprocessing import StandardScaler as SklearnStandardScaler
    >>> from yohou.preprocessing import SklearnScaler
    >>> 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 = SklearnScaler(scaler=SklearnStandardScaler, with_mean=True)
    >>> scaler.fit(X)  # doctest: +ELLIPSIS
    SklearnScaler(...)
    >>> X_scaled = scaler.transform(X)
    >>> "time" in X_scaled.columns
    True

    See Also
    --------
    - [`StandardScaler`][yohou.preprocessing.sklearn_wrappers.StandardScaler] : Pre-configured wrapper for sklearn's StandardScaler.
    - [`MinMaxScaler`][yohou.preprocessing.sklearn_wrappers.MinMaxScaler] : Pre-configured wrapper for sklearn's MinMaxScaler.
    - [`RobustScaler`][yohou.preprocessing.sklearn_wrappers.RobustScaler] : Pre-configured wrapper for sklearn's RobustScaler.
    - [`MaxAbsScaler`][yohou.preprocessing.sklearn_wrappers.MaxAbsScaler] : Pre-configured wrapper for sklearn's MaxAbsScaler.

    """

    _estimator_name = "scaler"
    _estimator_base_class = TransformerMixin
    _estimator_default_class: type | None = None

    _parameter_constraints: dict = {
        "scaler": [HasMethods(["fit", "transform"]), None],
    }

    _tags = {"stateful": False, "invertible": True}

    def __init__(self, scaler=None, **params):
        if scaler is not None:
            super().__init__(scaler=scaler, **params)
        else:
            super().__init__(**params)