Forecasting Website Traffic with Deep Learning: A Comprehensive Guide
Website traffic is the lifeblood of any online business. The ability to accurately predict future traffic is critical for everything from capacity planning to sales forecasting to marketing budget optimization.
In recent years, deep learning has emerged as one of the most powerful techniques for time series forecasting, including web traffic prediction. Deep learning models like Long Short-Term Memory (LSTM) networks and Convolutional Neural Networks (CNNs) have achieved state-of-the-art performance on a variety of forecasting benchmarks.
In this post, we‘ll walk through a complete example of using deep learning to forecast web traffic from historical data. We‘ll cover every step of the process, from data preparation to model building to evaluation. Along the way, we‘ll highlight best practices and tips for getting the most out of deep learning for forecasting.
Whether you‘re a data scientist looking to apply deep learning to your own forecasting problems, or a business stakeholder wanting to understand this increasingly important technology, this post will provide a solid foundation. Let‘s dive in!
Preparing Web Traffic Data for Deep Learning
The first step in any data science project is getting the data into a suitable format for analysis. Time series data like web traffic logs present some unique challenges. Let‘s walk through the key steps.
Handling Missing Data and Outliers
Real-world web traffic data is messy. It‘s common to have periods of missing data, e.g. due to logging errors or outages. There may also be outliers caused by one-off events like product launches or marketing campaigns.
It‘s important to identify and handle these data quality issues before modeling. Some strategies include:
- Filling in missing data points with interpolation or using a rolling average
- Flagging and removing anomalous outliers more than a few standard deviations from the mean
- Transforming data with techniques like log scaling to reduce impact of outliers
The exact approach depends on the nature of your data and business context. The key is to make sure your data is as clean and consistent as possible.
Splitting Data into Training and Test Sets
With time series data, you generally want to split your data chronologically for training and testing. This simulates the real-world scenario of forecasting future values from historical data.
A typical split might use the first 80% of the data for training, and the last 20% for testing. So if you have 2 years of web traffic data, you‘d train on the first 1.6 years and test on the last 5 months.
It‘s important not to randomize the train/test split as you might for other supervised learning problems. The chronological ordering is critical for evaluating time series forecasts.
Normalizing and Scaling
Deep learning models train better when the input data is normalized to a consistent scale, typically between -1 and 1 or 0 and 1. This prevents certain features from dominating the learning process solely due to their raw values.
For web traffic data, a common approach is min-max scaling:
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaled_traffic = scaler.fit_transform(traffic_data)
The MinMaxScaler learns the min and max values of the training data, and scales values proportionally between 0 and 1. Make sure to fit the scaler only on training data to avoid leaking information from the test set!
Converting to Supervised Learning
Most deep learning models require data to be formatted as a standard supervised learning problem, with input X and output y matrices. But time series data is initially a single contiguous sequence.
To convert time series to supervised format, we use a sliding window to create input/output pairs:
def to_supervised(data, window_size=6):
X, y = [], []
for i in range(len(data)-window_size):
X.append(data[i:i+window_size])
y.append(data[i+window_size])
return np.array(X), np.array(y)
X, y = to_supervised(scaled_traffic)
Here we take the last 6 time steps (e.g. hours) of traffic as the input X, and the next time step as the output y to be predicted. The sliding window moves through the data to create many X, y pairs for training.
The size of the input window is an important parameter to tune. Too short and the model may not have enough context to make good forecasts. Too long and it may struggle to learn patterns. A good starting point is to have the input window be 2-3x the typical seasonality of your data (e.g. if you have strong daily cycles, try 2-3 days of input).
With our web traffic data prepared, we‘re ready to start building deep learning models!
Forecasting Web Traffic with LSTMs
Long Short-Term Memory networks, or LSTMs, are a type of recurrent neural network well-suited for sequence data like time series. They have special units called "memory cells" that can learn to store relevant context over long input sequences.
Here‘s an example of building an LSTM model for web traffic forecasting in Keras:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
model = Sequential()
model.add(LSTM(128, input_shape=(6, 1)))
model.add(Dense(1))
model.compile(loss=‘mean_squared_error‘, optimizer=‘adam‘)
This simple architecture has a single LSTM layer with 128 memory units, taking in sequences of the last 6 time steps. The final Dense layer outputs a single value, the forecasted traffic for the next time step.
We can train this model on the prepared data:
history = model.fit(X_train, y_train,
epochs=100, batch_size=32,
validation_data=(X_test, y_test))
After training, we evaluate performance on the test set:
mse = model.evaluate(X_test, y_test)
print(‘Test MSE: %.3f‘ % mse)
Mean squared error (MSE) is a common metric for regression problems like time series forecasting. Lower values indicate better performance.
To make a forecast, we pass in the last 6 time steps of traffic and get back the predicted value for the next step:
# Forecast 12 hours ahead
forecast = []
for _ in range(12):
x = scaled_traffic[-6:]
yhat = model.predict(x.reshape(1, 6, 1)).squeeze()
forecast.append(yhat)
scaled_traffic = np.append(scaled_traffic, yhat)
forecast = scaler.inverse_transform(forecast)
An important consideration when forecasting multiple steps is that we don‘t have actual values beyond the first prediction. So each subsequent step uses the previous prediction as part of its input. Prediction errors can compound over long forecast horizons.
Convolutional Models for Forecasting
Convolutional neural networks (CNNs) are most associated with image data, but they can also be effective for time series. A 1D convolution slides a kernel over the input sequence, learning to extract relevant patterns.
Here‘s a simple CNN model for forecasting:
from tensorflow.keras.layers import Conv1D, GlobalMaxPool1D
model = Sequential()
model.add(Conv1D(128, kernel_size=3, activation=‘relu‘,
input_shape=(6, 1)))
model.add(Conv1D(64, kernel_size=3, activation=‘relu‘))
model.add(GlobalMaxPool1D())
model.add(Dense(1))
model.compile(loss=‘mse‘, optimizer=‘adam‘)
This architecture has two 1D convolutional layers to extract patterns from the input sequence, followed by global max pooling and a final output Dense layer.
In practice, CNNs tend to train faster than LSTMs, while achieving similar performance. Combining convolutional and recurrent layers in a single model can sometimes outperform either approach alone.
Improving Forecast Performance
While deep learning models are powerful, getting the best performance often takes some extra work. Here are some tips for improving web traffic forecasts:
Detecting and Removing Non-Stationarity
Many time series exhibit non-stationary behaviors like trends and seasonality. This means the statistical properties of the data change over time, which can make learning patterns harder.
To test for non-stationarity, you can use statistical tests like the Augmented Dickey-Fuller (ADF) test:
from statsmodels.tsa.stattools import adfuller
def adf_test(series):
result = adfuller(series)
print(‘ADF Statistic: %f‘ % result[0])
print(‘p-value: %f‘ % result[1])
adf_test(traffic_data)
If the p-value is below a critical threshold (e.g. 0.05) then we reject the null hypothesis that the data is non-stationary.
Common techniques for removing non-stationarity include:
- Differencing: subtracting each value from the previous time step
- Detrending: subtracting a rolling mean or fitted trend line
- Seasonal adjustments: removing repeating seasonal patterns
Stationary data can lead to more reliable forecasts, especially over longer horizons.
Ensembling Multiple Models
A powerful technique for improving any machine learning model is ensembling – combining the predictions of multiple models to reduce overall error.
A simple ensembling method is to train multiple models with different architectures or hyperparameters, and average their individual predictions:
lstm_preds = lstm_model.predict(X_test)
cnn_preds = cnn_model.predict(X_test)
naive_preds = naive_forecast(X_test)
ensemble_preds = (lstm_preds + cnn_preds + naive_preds) / 3
Here we‘re averaging the predictions of an LSTM, a CNN, and a naive model (e.g. simply using the last value as the forecast).
More sophisticated ensembling techniques like bagging or boosting can further improve performance.
Hyperparameter Optimization
Deep learning models have many hyperparameters that impact performance – things like number and size of layers, learning rate, batch size, etc. Finding the optimal combination can involve extensive trial and error.
Tools like Keras Tuner or Scikit-Optimize can automate hyperparameter search:
from sklearn.model_selection import RandomizedSearchCV
param_grid = {
‘n_units‘: [32, 64, 128],
‘learning_rate‘: [0.001, 0.01, 0.1],
‘batch_size‘: [16, 32, 64]
}
random_search = RandomizedSearchCV(model, param_grid, cv=3)
random_search.fit(X_train, y_train)
print(random_search.best_params_)
This does a randomized search over different combinations of hyperparameters, evaluating each using 3-fold cross validation. The best parameters can then be used to train a final model.
Bayesian optimization extends this idea by intelligently searching the parameter space to find a global optimum.
Transfer Learning
Many web traffic datasets are limited in size, which can make training deep models difficult. One way around this is to leverage transfer learning – taking a model pretrained on a large dataset, and fine-tuning it for your specific data.
There are a few ways to apply transfer learning to time series:
- Use pretrained NLP models like BERT or GPT to generate embeddings of time series
- Train a model on a large synthetic time series dataset, then fine-tune on real data
- Leverage models trained for classification on large image datasets, and extract features from intermediate layers to use for time series
While research on transfer learning for forecasting is still in early stages, it‘s a promising direction to get more value from limited data.
Conclusion
We‘ve covered a lot of ground in this post, from data preparation to model building to advanced techniques for improving forecast performance. Hopefully you now have a solid foundation for applying deep learning to your own web traffic forecasting problems.
Some key takeaways:
- Proper data preparation is critical, including handling missing values and outliers, normalizing data, and converting to supervised format
- Both LSTMs and 1D CNNs can be effective for time series forecasting, and combining them can be even better
- Detecting and adjusting for non-stationary is important for forecast reliability
- Ensembling, hyperparameter optimization, and transfer learning can significantly improve deep learning performance, especially for small datasets
Of course, we‘ve only scratched the surface of this complex topic. For those wanting to dive deeper, here are some excellent resources:
-
Deep Learning for Time Series Forecasting by Brownlee (https://machinelearningmastery.com/deep-learning-for-time-series-forecasting/)
-
Neural Networks for Time Series Forecasting with R by Kourentzes (https://kourentzes.com/forecasting/2017/02/10/neural-networks-for-time-series-forecasting-with-r/)
-
Greykite Python Package for Time Series Forecasting (https://linkedin.github.io/greykite/)
Armed with the knowledge in this post and these resources, you‘re well on your way to leveraging the power of deep learning for more accurate, automated forecasting. The future is bright!