Skip to content

WindowSummarizer

WindowSummarizer

class WindowSummarizer(lag_feature=None, n_jobs=-1, target_cols=None, truncate=None)[source]

Transformer for extracting time series features.

The WindowSummarizer transforms input series to features based on a provided dictionary of window summarizer, window shifts and window lengths.

Parameters:
n_jobsint, optional (default=-1)

The number of jobs to run in parallel for applying the window functions. -1 means using all processors.

target_cols: list of str, optional (default = None)

Specifies which columns in X to target for applying the window functions. None will 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 function lag, the argument window is an integer or a list of integers giving the lag values to be used. For all other functions, the argument window is a list with the arguments lag and window length. lag defines how far back in the past the window starts, window length gives 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 lag is between 0 and 1-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 window

The summarization function is applied to the window consisting of x and potentially z.

For window = [1, 3], we have a lag of 1 and window_length of 3 to target the three last days (exclusive z) that were observed. Summarization is done across windows like this:

|---------------------------|
| * * * * * * * * x x x z * |
|---------------------------|

For window = [0, 3], we have a lag of 0 and window_length of 3 to target the three last days (inclusive z) that were observed. Summarization is done across windows like this:

|---------------------------|
| * * * * * * * * x x z * * |
|---------------------------|

Special case lag: Since lags are frequently used and window length is redundant, you only need to provide a list of lag values. So window = [1] will result in the first lag:

|---------------------------|
| * * * * * * * * * * x z * |
|---------------------------|

And window = [1, 4] will result in the first and fourth lag:

|---------------------------|
| * * * * * * * x * * x z * |
|---------------------------|
key: either custom function call (to be provided by user) or
str corresponding to native pandas window function:
  • “sum”,

  • “mean”,

  • “median”,

  • “std”,

  • “var”,

  • “kurt”,

  • “min”,

  • “max”,

  • “corr”,

  • “cov”,

  • “skew”,

  • “sem”

See also: https://pandas.pydata.org/docs/reference/window.html.

The column generated will be named after the key provided, followed by the lag parameter and the window_length (if not a lag).

second value (window): list of integers

List containing lag and window_length parameters.

truncate: str, optional (default = None)

Defines how to deal with NAs that were created as a result of applying the functions in the lag_feature dict across windows that are longer than the remaining history of data. For example a lag config of [14, 7] cannot be fully applied for the first 20 observations of the targeted column. A lag_feature of [[8, 14], [1, 28]] cannot be correctly applied for the first 21 resp. 28 observations of the targeted column. Possible values to deal with those NAs:

  • None

  • “bfill”

None will keep the NAs generated, and would leave it for the user to choose an estimator that can correctly deal with observations with missing values, “bfill” will fill the NAs by carrying the first observation backwards.

Returns:
X: pd.DataFrame

Contains all transformed columns as well as non-transformed columns. The raw inputs to transformed columns will be dropped.

self: reference to self
Attributes:
truncate_startint

See section Parameters - truncate for a more detailed explanation of truncation as a result of applying windows of certain lengths across past observations. Truncate_start will give the maximum of observations that are filled with NAs across all arguments of the lag_feature when truncate is set to None.

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)

Methods

check_is_fitted([method_name])

Check if the estimator has been fitted.

clone()

Obtain a clone of the object with same hyper-parameters and config.

clone_tags(estimator[, tag_names])

Clone tags from another object as dynamic override.

create_test_instance([parameter_set])

Construct an instance of the class, using first test parameter set.

create_test_instances_and_names([parameter_set])

Create list of all test instances and a list of names for them.

fit(X[, y])

Fit transformer to X, optionally to y.

fit_transform(X[, y])

Fit to data, then transform it.

get_class_tag(tag_name[, tag_value_default])

Get class tag value from class, with tag level inheritance from parents.

get_class_tags()

Get class tags from class, with tag level inheritance from parent classes.

get_config()

Get config flags for self.

get_fitted_params([deep])

Get fitted parameters.

get_param_defaults()

Get object's parameter defaults.

get_param_names([sort])

Get object's parameter names.

get_params([deep])

Get a dict of parameters values for this object.

get_tag(tag_name[, tag_value_default, ...])

Get tag value from instance, with tag level inheritance and overrides.

get_tags()

Get tags from instance, with tag level inheritance and overrides.

get_test_params([parameter_set])

Return testing parameter settings for the estimator.

inverse_transform(X[, y])

Inverse transform X and return an inverse transformed version.

is_composite()

Check if the object is composed of other BaseObjects.

load_from_path(serial)

Load object from file location.

load_from_serial(serial)

Load object from serialized memory container.

reset()

Reset the object to a clean post-init state.

save([path, serialization_format])

Save serialized self to bytes-like object or to (.zip) file.

set_config(**config_dict)

Set config flags to given values.

set_params(**params)

Set the parameters of this object.

set_random_state([random_state, deep, ...])

Set random_state pseudo-random seed parameters for self.

set_tags(**tag_dict)

Set instance level tag overrides to given values.

transform(X[, y])

Transform X and return a transformed version.

update(X[, y, update_params])

Update transformer with X, optionally y.