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
1. Import Packages and Initialize Client
To start, import the required packages and initialize the Nixtla client with your API key.
Nixtla Client Initialization
import pandas as pdfrom nixtla import NixtlaClientnixtla_client = NixtlaClient( api_key='my_api_key_provided_by_nixtla' # Defaults to os.environ.get("NIXTLA_API_KEY"))
Use an Azure AI endpoint If you are using an Azure AI endpoint, also set the base_url argument:
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:
Calculate MAE
from utilsforecast.losses import maemae_timegpt = mae( df=timegpt_cv_df.drop(columns=['cutoff']), models=['TimeGPT'], target_col='users')mae_timegpt
MAE Result: The MAE obtained is 167.69, outperforming the original pipeline.
Exogenous variables can provide additional context that may improve forecast accuracy. In this example, we add binary indicators for each day of the week.
Add Weekday Indicators
for i in range(7): df[f'week_day_{i + 1}'] = 1 * (df['date'].dt.weekday == i)df.head(10)
We repeat the cross-validation with these new features:
Adding weekday indicators can capture weekly seasonality in user visits.
5
5. Comparing Results
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
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!