Load libraries
Data
Each timeseries should be stored in a directory named unique_id=timeseries_id. Within this directory, the timeseries can be entirely contained in a single Parquet file or split across multiple Parquet files. Regardless of the format, the timeseries must be ordered by time. For example, the following code splits the AirPassengers DataFrame (of which each timeseries is already sorted by time) into the below format:> data
> unique_id=Airline1
- a59945617fdb40d1bc6caa4aadad881c-0.parquet
> unique_id=Airline2
- a59945617fdb40d1bc6caa4aadad881c-0.parquet
We then simply input a list of the paths to these directories.
unique_id | ds | y | trend | y_[lag12] | |
---|---|---|---|---|---|
0 | Airline1 | 1949-01-31 | 112.0 | 0 | 112.0 |
1 | Airline1 | 1949-02-28 | 118.0 | 1 | 118.0 |
2 | Airline1 | 1949-03-31 | 132.0 | 2 | 132.0 |
3 | Airline1 | 1949-04-30 | 129.0 | 3 | 129.0 |
4 | Airline1 | 1949-05-31 | 121.0 | 4 | 121.0 |
… | … | … | … | … | … |
283 | Airline2 | 1960-08-31 | 906.0 | 283 | 859.0 |
284 | Airline2 | 1960-09-30 | 808.0 | 284 | 763.0 |
285 | Airline2 | 1960-10-31 | 761.0 | 285 | 707.0 |
286 | Airline2 | 1960-11-30 | 690.0 | 286 | 662.0 |
287 | Airline2 | 1960-12-31 | 732.0 | 287 | 705.0 |
id_col | airline1 | airline2 | |
---|---|---|---|
0 | Airline1 | 0 | 1 |
1 | Airline2 | 1 | 0 |
Model training
We now train a NHITS model on the above dataset. It is worth noting that NeuralForecast currently does not support scaling when using this DataLoader. If you want to scale the timeseries this should be done before passing it in to thefit
method.
Forecasting
When working with large datasets, we need to provide a single DataFrame containing the input timesteps of all the timeseries for which wish to generate predictions. If we have future exogenous features, we should also include the future values of these features in the separatefutr_df
DataFrame.
For the below prediction we are assuming we only want to predict the
next 12 timesteps for Airline2.
id_col | ds | NHITS | |
---|---|---|---|
0 | Airline2 | 1960-01-31 | 713.441406 |
1 | Airline2 | 1960-02-29 | 688.176880 |
2 | Airline2 | 1960-03-31 | 763.382935 |
3 | Airline2 | 1960-04-30 | 745.478027 |
4 | Airline2 | 1960-05-31 | 758.036438 |
5 | Airline2 | 1960-06-30 | 806.288574 |
6 | Airline2 | 1960-07-31 | 869.563782 |
7 | Airline2 | 1960-08-31 | 858.105896 |
8 | Airline2 | 1960-09-30 | 803.531555 |
9 | Airline2 | 1960-10-31 | 751.093079 |
10 | Airline2 | 1960-11-30 | 700.435852 |
11 | Airline2 | 1960-12-31 | 746.640259 |
Evaluation
metric | NHITS | |
---|---|---|
0 | mae | 20.728617 |
1 | rmse | 26.980698 |
2 | smape | 0.012879 |