Road accidents in Switzerland forecasting — A brief comparison between Facebook Prophet and LSTM neural networks.

Benoit Figuet
Towards Data Science
6 min readNov 3, 2019

--

For many years, the capacity of predicting the future was reserved to few people and their tools were limited to crystal balls, hand palms and tarot cards. But for the last 50 years, new tools have emerged and forecasting is now accessible to many more people and this is great!

In this article, I will show you how to perform basic timeseries forecasting on a simple example. We will analyze, visualize and forecast road accidents in Switzerland using the open-source library Facebook Prophet and a LSTM neural network using Keras / Tensorflow.

The jupyter notebooks I used for this article are available on my github.

[2011–2018] Accidents location in Switzerland

The Dataset:

For this blog article, we will use the “Strassenverkehrsunfallorte” dataset available at the following address: https://opendata.swiss/en/dataset/strassenverkehrsunfalle-mit-personenschaden

Dataset Description: Anonymised visualization and positioning of road traffic accidents with injury to persons since 2011 until 2018. Information on the year, the month, the weekday, the time of the accident, the type of road, the type of accident, the category of severity and the location is provided for road traffic accidents.

Visualization: Accidents locations

If possible, I always try to start a new data science project with a nice visualization and luckily this one is suitable for that purpose.

Actually, before thinking about forecasting, the reason I was interested into this dataset was it’s potential about being used for generating a “nice” visualisation using Datashader. For those familiar with this library you probably have already seen its use with the New York Taxis dataset or the OpenSky Network one and it’s capacity to handle billions of datapoints to plot.

[2011–2018] Accidents location in Switzerland with colors depending on severity

Accidents Forecasting

While the raw dataset is a timeseries where each row represents an accident, I manipulated it so that we have now a timeseries which counts the number of accident per Hours and this is what we will try to forecast.

Timeseries forecasting:

When it comes to forecast timeseries, there are popular methods that come to my mind:

  • Simple baseline: averaging, smoothing …
  • Autoregression: SARIMA models (Seasonal Autoregressive Integrated Moving Average Models)
  • Neural Networks / Deep learning: CNN, LSTM

Almost all those methods need a bit of effort to tune them and find the best parameters. The cool thing is that Facebook engineers (probably tired of using new models from scratch) came out with an open source library called Prophet: “Prophet is a procedure for forecasting time series data based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects. It works best with time series that have strong seasonal effects and several seasons of historical data. Prophet is robust to missing data and shifts in the trend, and typically handles outliers well.”

Dataset manipulations:

The goal here is not to show all the capabilities of Prophet or LSTM neural networks but more to manipulate a simple dataset and show you how you can shape it to predict the future 😮

Our original dataset contains one row per registered accident, we will just manipulate it in order to count the number of accidents happening into a delta time (dt) of your choice. We will obtain a dataset that is still a timeseries but now has only one row per ‘timestamp’ at a chosen frequency and a column ‘n_accident’ that contains the number of accident that have been registered during the chosen timespan.

Facebook Prophet:

Accident seasonality with Prophet:

Once the new dataset obtained, it is very easy to obtain a prediction as well as a seasonality analysis using Prophet.

Here is the type of seasonality plot you can obtain:

Seasonality of number of accidents per hours

On the y-axis, you have the number of accidents per timestamps (resolution of 1H).

As we can see, it looks like the number of accidents decreased quite a bit between 2011 and 2013 while it’s increasing again since 2016.

Other information we can extract from this plot is that the month of June seems to be the most critical one in terms of accidents as well as the 16–18H time span during the day.

Accident forecasting with Prophet:

As we have data from 2011 until 2018, we will only use the period 2011 -> 2017 to predict 2018 and then be able to assess the accuracy of the prediction. And instead of trying to predict the number of accidents per hour, we transform our dataset to predict the number of accidents per day.

Daily accidents number forecast for 2018

Difficult to assess the accuracy by just looking at a plot, therefore we need to have a look at some metrics into the next part of this article.

Long Short Term Memory Neural Network

Accident forecasting using deep learning and LSTM Neural Network:

LSTM stands for Long Short Term Memory. It is a type of recurrent neural network and the power of the LSTM is that it has kind of a memory and is able to memorize/forget patterns.

We will use the last 1000 days to predict the 365 next ones. To do so, we train the model on the data from 2011 till end of 2017 and then we try to predict the accidents in Switzerland for the year 2018.

We use a walk forward model. Imagine we want to predict the next element of the following sequence using the 3 last elements [1, 2, 3, 4, 5, 6]. I can design a training dataset of multiple rows such as:

[1, 2, 3] -> 4
[2, 3, 4] -> 5
[3, 4, 5] -> 6

To do so I used this very useful article: https://machinelearningmastery.com/convert-time-series-supervised-learning-problem-python/

The Neural Netowrk Architecture:

I used keras to model the neural network, we have a layer of 50 LSTM cells (I used a CuDNNLSTM layer, a fast implementation of the LSTM that takes full advantage of your GPU) and a dense layer where the number of neurons is equal to the number of predictions we want to do.

Neural network architecture using keras

The training and the results:

We use 33% of the data as validation and a ModelCheckPoint callback on the val_loss to avoid overfitting of the network on the training data.

2018 daily accidents forecast using LSTM

As we can see on the plot above, it catches the trend but nothing more, the oscillations we see are not really cathing any interesting part of the data. I am sure we could get better results by fine tuning the LSTM hyperparameters but that is not the purpose of this article.

Facebook Prophet vs. LSTM prediction accuracy

In order to judge the accuracy of our predictions, we will check how both model are performing versus different metrics. I have chosen 3 different metrics: the Root Mean Square Error [RMSE], the Mean Absolute Error [MAE] and the Mean Percentage Error [MPE].

2018 forecasting metrics

As we can see, prophet performs better for each one of the 3 metrics. This demonstrates the power of this tool. For 3 times less effort, we obtained a better prediction than with a LSTM neural network. Next time you want to forecast some timeseries, you might want to have a try at Prophet first.

But don’t get me wrong, LSTM are very powerful when you want to forecast using multiple features or more complex data and might be THE “tool” to be considered depending on your application of course.

--

--