WindowFeatures#
- class feature_engine.timeseries.forecasting.WindowFeatures(variables=None, window=3, min_periods=None, functions='mean', periods=1, freq=None, sort_index=True, missing_values='raise', drop_original=False, drop_na=False)[source]#
WindowFeatures adds new features to a dataframe based on window operations. Window operations are operations that perform an aggregation over a sliding partition of past values. A window feature is, in other words, a feature created after computing statistics (e.g., mean, min, max, etc.) using a window over the past data. For example, the mean value of the previous 3 months of data is a window feature. The maximum value of the previous three rows of data is another window feature.
WindowFeatures uses pandas functions
rolling()
,agg()
andshift()
. Withrolling()
, it creates rolling windows. Withagg()
it applies multiple functions within those windows. Withshift()
it allocates the values to the correct rows.For supported aggregation functions, see Rolling Window Functions.
With pandas
rolling()
we can perform rolling operations over 1 window size at a time. WindowFeatures builds on top of pandasrolling()
in that new features can be derived from multiple window sizes, and the created features will be automatically concatenated to the original dataframe.To be compatible with WindowFeatures, the dataframe’s index must have unique values and no missing data.
WindowFeatures works only with numerical variables. You can pass a list of variables to use as input for the windows. Alternatively, WindowFeatures will automatically select all numerical variables in the training set.
More details in the User Guide.
- Parameters
- variables: list, default=None
The list of numerical variables to transform. If None, the transformer will automatically find and select all numerical variables.
- window: int, offset, BaseIndexer subclass, or list, default=3
Size of the moving window. If an integer, the fixed number of observations used for each window. If an offset (recommended), the time period of each window. It can also take a function. See parameter
windows
in pandasrolling()
documentation for more details.In addition to pandas normal input values,
window
can also take a list with the above specified values, in which case, features will be created for each one of the windows specified in the list.- min_periods: int, default None.
Minimum number of observations in the window required to have a value; otherwise, the result is np.nan. See parameter
min_periods
in pandasrolling()
documentation for more details.- functions: string or list of strings, default = ‘mean’
The functions to apply within the window. Valid functions can be found here.
- periods: int, list of ints, default=1
Number of periods to shift. Can be a positive integer. See param
periods
in pandasshift()
.- freq: str, list of str, default=None
Offset to use from the tseries module or time rule. See parameter
freq
in pandasshift()
.- sort_index: bool, default=True
Whether to order the index of the dataframe before creating the features.
- missing_values: string, default=’raise’
Indicates if missing values should be ignored or raised. If
'raise'
the transformer will return an error if the the datasets tofit
ortransform
contain missing values. If'ignore'
, missing data will be ignored when learning parameters or performing the transformation.- drop_original: bool, default=False
If True, the original variables to transform will be dropped from the dataframe.
- drop_na: bool, default=False.
Whether the NAN introduced in the lag features should be removed.
- Attributes
- variables_:
The group of variables that will be used to create the window features.
- feature_names_in_:
List with the names of features seen during
fit
.- n_features_in_:
The number of features in the train set used in fit.
See also
pandas.rolling
pandas.aggregate
pandas.shift
Examples
>>> import pandas as pd >>> from feature_engine.timeseries.forecasting import WindowFeatures >>> X = pd.DataFrame(dict(date = ["2022-09-18", >>> "2022-09-19", >>> "2022-09-20", >>> "2022-09-21", >>> "2022-09-22"], >>> x1 = [1,2,3,4,5], >>> x2 = [6,7,8,9,10] >>> )) >>> wf = WindowFeatures(window = 2) >>> wf.fit_transform(X) date x1 x2 x1_window_2_mean x2_window_2_mean 0 2022-09-18 1 6 NaN NaN 1 2022-09-19 2 7 NaN NaN 2 2022-09-20 3 8 1.5 6.5 3 2022-09-21 4 9 2.5 7.5 4 2022-09-22 5 10 3.5 8.5
Methods
fit:
This transformer does not learn parameters.
transform:
Add window features.
transform_x_y:
Remove rows with missing data from X and y.
fit_transform:
Fit to data, then transform it.
get_feature_names_out:
Get output feature names for transformation.
get_params:
Get parameters for this estimator.
set_params:
Set the parameters of this estimator.
- fit(X, y=None)[source]#
This transformer does not learn parameters.
- Parameters
- X: pandas dataframe of shape = [n_samples, n_features]
The training dataset.
- y: pandas Series, default=None
y is not needed in this transformer. You can pass None or y.
- fit_transform(X, y=None, **fit_params)[source]#
Fit to data, then transform it.
Fits transformer to
X
andy
with optional parametersfit_params
and returns a transformed version ofX
.- Parameters
- Xarray-like of shape (n_samples, n_features)
Input samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None
Target values (None for unsupervised transformations).
- **fit_paramsdict
Additional fit parameters.
- Returns
- X_newndarray array of shape (n_samples, n_features_new)
Transformed array.
- get_feature_names_out(input_features=None)[source]#
Get output feature names for transformation. In other words, returns the variable names of transformed dataframe.
- Parameters
- input_featuresarray or list, default=None
This parameter exits only for compatibility with the Scikit-learn pipeline.
If
None
, thenfeature_names_in_
is used as feature names in.If an array or list, then
input_features
must matchfeature_names_in_
.
- Returns
- feature_names_out: list
Transformed feature names.
- get_metadata_routing()[source]#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns
- routingMetadataRequest
A
MetadataRequest
encapsulating routing information.
- get_params(deep=True)[source]#
Get parameters for this estimator.
- Parameters
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns
- paramsdict
Parameter names mapped to their values.
- set_params(**params)[source]#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters
- **paramsdict
Estimator parameters.
- Returns
- selfestimator instance
Estimator instance.
- transform(X)[source]#
Adds window features.
- Parameters
- X: pandas dataframe of shape = [n_samples, n_features]
The data to transform.
- Returns
- X_new: Pandas dataframe, shape = [n_samples, n_features + window_features]
The dataframe with the original plus the new variables.
- rtype
DataFrame
..
- transform_x_y(X, y)[source]#
Transform, align and adjust both X and y based on the transformations applied to X, ensuring that they correspond to the same set of rows if any were removed from X.
- Parameters
- X: pandas dataframe of shape = [n_samples, n_features]
The dataframe to transform.
- y: pandas Series or Dataframe of length = n_samples
The target variable to transform. Can be multi-output.
- Returns
- X_new: pandas dataframe
The transformed dataframe of shape [n_samples - n_rows, n_features]. It may contain less rows than the original dataset.
- y_new: pandas Series or DataFrame
The transformed target variable of length [n_samples - n_rows]. It contains as many rows as those left in X_new.