WindowSummarizer
Transformer for extracting time series features.
Quickstart
from sktime.transformations.summarize import WindowSummarizer
estimator = WindowSummarizer(lag_feature=None, n_jobs=-1, target_cols=None, truncate=None)Parameters(3)
- n_jobsint, optional (default=-1)
The number of jobs to run in parallel for applying the window functions.
-1means using all processors.- target_cols: list of str, optional (default = None)
Specifies which columns in X to target for applying the window functions.
Nonewill target the first column- lag_feature: dict of str and list, optional (default = dict containing first lag)
Dictionary specifying as key the type of function to be used and as value the argument
window. For the functionlag, the argumentwindowis an integer or a list of integers giving thelagvalues to be used. For all other functions, the argumentwindowis a list with the argumentslagandwindow length.lagdefines how far back in the past the window starts,window lengthgives the length of the window across which to apply the function. For multiple different windows, provide a list of lists.Please see below a graphical representation of the logic using the following symbols:
z= time stamp that the window is summarized to.Part of the window if
lagis between 0 and1-window_length, otherwise not part of the window.x= (other) time stamps in the window which is summarized*= observations, past or future, not part of the windowThe summarization function is applied to the window consisting of x and potentially z.
For
|---------------------------| | * * * * * * * * x x x z * | |---------------------------|window = [1, 3], we have alagof 1 andwindow_lengthof 3 to target the three last days (exclusive z) that were observed. Summarization is done across windows like this:For
|---------------------------| | * * * * * * * * x x z * * | |---------------------------|window = [0, 3], we have alagof 0 andwindow_lengthof 3 to target the three last days (inclusive z) that were observed. Summarization is done across windows like this:Special case
|---------------------------| | * * * * * * * * * * x z * | |---------------------------|lag: Since lags are frequently used and window length is redundant, you only need to provide a list oflagvalues. Sowindow = [1]will result in the first lag:And
|---------------------------| | * * * * * * * x * * x z * | |---------------------------| key: either custom function call (to be provided by user) or str corresponding to native pandas window function:window = [1, 4]will result in the first and fourth lag:“sum”,
“mean”,
“median”,
“std”,
“var”,
“kurt”,
“min”,
“max”,
“corr”,
“cov”,
“skew”,
“sem”
See also: https://pandas.pydata.org/docs/reference/window.html.
Examples
>>> import pandas as pd
>>> from sktime.transformations.summarize import WindowSummarizer
>>> from sktime.datasets import load_airline, load_longley
>>> from sktime.forecasting.naive import NaiveForecaster
>>> from sktime.forecasting.base import ForecastingHorizon
>>> from sktime.forecasting.compose import ForecastingPipeline
>>> from sktime.split import temporal_train_test_split
>>> y = load_airline ()
>>> kwargs = {
... "lag_feature": {
... "lag": [1 ],
... "mean": [[1, 3 ], [3, 6 ]],
... "std": [[1, 4 ]],
... }
... }
>>> transformer = WindowSummarizer (** kwargs)
>>> y_transformed = transformer. fit_transform (y) Example with transforming multiple columns of exogenous features
>>> y, X = load_longley ()
>>> y_train, y_test, X_train, X_test = temporal_train_test_split (y, X)
>>> fh = ForecastingHorizon (X_test. index, is_relative = False)
>>> # Example transforming only X
>>> pipe = ForecastingPipeline (
... steps = [
... ("a", WindowSummarizer (n_jobs = 1, target_cols = ["POP", "GNPDEFL" ])),
... ("b", WindowSummarizer (n_jobs = 1, target_cols = ["GNP" ], ** kwargs)),
... ("forecaster", NaiveForecaster (strategy = "drift")),
... ]
... )
>>> pipe_return = pipe. fit (y_train, X_train)
>>> y_pred1 = pipe_return. predict (fh = fh, X = X_test) Example with transforming multiple columns of exogenous features as well as the y column
>>> Z_train = pd. concat ([X_train, y_train ], axis = 1)
>>> Z_test = pd. concat ([X_test, y_test ], axis = 1)
>>> pipe = ForecastingPipeline (
... steps = [
... ("a", WindowSummarizer (n_jobs = 1, target_cols = ["POP", "TOTEMP" ])),
... ("b", WindowSummarizer (** kwargs, n_jobs = 1, target_cols = ["GNP" ])),
... ("forecaster", NaiveForecaster (strategy = "drift")),
... ]
... )
>>> pipe_return = pipe. fit (y_train, Z_train)
>>> y_pred2 = pipe_return. predict (fh = fh, X = Z_test)