MODULE REFERENCE
static_covariates_transformer
anomx.data.processing.transformers.static_covariates_transformerStatic Covariates Transformer
StaticCovariatesTransformer
StaticCovariatesTransformer(transformer_num = None, transformer_cat = None, cols_num: list[str] | None = None, cols_cat: list[str] | None = None, name = 'StaticCovariatesTransformer', n_jobs: int = 1, verbose: bool = False)Bases: FittableDataTransformer, InvertibleDataTransformer
| Parameter | Type | Default |
|---|---|---|
transformer_num | — | None |
transformer_cat | — | None |
cols_num | list[str] | None | None |
cols_cat | list[str] | None | None |
name | — | 'StaticCovariatesTransformer' |
n_jobs | int | 1 |
verbose | bool | False |
Generic wrapper class for scalers/encoders/transformers of static covariates. This transformer acts only on static covariates of the series passed to fit(), transform(), fit_transform(), and inverse_transform(). It can both scale numerical features, as well as encode categorical features.
The underlying transformer_num and transformer_cat have to implement the fit(), transform(), and inverse_transform() methods (typically from scikit-learn).
By default, numerical and categorical columns/features are inferred and allocated to transformer_num and transformer_cat, respectively. Alternatively, specify which columns to scale/transform with cols_num and cols_cat.
Both transformer_num and transformer_cat are fit globally on static covariate data from all series passed to anomx.data.processing.transformers.StaticCovariatesTransformer.fit
Parameters
- transformer_num
The transformer to transform numeric static covariate columns with. It must provide
fit(),transform()andinverse_transform()methods. Default:sklearn.preprocessing.MinMaxScalerwithfeature_range=(0, 1); this will scale all values between 0 and 1.- transformer_cat
The encoder to transform categorical static covariate columns with. It must provide
fit(),transform()andinverse_transform()methods. Default:sklearn.preprocessing.OrdinalEncoder; this will convert categories into integer valued arrays where each integer stands for a specific category.- cols_num
Optionally, a list of column names for which to apply the numeric transformer
transformer_num. By default, the transformer will infer all numerical features based on types, and scale them withtransformer_num. If an empty list, no column will be scaled.- cols_cat
Optionally, a list of column names for which to apply the categorical transformer
transformer_cat. By default, the transformer will infer all categorical features based on types, and transform them withtransformer_cat. If an empty list, no column will be transformed.- name
A specific name for the
anomx.data.processing.transformers.StaticCovariatesTransformer.- n_jobs
The number of jobs to run in parallel. Parallel jobs are created only when a
Sequence[TimeSeries]is passed as input to a method, parallelising operations regarding differentTimeSeries. Defaults to1(sequential). Setting the parameter to-1means using all the available processors. Note: for a small amount of data, the parallelisation overhead could end up increasing the total required amount of time.- verbose
Optionally, whether to print operations progress
Examples
>>> import numpy as np
>>> import pandas as pd
>>> from anomx.data.timeseries import TimeSeries
>>> from anomx.data.processing.transformers import StaticCovariatesTransformer
>>> static_covs = pd.DataFrame(data={"num": [0, 2, 1], "cat": ["a", "c", "b"]})
>>> series = TimeSeries.from_values(
>>> values=np.random.random((10, 3)),
>>> columns=["comp1", "comp2", "comp3"],
>>> static_covariates=static_covs,
>>> )
>>> transformer = StaticCovariatesTransformer()
>>> series_transformed = transformer.fit_transform(series)
>>> print(series.static_covariates)
static_covariates num cat
component
comp1 0.0 a
comp2 2.0 c
comp3 1.0 b
>>> print(series_transformed.static_covariates)
static_covariates num cat
component
comp1 0.0 0.0
comp2 1.0 2.0
comp3 0.5 1.0Members
ts_fitstaticmethod
ts_fit(series: Sequence[TimeSeries], params: dict[str, dict[str, Any]], *args, **kwargs)Collates static covariates of all provided TimeSeries and fits the following parameters:
1. `transformer_num`, the fitted numerical static covariate transformer.
2. `transformer_cat`, the fitted categorical static covariate transformer.
3. `mask_num`, a dictionary containing two boolean arrays: one that indicates which
components of the *untransformed* static covariates are numerical, and another that
indicates which components of the *transformed* static covariates are numerical.
4. `mask_cat`, a dictionary containing two boolean arrays: one that indicates which
components of the *untransformed* static covariates are categorical, and another that
indicates which components of the *transformed* static covariates are categorical.
5. `n_cat_cols`, a dictionary that stores the number of categorical columns
we should expect in the untransformed and in the transformed static covariates.| Parameter | Type | Default |
|---|---|---|
series | Sequence[TimeSeries] | required |
params | dict[str, dict[str, Any]] | required |
*args | — | — |
**kwargs | — | — |
ts_transformstaticmethod
ts_transform(series: TimeSeries, params: dict[str, Any], *args, **kwargs) -> TimeSeries| Parameter | Type | Default |
|---|---|---|
series | TimeSeries | required |
params | dict[str, Any] | required |
*args | — | — |
**kwargs | — | — |
Returns TimeSeries
ts_inverse_transformstaticmethod
ts_inverse_transform(series: TimeSeries, params: Mapping[str, Any], insample: TimeSeries | None = None, **kwargs) -> TimeSeries| Parameter | Type | Default |
|---|---|---|
series | TimeSeries | required |
params | Mapping[str, Any] | required |
insample | TimeSeries | None | None |
**kwargs | — | — |
Returns TimeSeries
Inherited members
fitFittableDataTransformertransformFittableDataTransformerfit_transformFittableDataTransformerset_verboseBaseDataTransformerset_n_jobsBaseDataTransformerapply_component_maskBaseDataTransformerunapply_component_maskBaseDataTransformerstack_samplesBaseDataTransformerunstack_samplesBaseDataTransformernameBaseDataTransformerinverse_transformInvertibleDataTransformer