How to use Keras TimeseriesGenerator for time series data



You might have dealt with a predictive model whose task is to predict a future value based on historical data. It is tedious to prepare the input and output pairs given the time series data. I recently come across the Keras built-in utility TimeseriesGenerator which precisely does what I want.

In the following demo, you will learn how to apply it to your dataset.

The source code is available on my GitHub repository.

Imagining you are a fund manager with an acute data science awareness who wants to predict today’s Dow Jones Index given publicly available stock prices.

DJI 2015/09/19–2018/09/18

Instead of using the absolute DJI index value which has increased by 60% during past few years, we will use the day change value as the time-series data instead. As dataset_DJI representing the absolute DJI index, the day change values can be computed by

dataset = dataset_DJI[1:]- dataset_DJI[:-1]

We can further normalize all values and split them into the train/test datasets.

Single time-series prediction

You are aware of the RNN, or more precisely LSTM network captures time-series patterns, we can build such a model with the input being the past three days’ change values, and the output being the current day’s change value. The number three is the look back length which can be tuned for different datasets and tasks. Put it simply, Day T’s value is predicted by day T-3, T-2, and T-1’s. But how can we construct the training and testing input/output pairs for the model? Keras’ TimeseriesGenerator makes our life easier by eliminating the boilerplate code we used to use to complete this step.

Let’s build two time-series generators one for training and one for testing. We use a sampling rate as one as we don’t want to skip any samples in the datasets.

After a simple Keras model comes into place, we can fire up the training process.

After training, evaluation and the final prediction can also be done like this.

From there, we can reconstruct the predicted DJI absolute values from the predicted day change values by first reverse the min-max normalization process and adds the predicted day change value to the previous day’s absolute value.

Reconstruct the predicted DJI

Multiple time-series as input

You might have noticed all previous TimeseriesGenerator’s “data”, and “targets” arguments are the same which means inputs and outputs all came from the same time-series. What if in real life today’s DJI close price might be affected by the previous stock prices of some big firms like Apple and Amazon? We want to incorporate those stocks day change values into the model input as well. To accomplish this, you can first concatenate all three time-series to make a (T, 3) shaped numpy array then pass the preprocessed result to the “data” argument of TimeseriesGenerator.

Finally, be sure to change the model input shape to match the input shape of (None, look_back, 3).

Conclusion

This quick tutorial shows you how to use Keras’ TimeseriesGenerator to alleviate work when dealing with time series prediction tasks. It allows you to apply the same or different time-series as input and output to train a model. The source code is available on my GitHub repository.


Originally published at www.dlology.com.

Source: Deep Learning on Medium