Skip to main content
Fit an LSTM and NHITS model
This notebook provides an example on how to start using the main functionalities of the NeuralForecast library. The NeuralForecast class allows users to easily interact with NeuralForecast.models PyTorch models. In this example we will forecast AirPassengers data with a classic LSTM and the recent NHITS models. The full list of available models is available here. You can run these experiments using GPU with Google Colab. Open In Colab

1. Installing NeuralForecast

2. Loading AirPassengers Data

The core.NeuralForecast class contains shared, fit, predict and other methods that take as inputs pandas DataFrames with columns ['unique_id', 'ds', 'y'], where unique_id identifies individual time series from the dataset, ds is the date, and y is the target variable. In this example dataset consists of a set of a single series, but you can easily fit your model to larger datasets in long format.
unique_iddsy
01.01949-01-31112.0
11.01949-02-28118.0
21.01949-03-31132.0
31.01949-04-30129.0
41.01949-05-31121.0
Important DataFrames must include all ['unique_id', 'ds', 'y'] columns. Make sure y column does not have missing or non-numeric values.

3. Model Training

Fit the models

Using the NeuralForecast.fit method you can train a set of models to your dataset. You can define the forecasting horizon (12 in this example), and modify the hyperparameters of the model. For example, for the LSTM we changed the default hidden size for both encoder and decoders.
Tip The performance of Deep Learning models can be very sensitive to the choice of hyperparameters. Tuning the correct hyperparameters is an important step to obtain the best forecasts. The Auto version of these models, AutoLSTM and AutoNHITS, already perform hyperparameter selection automatically.

Predict using the fitted models

Using the NeuralForecast.predict method you can obtain the h forecasts after the training data Y_df.
The NeuralForecast.predict method returns a DataFrame with the forecasts for each unique_id, ds, and model.
unique_iddsLSTMNHITS
01.01961-01-31445.602112447.531281
11.01961-02-28431.253510439.081024
21.01961-03-31456.301270481.924194
31.01961-04-30508.149750501.501343
41.01961-05-31524.903870514.664551

4. Plot Predictions

Finally, we plot the forecasts of both models against the real values.
Tip For this guide we are using a simple LSTM model. More recent models, such as TSMixer, TFT and NHITS achieve better accuracy than LSTM in most settings. The full list of available models is available here.

References