Compute features based on lagsmlforecast allows you to define transformations on the lags to use as features. These are provided through the
lag_transforms argument,
which is a dict where the keys are the lags and the values are a list of
transformations to apply to that lag.
Data setup
Built-in transformations
The built-in lag transformations are in themlforecast.lag_transforms
module.
MLForecast.preprocess.
Extending the built-in transformations
You can compose the built-in transformations by using theCombine
class, which takes two transformations and an operator.
Combine to be applied to a
different lag you can use the Offset class, which will apply the
offset first and then the transformation.
Cross-series features: global_, groupby, and partition_by
Every built-in rolling, expanding, seasonal-rolling, and exponentially
weighted transform also accepts global_=True (compute across all
series), groupby=["col", ...] (compute within a static-feature group),
or partition_by=["col", ...] (split further along a dynamic column
that can vary over time, like promo or regime). This is useful when
a single series has too little history to support per-series rolling
statistics, or when cross-series aggregates carry more signal than any
single series’ lag.
These pooled transforms use SQL RANGE BETWEEN ... PRECEDING
semantics over actual timestamps, so they handle staggered series starts
correctly and treat min_samples as a coverage threshold across the
bucket. partition_by extends this to time-varying keys and is supplied
via X_df at prediction time. See the Pooled lag
transforms guide for a full walkthrough.
numba-based transformations
The window-ops package provides transformations defined as numba JIT compiled functions. We use numba because it makes them really fast and can also bypass python’s GIL, which allows running them concurrently with multithreading. The main benefit of using these transformations is that they’re very easy to implement. However, when we need to update their values on the predict step they can very slow, because we have to call the function again on the complete history and just keep the last value, so if performance is a concern you should try to use the built-in ones or setkeep_last_n in MLForecast.preprocess or MLForecast.fit to the
minimum number of samples that your transformations require.
(func, arg1, arg2, ...)
As you can see the name of the function is used as the transformation
name plus the
_lag suffix. If the function has other arguments and
they’re not set to their default values they’re included as well, as is
done with offset=2 here.

