Time Series Forecasting with Recurrent Neural Network (RNN)
by Haydar Özler and Tankut Tekeli
github repository: https://github.com/haydarozler/bike-sharing-prediction-with-RNN
- Purpose: to practice solving a timeseries problem by using Recurrent Neural Network
- Data: Bike Sharing in Washington D.C. Dataset
- Applied Tools & Methods: TimeSeriesGenerator, SimpleRNN
- Result: Around %80 correctness (calculated as 1-mae/mean)
- Further Studies: More advanced sequential methods like LSTM and GRU can be applied.
Explanation of the Study
We have created a model to predict how many bicycles will be rented in the following days. The features used like weather, temperature, working day are explained in the following sections in detail.
We have used SimpleRNN method in Keras library. It is one of the sequential models. The others are LSTM and GRU.
Sequential models have 3 dimension (sample size, time steps, features). Preparing 3D input is another challenge. Instead of trying to create a 3D array, we use TimeSeriesGenerator class which brings some other advantages like setting the batch size.
We skipped feature engineering and visualization parts because main purpose was to practice a sequential neural network. It is possible to have better achivements by applying these methods and then create a predictive model.
Data is 2 years daily data. Number of samples is 731. We have splitted it into 631, 50, 50 as train, test and hold-out data respectively.
We have measured the performance of the model with ( 1 — (mean average error) / (mean) ) and we have reached values around %80.
There are so many further studies: More feature engineering for better accuracy and trying other sequential models.
Reading the dataset
Daily data has the following fields. Thanks to the people who prepared it because it is very well processed data with even scaled features.
instant: Record index
season: Season (1:springer, 2:summer, 3:fall, 4:winter)
yr: Year (0: 2011, 1:2012)
mnth: Month (1 to 12)
holiday: weather day is holiday or not (extracted from Holiday Schedule)
weekday: Day of the week
workingday: If day is neither weekend nor holiday is 1, otherwise is 0.
weathersit: (extracted from Freemeteo)
1: Clear, Few clouds, Partly cloudy, Partly cloudy
2: Mist + Cloudy, Mist + Broken clouds, Mist + Few clouds, Mist
3: Light Snow, Light Rain + Thunderstorm + Scattered clouds, Light Rain + Scattered clouds
4: Heavy Rain + Ice Pallets + Thunderstorm + Mist, Snow + Fog
temp: Normalized temperature in Celsius. The values are derived via (t-t_min)/(t_max-t_min), t_min=-8, t_max=+39 (only in hourly scale)
atemp: Normalized feeling temperature in Celsius. The values are derived via (t-t_min)/(t_max-t_min), t_min=-16, t_max=+50 (only in hourly scale)
hum: Normalized humidity. The values are divided to 100 (max)
windspeed: Normalized wind speed. The values are divided to 67 (max)
casual: count of casual users
registered: count of registered users
cnt: count of total rental bikes including both casual and registered
Plot of 2 years number of sharing (cnt)
Data Exploration and Manipulation
- Number of bike sharing is 22 only at 2012–10–29 and such a low value deserves a special attention.
- There was a hurricane at Washington at that day.
- Since it is such an extraordinary day, hurricane and the following days data will be replaced by the average of that month.
One Hot Encoding
We should apply one hot encoding for categorical features. In our case weekday, weathersit and mnth features are one hot encoded.
Thanks to the guys prepared the original data, they scaled all features. That is why we have to apply it only for our value Y which is cnt. It is also a discussion whether Y value should be scaled or not in sucha model but we did.
Preparing 3-Dimensional Input for Sequential Model
The following steps show the way how to prepare input for a sequential model by using TimeSeriesGenerator.
Creating the SimpleRNN Model
Training the Model
Plot of Training and Test Loss Functions
Predictions for Test Data
Predicting for Test Data
Tabulating Actuals, Predictions and Differences
Calculating the Correctness for Test Data
Plot of Actuals and Predictions for Test Data
Predictions for Hold-Out Data
Predicting for Hold-Out Data
Tabulating Actuals, Predictions and Differences for Hold-Out Data
Calculating the Correctness for Hold-Out Data
Plot of Actuals and Predictions for Hold-Out Data