mlforecast abstracts away most of the training details, which is useful for iterating quickly. However, sometimes you want more control over the fit parameters, the data that goes into the model, etc. This guide shows how you can train a model in a specific way and then giving it back to mlforecast to produce forecasts with it.

Data setup

from mlforecast.utils import generate_daily_series
series = generate_daily_series(5)

Creating forecast object

import lightgbm as lgb
import numpy as np
from sklearn.linear_model import LinearRegression

from mlforecast import MLForecast

Suppose we want to train a linear regression with the default settings.

fcst = MLForecast(
    models={'lr': LinearRegression()},
    freq='D',
    lags=[1],
    date_features=['dayofweek'],
)

Generate training set

Use MLForecast.preprocess to generate the training data.

prep = fcst.preprocess(series)
prep.head()
unique_iddsylag1dayofweek
1id_02000-01-021.4236260.4289736
2id_02000-01-032.3117821.4236260
3id_02000-01-043.1921912.3117821
4id_02000-01-054.1487673.1921912
5id_02000-01-065.0283564.1487673
X = prep.drop(columns=['unique_id', 'ds', 'y'])
y = prep['y']

Regular training

Since we don’t want to do anything special in our training process for the linear regression, we can just call MLForecast.fit_models

fcst.fit_models(X, y)
MLForecast(models=[lr], freq=D, lag_features=['lag1'], date_features=['dayofweek'], num_threads=1)

This has trained the linear regression model and is now available in the MLForecast.models_ attribute.

fcst.models_
{'lr': LinearRegression()}

Custom training

Now suppose you also want to train a LightGBM model on the same data, but treating the day of the week as a categorical feature and logging the train loss.

model = lgb.LGBMRegressor(n_estimators=100, verbosity=-1)
model.fit(
    X,
    y,
    eval_set=[(X, y)],
    categorical_feature=['dayofweek'],
    callbacks=[lgb.log_evaluation(20)],
);
[20]    training's l2: 0.0823528
[40]    training's l2: 0.0230292
[60]    training's l2: 0.0207829
[80]    training's l2: 0.019675
[100]   training's l2: 0.018778

Computing forecasts

Now we just assign this model to the MLForecast.models_ dictionary. Note that you can assign as many models as you want.

fcst.models_['lgbm'] = model
fcst.models_
{'lr': LinearRegression(), 'lgbm': LGBMRegressor(verbosity=-1)}

And now when calling MLForecast.predict, mlforecast will use those models to compute the forecasts.

fcst.predict(1)
unique_iddslrlgbm
0id_02000-08-103.5491245.166797
1id_12000-04-073.1542854.252490
2id_22000-06-162.8809333.224506
3id_32000-08-304.0618010.245443
4id_42001-01-082.9048722.225106