Machine Learning πŸ€– Forecast

Scalable machine learning for time series forecasting

mlforecast is a framework to perform time series forecasting using machine learning models, with the option to scale to massive amounts of data using remote clusters.

Install

PyPI

pip install mlforecast

conda-forge

conda install -c conda-forge mlforecast

For more detailed instructions you can refer to the installation page.

Quick Start

Get Started with this quick guide.

Follow this end-to-end walkthrough for best practices.

Videos

Sample notebooks

Why?

Current Python alternatives for machine learning models are slow, inaccurate and don’t scale well. So we created a library that can be used to forecast in production environments. MLForecast includes efficient feature engineering to train any machine learning model (with fit and predict methods such as sklearn) to fit millions of time series.

Features

  • Fastest implementations of feature engineering for time series forecasting in Python.
  • Out-of-the-box compatibility with pandas, polars, spark, dask, and ray.
  • Probabilistic Forecasting with Conformal Prediction.
  • Support for exogenous variables and static covariates.
  • Familiar sklearn syntax: .fit and .predict.

Missing something? Please open an issue or write us in

Examples and Guides

πŸ“š End to End Walkthrough: model training, evaluation and selection for multiple time series.

πŸ”Ž Probabilistic Forecasting: use Conformal Prediction to produce prediciton intervals.

πŸ‘©β€πŸ”¬ Cross Validation: robust model’s performance evaluation.

πŸ”Œ Predict Demand Peaks: electricity load forecasting for detecting daily peaks and reducing electric bills.

πŸ“ˆ Transfer Learning: pretrain a model using a set of time series and then predict another one using that pretrained model.

🌑️ Distributed Training: use a Dask, Ray or Spark cluster to train models at scale.

How to use

The following provides a very basic overview, for a more detailed description see the documentation.

Data setup

Store your time series in a pandas dataframe in long format, that is, each row represents an observation for a specific serie and timestamp.

from mlforecast.utils import generate_daily_series

series = generate_daily_series(
    n_series=20,
    max_length=100,
    n_static_features=1,
    static_as_categorical=False,
    with_trend=True
)
series.head()
unique_iddsystatic_0
0id_002000-01-0117.51916772
1id_002000-01-0287.79969572
2id_002000-01-03177.44297572
3id_002000-01-04232.70411072
4id_002000-01-05317.51047472

Note: The unique_id serves as an identifier for each distinct time series in your dataset. If you are using only single time series from your dataset, set this column to a constant value.

Models

Next define your models, each one will be trained on all series. These can be any regressor that follows the scikit-learn API.

import lightgbm as lgb
from sklearn.linear_model import LinearRegression
models = [
    lgb.LGBMRegressor(random_state=0, verbosity=-1),
    LinearRegression(),
]

Forecast object

Now instantiate an MLForecast object with the models and the features that you want to use. The features can be lags, transformations on the lags and date features. You can also define transformations to apply to the target before fitting, which will be restored when predicting.

from mlforecast import MLForecast
from mlforecast.lag_transforms import ExpandingMean, RollingMean
from mlforecast.target_transforms import Differences
fcst = MLForecast(
    models=models,
    freq='D',
    lags=[7, 14],
    lag_transforms={
        1: [ExpandingMean()],
        7: [RollingMean(window_size=28)]
    },
    date_features=['dayofweek'],
    target_transforms=[Differences([1])],
)

Training

To compute the features and train the models call fit on your Forecast object.

fcst.fit(series)
MLForecast(models=[LGBMRegressor, LinearRegression], freq=D, lag_features=['lag7', 'lag14', 'expanding_mean_lag1', 'rolling_mean_lag7_window_size28'], date_features=['dayofweek'], num_threads=1)

Predicting

To get the forecasts for the next n days call predict(n) on the forecast object. This will automatically handle the updates required by the features using a recursive strategy.

predictions = fcst.predict(14)
predictions
unique_iddsLGBMRegressorLinearRegression
0id_002000-04-04299.923771311.432371
1id_002000-04-05365.424147379.466214
2id_002000-04-06432.562441460.234028
3id_002000-04-07495.628000524.278924
4id_002000-04-0860.78622379.828767
……………
275id_192000-03-2336.26678028.333215
276id_192000-03-2444.37098433.368228
277id_192000-03-2550.74622238.613001
278id_192000-03-2658.90652443.447398
279id_192000-03-2763.07394948.666783

Visualize results

from utilsforecast.plotting import plot_series
fig = plot_series(series, predictions, max_ids=4, plot_random=False)

How to contribute

See CONTRIBUTING.md.