Predicting Unique Website Visitors With a Tensorflow Time Series Model
This project will use Tensorflow to create a time series model capable of making predictions on unique website visitor data. Data source:
https://www.kaggle.com/bobnau/daily-website-visitors
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2167 entries, 0 to 2166
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 date 2167 non-null datetime64[ns]
1 unique_visits 2167 non-null object
dtypes: datetime64[ns](1), object(1)
memory usage: 34.0+ KB
Let's get the ball rolling with making sure the data is clean and usable. We know we're working with dates, so it's always a good idea to make sure these are in datetime format. We also see commas in the unique_visits numbers, which need to be removed before changing the data type to int from object.
Tensorflow and Keras both love arrays, so it is a good idea to turn the data into np.arrays now, as we will need to do so later on anyways in order to feed data into our network model.
Training and Validation Splits, Finding Baseline
Now we have our data cleaned and in the right format for analysis, let's get the Training and Validation splits set up. I chose to use data up to the 1st of Jan 2019 as training, and data post this date as validation. This is how I landed on index value 1571 as the cut-off position.
Let's have a look at the Naïve Forecast, where we take the previous period and use it to forecast the period ahead. We'll use the resulting Mean Absolute Error as our baseline accuracy to beat.
Baseline accuracy is: 515
Tensorflow Model Design
Setting a checkpoint callback to save the best model weights (lowest MAE):
Epoch 1/100
1551/1551 [==============================] - 46s 25ms/step - loss: 34.7660 - mae: 937.5386
Epoch 2/100
1551/1551 [==============================] - 39s 25ms/step - loss: 17.8047 - mae: 417.2024
Epoch 3/100
1551/1551 [==============================] - 39s 25ms/step - loss: 14.5034 - mae: 342.0269
Epoch 4/100
1551/1551 [==============================] - 39s 25ms/step - loss: 12.1551 - mae: 286.9319
Epoch 5/100
1551/1551 [==============================] - 39s 25ms/step - loss: 12.5660 - mae: 299.0293
Epoch 6/100
1551/1551 [==============================] - 39s 25ms/step - loss: 11.6614 - mae: 287.8062
Epoch 7/100
1551/1551 [==============================] - 39s 25ms/step - loss: 12.0576 - mae: 292.5411
Epoch 8/100
1551/1551 [==============================] - 39s 25ms/step - loss: 10.9524 - mae: 271.2036
Epoch 9/100
1551/1551 [==============================] - 40s 26ms/step - loss: 10.0189 - mae: 251.0590
Epoch 10/100
1551/1551 [==============================] - 64s 41ms/step - loss: 9.8853 - mae: 247.1069
Epoch 11/100
236/1551 [===>..........................] - ETA: 39s - loss: 10.4503 - mae: 236.1027
Execution Error
Predictions
[[4320.231 ]
[4126.498 ]
[3577.1511]
[2711.655 ]
[2030.4172]
[3809.4297]
[4303.554 ]
[4363.565 ]
[4129.9946]
[3448.4712]]
[2361 2793 2373 1609 2046 3100 3182 3245 2836 2476]