Skip to content

Scalers

cesnet_tszoo.utils.scaler

Scaler

Bases: ABC

Base class for scalers, used for transforming data.

This class serves as the foundation for creating custom scalers. To implement a custom scaler, this class is recommended to be subclassed and extended.

Example:

import numpy as np

class LogScaler(Scaler):

    def fit(self, data: np.ndarray):
        ...

    def partial_fit(self, data: np.ndarray) -> None:
        ...

    def transform(self, data: np.ndarray):
        log_data = np.ma.log(data)

        return log_data.filled(np.NaN)
Source code in cesnet_tszoo\utils\scaler.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Scaler(ABC):
    """
    Base class for scalers, used for transforming data.

    This class serves as the foundation for creating custom scalers. To implement a custom scaler, this class is recommended to be subclassed and extended.

    Example:

        import numpy as np

        class LogScaler(Scaler):

            def fit(self, data: np.ndarray):
                ...

            def partial_fit(self, data: np.ndarray) -> None:
                ...

            def transform(self, data: np.ndarray):
                log_data = np.ma.log(data)

                return log_data.filled(np.NaN)
    """

    @abstractmethod
    def fit(self, data: np.ndarray) -> None:
        """
        Sets the scaler values for a given time series part.

        This method must be implemented if using multiple scalers that have not been pre-fitted.

        Parameters:
            data: A numpy array representing data for a single time series with shape `(times, features)` excluding any identifiers.  
        """
        ...

    @abstractmethod
    def partial_fit(self, data: np.ndarray) -> None:
        """
        Partially sets the scaler values for a given time series part.

        This method must be implemented if using a single scaler that is not pre-fitted for all time series, or when using pre-fitted scaler(s) with `partial_fit_initialized_scalers` set to `True`.

        Parameters:
            data: A numpy array representing data for a single time series with shape `(times, features)` excluding any identifiers.        
        """
        ...

    @abstractmethod
    def transform(self, data: np.ndarray) -> np.ndarray:
        """
        Transforms the input data for a given time series part.

        This method must always be implemented.

        Parameters:
            data: A numpy array representing data for a single time series with shape `(times, features)` excluding any identifiers.  

        Returns:
            The transformed data, with the same shape as the input `(times, features)`.            
        """
        ...

fit abstractmethod

fit(data: ndarray) -> None

Sets the scaler values for a given time series part.

This method must be implemented if using multiple scalers that have not been pre-fitted.

Parameters:

Name Type Description Default
data ndarray

A numpy array representing data for a single time series with shape (times, features) excluding any identifiers.

required
Source code in cesnet_tszoo\utils\scaler.py
34
35
36
37
38
39
40
41
42
43
44
@abstractmethod
def fit(self, data: np.ndarray) -> None:
    """
    Sets the scaler values for a given time series part.

    This method must be implemented if using multiple scalers that have not been pre-fitted.

    Parameters:
        data: A numpy array representing data for a single time series with shape `(times, features)` excluding any identifiers.  
    """
    ...

partial_fit abstractmethod

partial_fit(data: ndarray) -> None

Partially sets the scaler values for a given time series part.

This method must be implemented if using a single scaler that is not pre-fitted for all time series, or when using pre-fitted scaler(s) with partial_fit_initialized_scalers set to True.

Parameters:

Name Type Description Default
data ndarray

A numpy array representing data for a single time series with shape (times, features) excluding any identifiers.

required
Source code in cesnet_tszoo\utils\scaler.py
46
47
48
49
50
51
52
53
54
55
56
@abstractmethod
def partial_fit(self, data: np.ndarray) -> None:
    """
    Partially sets the scaler values for a given time series part.

    This method must be implemented if using a single scaler that is not pre-fitted for all time series, or when using pre-fitted scaler(s) with `partial_fit_initialized_scalers` set to `True`.

    Parameters:
        data: A numpy array representing data for a single time series with shape `(times, features)` excluding any identifiers.        
    """
    ...

transform abstractmethod

transform(data: ndarray) -> np.ndarray

Transforms the input data for a given time series part.

This method must always be implemented.

Parameters:

Name Type Description Default
data ndarray

A numpy array representing data for a single time series with shape (times, features) excluding any identifiers.

