Forecasting Web Traffic
Learn how to predict website traffic patterns using TimeGPT.
Goal: Forecast the next 7 days of daily visits to the website cienciadedatos.net using TimeGPT.
This tutorial is adapted from “Forecasting web traffic with machine learning and Python” by Joaquín Amat Rodrigo and Javier Escobar Ortiz. You will learn how to:
Improve Accuracy
Obtain forecasts nearly 10% more accurate than the original method.
Reduce Complexity
Use significantly fewer lines of code and simpler workflows.
Save Time
Generate forecasts in substantially less computation time.
1. Import Packages and Initialize Client
To start, import the required packages and initialize the Nixtla client with your API key.
Use an Azure AI endpoint
If you are using an Azure AI endpoint, also set the base_url
argument:
2. Load Data
We will load the website visit data directly from a CSV file. Then, we format the dataset by adding an identifier column named daily_visits
.
Note: No further preprocessing is required before we start forecasting.
3. Cross-Validation with TimeGPT
We will set up a rolling window cross-validation using TimeGPT. This will help us evaluate the forecast accuracy across multiple historic windows.
Cross-validation forecast plot
The results align closely with those from the original tutorial on forecasting web traffic with machine learning.
Next, we compute the Mean Absolute Error (MAE) to quantify forecast accuracy:
MAE Result: The MAE obtained is 167.69
, outperforming the original pipeline.
4. Adding Exogenous Variables (Weekday Indicators)
Exogenous variables can provide additional context that may improve forecast accuracy. In this example, we add binary indicators for each day of the week.
We repeat the cross-validation with these new features:
Forecast with Exogenous Variables
Adding weekday indicators can capture weekly seasonality in user visits.
5. Comparing Results
Model | Exogenous features | MAE Backtest |
---|---|---|
TimeGPT | No | 167.6917 |
TimeGPT | Yes | 167.2286 |
We see a slight improvement in MAE by including the weekday indicators. This illustrates how TimeGPT can incorporate additional signals without complex data processing or extensive model tuning.
6. Final Thoughts
Key Takeaways
- TimeGPT simplifies forecasting workflows by reducing code and tuning overhead.
- Feature engineering (like adding weekday variables) further boosts accuracy.
- Cross-validation provides a robust way to evaluate model performance.
We have demonstrated significant improvements in forecasting accuracy with minimal effort using TimeGPT. This avoids the majority of the complex steps required when building custom models—such as extensive feature engineering, validation, model comparisons, and hyperparameter tuning.
Good luck and happy forecasting!