NeuralProphet: A Neural Network based Time-Series Model

Sandeep S
Analytics Vidhya
Published in
4 min readFeb 9, 2021

NeuralProphet is a python library for modeling time-series data based on neural networks. It’s built on top of PyTorch and is heavily inspired by Facebook Prophet and AR-Net libraries. Time series data is basically the collection of observations obtained through repeated measurements over time. Example — Weather data, Pollution levels data, etc.

In order to understand NeuralProphet we will take a look at a simple example to forecast the temperature of a Kaggle data-set of Rain in Australia using the NeuralProphet package.

The steps are as follows -

  1. Installing and Importing Neural Prophet
  2. Reading and Preprocessing Weather Data
  3. Training a NeuralProphet Model
  4. Generating Forecasts with NeuralProphet
  5. Saving the Model for Future Use

1. Installing and Importing NeuralProphet

To install NeuralProphet on Windows, open the command prompt and enter the following pip command-

pip install neuralprophet

Importing all dependencies -

import pandas as pd 
from neuralprophet import NeuralProphet
from matplotlib import pyplot as plt
import pickle

We import pandas, neuralprophet, matplotlib for data visualization and pickle library to save our trained model for future use.

2. Reading and Preprocessing Weather Data

Now the next thing that we need to do is actually go ahead and import our data and the data-set we’re going to be using is this weatherAUS.csv file. We read it into a data frame using the python library pandas.

df = pd.read_csv('weatherAUS.csv')
df.head()

The df.head() command helps display the first 5 rows of the data-set.

The data-set column values are as follows —

‘Date’, ‘Location’, ‘MinTemp’, ‘MaxTemp’, ‘Rainfall’, ‘Evaporation’,
‘Sunshine’, ‘WindGustDir’, ‘WindGustSpeed’, ‘WindDir9am’, ‘WindDir3pm’,
‘WindSpeed9am’, ‘WindSpeed3pm’, ‘Humidity9am’, ‘Humidity3pm’,
‘Pressure9am’, ‘Pressure3pm’, ‘Cloud9am’, ‘Cloud3pm’, ‘Temp9am’,
‘Temp3pm’, ‘RainToday’, ‘RainTomorrow’

For simplicity, we will choose the location as ‘Melbourne’ and the forecast the temperature of ‘Temp3pm’.

On analyzing the data types of the data frame, we see the date is of type ‘object’, we convert it to ‘datetime’ type for easier processing.

Now we filter out the data-set only for the location of ‘Melbourne’ and convert the data type of ‘Date’ column.

melb = df[df[‘Location’]==’Melbourne’]
melb[‘Date’] = pd.to_datetime(melb[‘Date’])
melb.head()

We can visualize the data using matplotlib and find weather patterns.

plt.plot(melb[‘Date’], melb[‘Temp3pm’])
plt.show()
Date v/s Temp3pm Plot from 2009 to 2015

Whenever we’re passing our data to neuralprophet, it expects only two columns that is — a column called ‘ds’ which stands for dates and it’s expecting another column called ‘y’ which represents the value that we’re trying to predict so we’re going to set our date column to equal ds and set our temp3pm equal to y and then get rid of all the other columns.

data = melb[[‘Date’, ‘Temp3pm’]] 
data.dropna(inplace=True)
data.columns = [‘ds’, ‘y’]
data.head()
Updated data-set for training in NeuralProphet

3. Training a NeuralProphet Model

After all the tedious preprocessing, we get to train the model. We create a new untrained instance of our neuralprophet model and use the fit() method to train the model. The final model will be stored in the variable ‘m’.

m = NeuralProphet()
model = m.fit(data, freq=’D’, epochs=1000)

The fit method takes 3 parameters — the data frame, the frequency of the data i.e. days, and the epochs (number of passes that the algorithm has to complete during training).

On completion of training, we end up with an mean absolute error of 3, which means our predictions will be plus or minus 3 degrees accurate. This totally depends on the size of the data-set and the number of epochs.

Now the model is now ready and trained, we can now forecast the future temperatures.

4. Generating Forecasts with NeuralProphet

We will first make a ‘future’ data frame for a period of 500 days and use the predict method to forecast the future temperatures.

future = m.make_future_dataframe(data, periods=500)
forecast = m.predict(future)
forecast.head()

In the first line, the make_future_dataframe() method takes the parameters-the data, data frame and the period of 500 days, i.e. the temperature for 500 days into the future.

Final forecast data

By changing the period parameter, we can forecast the temperature for a much further future day as we wish.

For better understanding of the results we can plot the results out -

plot1 = m.plot(forecast)
Forecast plot from 2015 to 2017— Dates (x axis) v/s Temperature (y axis)

We generate the plots of the components for detailed analysis -

plt2 = m.plot_components(forecast)

Overall trend shows the temperature is increasing.

This is how you generate time-series forecasts using the NeuralProphet library.

5. Saving the Model for Future Use

For most machine learning programs the trained model is saved using the pickle library to save the trained model for future use.

with open('neuralprophet_model.pkl', "wb") as f:
pickle.dump(m, f)

Here we open a file ‘neuralprophet_model.pkl’ which will contain the saved model, we open it in ‘wb’ (write binary); as f.

Then we dump our model ‘m’ into ‘f’.

Now our model is saved for future use.

References

  1. https://pypi.org/project/neuralprophet/
  2. https://neuralprophet.com/
  3. https://github.com/ourownstory/neural_prophet

Thank you for reading!

--

--