Model training, evaluation and selection for multiple time series
Prerequesites This Guide assumes basic familiarity with NeuralForecast. For a minimal example visit the Quick StartFollow this article for a step to step guide on building a production-ready forecasting pipeline for multiple time series. During this guide you will gain familiarity with the core
NeuralForecast
class
and some relevant methods like
NeuralForecast.fit
,
NeuralForecast.predict
,
and StatsForecast.cross_validation.
We will use a classical benchmarking dataset from the M4 competition.
The dataset includes time series from different domains like finance,
economy and sales. In this example, we will use a subset of the Hourly
dataset.
We will model each time series globally Therefore, you will train a set
of models for the whole dataset, and then select the best model for each
individual time series. NeuralForecast focuses on speed, simplicity, and
scalability, which makes it ideal for this task.
Outline:
Not Covered in this guide
- Using external regressors or exogenous variables
- Follow this tutorial to include exogenous variables like weather or holidays or static variables like category or family.
- Probabilistic forecasting
- Follow this tutorial to generate probabilistic forecasts
- Transfer Learning
- Train a model and use it to forecast on different data using this tutorial
Tip You can use Colab to run this Notebook interactively![]()
Warning
To reduce the computation time, it is recommended to use GPU. Using
Colab, do not forget to activate it. Just go to
Runtime>Change runtime type
and select GPU as hardware accelerator.
NeuralForecast
already installed. Check this guide for instructions on how to install
NeuralForecast.
.csv
.
The input to
NeuralForecast
is always a data frame in long
format with
three columns: unique_id
, ds
and y
:
unique_id
(string, int or category) represents an identifier
for the series.
ds
(datestamp or int) column should be either an integer
indexing time or a datestampe ideally like YYYY-MM-DD for a date or
YYYY-MM-DD HH:MM:SS for a timestamp.
y
(numeric) represents the measurement we wish to forecast. We
will rename the
unique_id | ds | y | |
---|---|---|---|
0 | H1 | 1 | 605.0 |
1 | H1 | 2 | 586.0 |
2 | H1 | 3 | 586.0 |
3 | H1 | 4 | 559.0 |
4 | H1 | 5 | 511.0 |
Note Processing time is dependent on the available computing resources. Running this example with the complete dataset takes around 10 minutes in a c5d.24xlarge (96 cores) instance from AWS.
plot_series
function from the
utilsforecast
library. This method prints 8 random series from the
dataset and is useful for basic EDA.
Note Theplot_series
function uses matplotlib as a default engine. You can change to plotly by settingengine="plotly"
.
NeuralForecast
can train many models on many time series globally and efficiently.
Auto
model contains a default search space that was extensively
tested on multiple large-scale datasets. Additionally, users can define
specific search spaces tailored for particular datasets and tasks.
First, we create a custom search space for the
AutoNHITS
and
AutoLSTM
models. Search spaces are specified with dictionaries, where keys
corresponds to the model’s hyperparameter and the value is a Tune
function to specify how the hyperparameter will be sampled. For example,
use randint
to sample integers uniformly, and choice
to sample
values of a list.
Auto
model you need to define:
h
: forecasting horizon.loss
: training and validation loss from
neuralforecast.losses.pytorch
.config
: hyperparameter search space. If None
, the Auto
class
will use a pre-defined suggested hyperparameter space.search_alg
: search algorithmnum_samples
: number of configurations explored.h
as 48, use the
MQLoss
distribution loss for training and validation, and use the default
search algorithm.
Tip The number of samples,Next, we use thenum_samples
, is a crucial parameter! Larger values will usually produce better results as we explore more configurations in the search space, but it will increase training times. Larger search spaces will usually require more samples. As a general rule, we recommend settingnum_samples
higher than 20.
Neuralforecast
class to train the Auto
model. In
this step, Auto
models will automatically perform hyperparameter
tuning training multiple models with different hyperparameters,
producing the forecasts on the validation set, and evaluating them. The
best configuration is selected based on the error on a validation set.
Only the best model is stored and used during inference.
predict
method to forecast the next 48 days using the
optimal hyperparameters.
plot_series
function allows for further customization. For
example, plot the results of the different models and unique ids.
Tip
Setting n_windows=1
mirrors a traditional train-test split with our
historical data serving as the training set and the last 48 hours
serving as the testing set.
The cross_validation
method from the
NeuralForecast
class takes the following arguments.
df
: training data frame
step_size
(int): step size between each window. In other words:
how often do you want to run the forecasting processes.
n_windows
(int): number of windows used for cross validation. In
other words: what number of forecasting processes in the past do you
want to evaluate.
cv_df
object is a new data frame that includes the following
columns:
unique_id
: identifies each time seriesds
: datestamp or temporal indexcutoff
: the last datestamp or temporal index for the n_windows. If
n_windows=1, then one unique cuttoff value, if n_windows=2 then two
unique cutoff values.y
: true value"model"
: columns with the model’s name and fitted value.unique_id | ds | cutoff | AutoNHITS | AutoNHITS-lo-90 | AutoNHITS-lo-80 | AutoNHITS-hi-80 | AutoNHITS-hi-90 | AutoLSTM | AutoLSTM-lo-90 | AutoLSTM-lo-80 | AutoLSTM-hi-80 | AutoLSTM-hi-90 | y | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | H1 | 700 | 699 | 654.506348 | 615.993774 | 616.021851 | 693.879272 | 712.376587 | 777.396362 | 511.052124 | 585.006470 | 992.880249 | 1084.980957 | 684.0 |
1 | H1 | 701 | 699 | 619.320068 | 573.836060 | 577.762695 | 663.133301 | 683.214478 | 691.002991 | 417.614349 | 488.192810 | 905.101135 | 1002.091919 | 619.0 |
2 | H1 | 702 | 699 | 546.807922 | 486.383362 | 498.541748 | 599.284302 | 623.889038 | 569.914795 | 314.173462 | 389.398865 | 763.250244 | 852.974121 | 565.0 |
3 | H1 | 703 | 699 | 483.149811 | 420.416351 | 435.613708 | 536.380005 | 561.349487 | 548.401917 | 305.305054 | 379.597839 | 732.263123 | 817.543152 | 532.0 |
4 | H1 | 704 | 699 | 434.347931 | 381.605713 | 394.665619 | 481.329041 | 501.715546 | 511.798950 | 269.810272 | 346.146484 | 692.443542 | 776.531921 | 495.0 |
Warning You can also use Mean Average Percentage Error (MAPE), however for granular forecasts, MAPE values are extremely hard to judge and not useful to assess forecasting quality.Create the data frame with the results of the evaluation of your cross-validation data frame using a Mean Squared Error metric.
unique_id | metric | AutoNHITS | AutoLSTM | best_model | |
---|---|---|---|---|---|
0 | H1 | mse | 2295.630068 | 1889.340182 | AutoLSTM |
1 | H10 | mse | 724.468906 | 362.463659 | AutoLSTM |
2 | H100 | mse | 62943.031250 | 17063.347107 | AutoLSTM |
3 | H101 | mse | 48771.973540 | 12213.554997 | AutoLSTM |
4 | H102 | mse | 30671.342050 | 84569.434859 | AutoNHITS |
metric | model | nr. of unique_ids | |
---|---|---|---|
0 | mae | AutoNHITS | 3 |
1 | mse | AutoNHITS | 4 |
2 | rmse | AutoNHITS | 4 |
3 | mse | AutoLSTM | 6 |
4 | rmse | AutoLSTM | 6 |
5 | mae | AutoLSTM | 7 |
metric | model | nr. of unique_ids | |
---|---|---|---|
1 | mse | AutoNHITS | 4 |
3 | mse | AutoLSTM | 6 |
unique_id | ds | best_model | best_model-lo-90 | best_model-hi-90 | |
---|---|---|---|---|---|
0 | H1 | 749 | 603.923767 | 437.270447 | 786.502686 |
1 | H1 | 750 | 533.691284 | 383.289154 | 702.944397 |
2 | H1 | 751 | 490.400085 | 349.417816 | 648.831299 |
3 | H1 | 752 | 463.768066 | 327.452026 | 616.572144 |
4 | H1 | 753 | 454.710266 | 320.023468 | 605.468018 |
… | … | … | … | … | … |
475 | H107 | 792 | 4720.256348 | 4142.459961 | 5235.727051 |
476 | H107 | 793 | 4394.605469 | 3952.059082 | 4992.124023 |
477 | H107 | 794 | 4161.221191 | 3664.091553 | 4632.160645 |
478 | H107 | 795 | 3945.432617 | 3453.011963 | 4437.968750 |
479 | H107 | 796 | 3666.446045 | 3177.937744 | 4059.684570 |