Time Series Tutorial using NeuralProphet

Chirag Samal
Analytics Vidhya
Published in
4 min readFeb 21, 2022
NeuralProphet. Image Credit: NeuralProphet official Repository

In this article, we will build a time series forecasting model using NeuralProphet. NeuralProphet is a neural-network-based Time-Series model, inspired by Facebook Prophet and AR-Net, built on PyTorch. In this article, we will use the AirPassengers Dataset that provides monthly totals of US airline passengers from 1949 to 1960.

Tutorial of this article:

  1. Introduction to NeuralProphet
  2. Advantages of NeuralProphet
  3. Changes from Prophet
  4. Installation of NeuralProphet
  5. Importing Libraries
  6. Import Input Data
  7. Preprocessing of the dataset
  • Renaming Columns.
  • Changing data type.

8. Visualize the dataset

9. Training NeuralProphet Model

  • Visualize the forecast by the model
  • Visualize the components
  • Visualize the parameters
  • Visualize Training and Validation Loss

10. Conclusion

11. References

Introduction to NeuralProphet

NeuralProphet is a PyTorch implementation of a user-friendly time series forecasting tool for practitioners based on Neural Networks. This is heavily influenced by Prophet, a popular Facebook-developed forecasting tool. NeuralProphet is built on a fully modular architecture that allows it to grow in the future as new components are added. This is a user-friendly forecasting tool that maintains the original goals of Prophet, such as interpretability and configurability, while also adding new features, such as automatic differencing capabilities, by using PyTorch as the backend.

Advantages of NeuralProphet

NeuralProphet revolutionizes time series modeling and forecasting:

  • Auto-regression and covariates are supported.
  • Hyperparameters related to training are automatically selected.
  • Fourier term seasonality at various time scales, including yearly, daily, weekly, and hourly.
  • Piecewise linear trend with automatic changepoint detection as an option.
  • Forecast component plotting, model coefficient plotting, and final prediction plotting.
  • Global modeling assistance.
  • Future and lag regressors
  • Regularization reduces coefficient sparsity.
  • Python package that is both user-friendly and powerful.

Changes from NeuralProphet

In comparison to the original Prophet, NeuralProphet has a number of additional features. The following are the details.

  • Optimization using Gradient Descent with PyTorch as the backend.
  • Using AR-Net to model time series autocorrelation.
  • Using a separate Feed-Forward Neural Network to model lagged regressors.
  • FFNNs with configurable non-linear deep layers.
  • Adaptable to different forecast horizons (greater than 1).
  • Losses and metrics that are unique to you.

Installation of NeuralProphet

We can directly install neuralprophet using pip:

Import Libraries

Import Input Data

Summary of the dataset

Summary of the dataset

Preprocessing of the dataset

NeuralProphet expects only two columns by default- a column called ‘ds’, which stands for dates, and another column called ‘y’, which represents the value that we’re trying to predict.

Renaming Columns

Changing data-type

The important thing to note here is that, the ds column must be of the datetime type. But, we can see that it is of object data type. Now, because the ds column is not of the datetime type. So, we’ll need to convert it into datetime type.

Visualize the dataset

Visualize the dataset

Training a NeuralProphet Model

We’ll make a simple model with neural_prophet for this dataset, which can be fitted by creating an object of the NeuralProphet class. Here, the frequency of the dataset is ‘month’. So we will use the change the frequency parameter accordingly. All the parameters are set by default by NeuralProphet. Model validation for NeuralProphet can be done in two ways. Users can split the dataset manually to validate after the model fitting like below by specifying the fraction of data to be used for validation in the argument valid_p. In this article, we will use the first method.

We can make predictions using the fitted model once it has been fitted.

Visualize the forecast by the model

NeuralProphet forecast plot. Image by the author.

Visualize the components

NeuralProphet model components plot. Image by the author.

Visualize the model parameters

NeuralProphet model parameters forecast plot. Image by the author.

Visualize Training and Validation Loss

Model Loss using Mean Absolute Error

Summary

In this article, we discussed NeuralProphet, a Python package that uses Neural Networks to model time series. The library’s backend is PyTorch. In this article, we developed a prediction model using time-series data on Air Passengers. NeuralProphet is built on a fully modular architecture based on AR-Net, making it scalable to incorporate new features in the future.

You can access full code here in this GitHub Repository: NeuralProphet Tutorial

References:

Thanks for reading; I will be writing more similar posts soon. Let’s get involved in discussions, and suggestions are always welcome.

--

--

Chirag Samal
Analytics Vidhya

Computer Vision Engineer @ Zeiss | Former Intern @ Stanford University, IISc | Kaggle Master | IIIT-NR