required

Returns:

Type Description
ndarray

The transformed data, with the same shape as the input (times, features).

Source code in cesnet_tszoo\utils\scaler.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@abstractmethod
def transform(self, data: np.ndarray) -> np.ndarray:
    """
    Transforms the input data for a given time series part.

    This method must always be implemented.

    Parameters:
        data: A numpy array representing data for a single time series with shape `(times, features)` excluding any identifiers.  

    Returns:
        The transformed data, with the same shape as the input `(times, features)`.            
    """
    ...

LogScaler

Bases: Scaler

Tranforms data with natural logarithm. Zero or invalid values are set to np.NaN.

Corresponds to enum ScalerType.LOG_SCALER or literal log_scaler.

Source code in cesnet_tszoo\utils\scaler.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
class LogScaler(Scaler):
    """
    Tranforms data with natural logarithm. Zero or invalid values are set to `np.NaN`.

    Corresponds to enum [`ScalerType.LOG_SCALER`][cesnet_tszoo.utils.enums.ScalerType] or literal `log_scaler`.
    """

    def fit(self, data: np.ndarray):
        ...

    def partial_fit(self, data: np.ndarray) -> None:
        ...

    def transform(self, data: np.ndarray):
        log_data = np.ma.log(data)

        return log_data.filled(np.NaN)

L2Normalizer

Bases: Scaler

Tranforms data using Scikit L2Normalizer.

Corresponds to enum ScalerType.L2_NORMALIZER or literal l2_normalizer.

Source code in cesnet_tszoo\utils\scaler.py
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
class L2Normalizer(Scaler):
    """
    Tranforms data using Scikit [`L2Normalizer`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.Normalizer.html).

    Corresponds to enum [`ScalerType.L2_NORMALIZER`][cesnet_tszoo.utils.enums.ScalerType] or literal `l2_normalizer`.
    """

    def __init__(self):
        self.scaler = sk.Normalizer(norm="l2")

    def fit(self, data: np.ndarray):
        ...

    def partial_fit(self, data: np.ndarray) -> None:
        ...

    def transform(self, data: np.ndarray):
        return self.scaler.fit_transform(data)

MinMaxScaler

Bases: Scaler

Tranforms data using Scikit MinMaxScaler.

Corresponds to enum ScalerType.MIN_MAX_SCALER or literal min_max_scaler.

Source code in cesnet_tszoo\utils\scaler.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class MinMaxScaler(Scaler):
    """
    Tranforms data using Scikit [`MinMaxScaler`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html).

    Corresponds to enum [`ScalerType.MIN_MAX_SCALER`][cesnet_tszoo.utils.enums.ScalerType] or literal `min_max_scaler`.
    """

    def __init__(self):
        self.scaler = sk.MinMaxScaler()

    def fit(self, data: np.ndarray):
        self.scaler.fit(data)

    def partial_fit(self, data: np.ndarray) -> None:
        self.scaler.partial_fit(data)

    def transform(self, data: np.ndarray):
        return self.scaler.transform(data)

StandardScaler

Bases: Scaler

Tranforms data using Scikit StandardScaler.

Corresponds to enum ScalerType.STANDARD_SCALER or literal standard_scaler.

Source code in cesnet_tszoo\utils\scaler.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
class StandardScaler(Scaler):
    """
    Tranforms data using Scikit [`StandardScaler`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html).

    Corresponds to enum [`ScalerType.STANDARD_SCALER`][cesnet_tszoo.utils.enums.ScalerType] or literal `standard_scaler`.
    """

    def __init__(self):
        self.scaler = sk.StandardScaler()

    def fit(self, data: np.ndarray):
        self.scaler.fit(data)

    def partial_fit(self, data: np.ndarray) -> None:
        self.scaler.partial_fit(data)

    def transform(self, data: np.ndarray):
        return self.scaler.transform(data)

MaxAbsScaler

Bases: Scaler

Tranforms data using Scikit MaxAbsScaler.

Corresponds to enum ScalerType.MAX_ABS_SCALER or literal max_abs_scaler.

