Skip to main content

mlforecast

CI Python PyPi conda-forge License

Install

PyPI

pip install mlforecast

If you want to perform distributed training, you can instead use pip install mlforecast[distributed], which will also install dask. Note that you’ll also need to install either LightGBM or XGBoost.

conda-forge

conda install -c conda-forge mlforecast

Note that this installation comes with the required dependencies for the local interface. If you want to perform distributed training, you must install dask (conda install -c conda-forge dask) and either LightGBM or XGBoost.

How to use

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

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()
ds y static_0
unique_id
id_00 2000-01-01 1.751917 72
id_00 2000-01-02 9.196715 72
id_00 2000-01-03 18.577788 72
id_00 2000-01-04 24.520646 72
id_00 2000-01-05 33.418028 72

Next define your models. If you want to use the local interface this can be any regressor that follows the scikit-learn API. For distributed training there are LGBMForecast and XGBForecast.

import lightgbm as lgb
import xgboost as xgb
from sklearn.ensemble import RandomForestRegressor

models = [
    lgb.LGBMRegressor(),
    xgb.XGBRegressor(),
    RandomForestRegressor(random_state=0),
]

Now instantiate a Forecast object with the models and the features that you want to use. The features can be lags, transformations on the lags and date features. The lag transformations are defined as numba jitted functions that transform an array, if they have additional arguments you supply a tuple (transform_func, arg1, arg2, …).

from mlforecast import Forecast
from window_ops.expanding import expanding_mean
from window_ops.rolling import rolling_mean

fcst = Forecast(
    models=models,
    freq='D',
    lags=[7, 14],
    lag_transforms={
        1: [expanding_mean],
        7: [(rolling_mean, 7)]
    },
    date_features=['dayofweek'],
    differences=[1],
)

To compute the features and train the models call fit on your Forecast object. Here you have to specify the columns that:

  • Identify each serie (id_col). If the series identifier is the index you can specify id_col='index'
  • Contain the timestamps (time_col). Can also be integers if your data doesn’t have timestamps.
  • Are the series values (target_col)
fcst.fit(series, id_col='index', time_col='ds', target_col='y', static_features=['static_0'])
Forecast(models=[LGBMRegressor, XGBRegressor, RandomForestRegressor], freq=<Day>, lag_features=['lag-7', 'lag-14', 'expanding_mean_lag-1', 'rolling_mean_lag-7_window_size-7'], date_features=['dayofweek'], num_threads=1)

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

predictions = fcst.predict(14)
import matplotlib.pyplot as plt
import pandas as pd

fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(12, 6), gridspec_kw=dict(hspace=0.3))
for i, (cat, axi) in enumerate(zip(series.index.categories, ax.flat)):
    pd.concat([series.loc[cat, ['ds', 'y']], predictions.loc[cat]]).set_index('ds').plot(ax=axi)
    axi.set(title=cat, xlabel=None)
    if i % 2 == 0:
        axi.legend().remove()
    else:
        axi.legend(bbox_to_anchor=(1.01, 1.0))
fig.savefig('figs/index.png', bbox_inches='tight')
plt.close()

Metadata

Release files for mlforecast 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mlforecast 0.3.1
File Size Uploaded
mlforecast-0.3.1.tar.gz 29.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mlforecast 0.3.1
File Interpreter ABI Platform
mlforecast-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 62.0 kB

Release files / mlforecast-0.3.1.tar.gz

Download URL mlforecast-0.3.1.tar.gz
Size 29.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4945a417a930c8a495dd21153917a7b2b711835198a3310b7c11973f68401ba1
BLAKE2b-256 checksum
How to use checksums
59bacae3f34eec8faecc92128a63c6dd3048e2e9314a7b0807fb9c7ed9502bf9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.12

Release files / mlforecast-0.3.1-py3-none-any.whl

Download URL mlforecast-0.3.1-py3-none-any.whl
Size 32.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f10b370b571d459b0da748a405d1fea7bdc73120dbe72f875fc2039a3ad16451
BLAKE2b-256 checksum
How to use checksums
ebb274642f4947f1fe38bd228f440eabe70a2e056a1371d133f3056c163bb264
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.12

Release history Release notifications | RSS feed

1.1.0

2 release files

1.0.31

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.15.1

2 release files

0.15.0

2 release files

0.14.0

2 release files

0.13.5

2 release files

0.13.4

2 release files

0.13.3

2 release files

0.13.2

2 release files

0.11.8

2 release files

0.11.7

2 release files

0.11.6

2 release files

0.11.3

2 release files

0.11.1

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page