Source code in cesnet_tszoo\utils\scaler.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class MaxAbsScaler(Scaler):
    """
    Tranforms data using Scikit [`MaxAbsScaler`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MaxAbsScaler.html).

    Corresponds to enum [`ScalerType.MAX_ABS_SCALER`][cesnet_tszoo.utils.enums.ScalerType] or literal `max_abs_scaler`.
    """

    def __init__(self):
        self.scaler = sk.MaxAbsScaler()

    def fit(self, data: np.ndarray):
        self.scaler.fit(data)

    def partial_fit(self, data: np.ndarray) -> None:
        self.scaler.partial_fit(data)

    def transform(self, data: np.ndarray):
        return self.scaler.transform(data)

PowerTransformer

Bases: Scaler

Tranforms data using Scikit PowerTransformer.

Corresponds to enum ScalerType.POWER_TRANSFORMER or literal power_transformer.

partial_fit not supported

Because this transformer does not support partial_fit it can't be used when using one scaler that needs to be fitted for multiple time series.

Source code in cesnet_tszoo\utils\scaler.py
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
class PowerTransformer(Scaler):
    """
    Tranforms data using Scikit [`PowerTransformer`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.PowerTransformer.html).

    Corresponds to enum [`ScalerType.POWER_TRANSFORMER`][cesnet_tszoo.utils.enums.ScalerType] or literal `power_transformer`.

    !!! warning "partial_fit not supported"
        Because this transformer does not support partial_fit it can't be used when using one scaler that needs to be fitted for multiple time series.
    """

    def __init__(self):
        self.scaler = sk.PowerTransformer()

    def fit(self, data: np.ndarray):
        self.scaler.fit(data)

    def partial_fit(self, data: np.ndarray) -> None:
        raise NotImplementedError("PowerTransformer does not support partial_fit.")

    def transform(self, data: np.ndarray):
        return self.scaler.transform(data)

QuantileTransformer

Bases: Scaler

Tranforms data using Scikit QuantileTransformer.

Corresponds to enum ScalerType.QUANTILE_TRANSFORMER or literal quantile_transformer.

partial_fit not supported

Because this transformer does not support partial_fit it can't be used when using one scaler that needs to be fitted for multiple time series.

Source code in cesnet_tszoo\utils\scaler.py
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
class QuantileTransformer(Scaler):
    """
    Tranforms data using Scikit [`QuantileTransformer`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.QuantileTransformer.html).

    Corresponds to enum [`ScalerType.QUANTILE_TRANSFORMER`][cesnet_tszoo.utils.enums.ScalerType] or literal `quantile_transformer`.

    !!! warning "partial_fit not supported"
        Because this transformer does not support partial_fit it can't be used when using one scaler that needs to be fitted for multiple time series.    
    """

    def __init__(self):
        self.scaler = sk.QuantileTransformer()

    def fit(self, data: np.ndarray):
        self.scaler.fit(data)

    def partial_fit(self, data: np.ndarray) -> None:
        raise NotImplementedError("QuantileTransformer does not support partial_fit.")

    def transform(self, data: np.ndarray):
        return self.scaler.transform(data)

RobustScaler

Bases: Scaler

Tranforms data using Scikit RobustScaler.

Corresponds to enum ScalerType.ROBUST_SCALER or literal robust_scaler.

partial_fit not supported

Because this scaler does not support partial_fit it can't be used when using one scaler that needs to be fitted for multiple time series.

Source code in cesnet_tszoo\utils\scaler.py
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
class RobustScaler(Scaler):
    """
    Tranforms data using Scikit [`RobustScaler`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.RobustScaler.html).

    Corresponds to enum [`ScalerType.ROBUST_SCALER`][cesnet_tszoo.utils.enums.ScalerType] or literal `robust_scaler`.

    !!! warning "partial_fit not supported"
        Because this scaler does not support partial_fit it can't be used when using one scaler that needs to be fitted for multiple time series.    
    """

    def __init__(self):
        self.scaler = sk.RobustScaler()

    def fit(self, data: np.ndarray):
        self.scaler.fit(data)

    def partial_fit(self, data: np.ndarray) -> None:
        raise NotImplementedError("RobustScaler does not support partial_fit.")

    def transform(self, data: np.ndarray):
        return self.scaler.transform(data)