Data Science project 08
Web Traffic Forecasting Data Science Project
Build a complete web traffic forecasting workflow with reproducible Python code, traceable calculations, validation checks, and honest limitations.
Explore Data Science training in VizagView all project ideas
Analysis question
Can lagged traffic patterns improve next-day estimates over a simple same-weekday baseline?
Dataset
A seeded 500-day synthetic daily visit series with trend, weekly and monthly seasonality, and random noise.
Requirements
- Python 3.10 or later
- A terminal or command prompt
python -m pip install numpy pandas scikit-learn- About 45-60 minutes to build and review
Method and data checks
Create day-of-week, lag-1, lag-7, lag-14, and shifted rolling features, preserve chronological order, train on the first 80%, and evaluate on the final 20%.
- All lag and rolling features use prior observations only
- The split is chronological and never shuffled
- Model MAE is compared with a lag-7 baseline
- Test rows are never used to fit the model
Complete Python code
Save the program as ds_web_traffic_forecasting.py. The dataset generator or loader, analysis functions, output, and reproducibility controls are included.
"""Leakage-aware next-day web traffic forecasting demonstration."""
from __future__ import annotations
import numpy as np
import pandas as pd
from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.metrics import mean_absolute_error
def make_demo_traffic(days: int = 500, seed: int = 42) -> pd.DataFrame:
rng = np.random.default_rng(seed)
x = np.arange(days)
visits = 900 + 1.4 * x + 170 * np.sin(2 * np.pi * x / 7) + 60 * np.sin(2 * np.pi * x / 30) + rng.normal(0, 55, days)
return pd.DataFrame({"date": pd.date_range("2025-01-01", periods=days), "visits": np.maximum(visits.round(), 0)})
def build_features(data: pd.DataFrame) -> pd.DataFrame:
frame = data.copy().sort_values("date")
frame["day_of_week"] = pd.to_datetime(frame["date"]).dt.dayofweek
for lag in (1, 7, 14):
frame[f"lag_{lag}"] = frame["visits"].shift(lag)
frame["rolling_7"] = frame["visits"].shift(1).rolling(7).mean()
return frame.dropna().reset_index(drop=True)
def evaluate_forecast(data: pd.DataFrame) -> dict[str, float]:
frame = build_features(data)
cut = int(len(frame) * 0.8)
train, test = frame.iloc[:cut], frame.iloc[cut:]
features = ["day_of_week", "lag_1", "lag_7", "lag_14", "rolling_7"]
model = HistGradientBoostingRegressor(max_iter=180, learning_rate=0.06, random_state=42)
model.fit(train[features], train["visits"])
prediction = model.predict(test[features])
baseline = test["lag_7"]
return {"model_mae": float(mean_absolute_error(test["visits"], prediction)), "seasonal_baseline_mae": float(mean_absolute_error(test["visits"], baseline)), "test_rows": float(len(test))}
def main() -> None:
metrics = evaluate_forecast(make_demo_traffic())
print({key: round(value, 2) for key, value in metrics.items()})
print("The final 20% is held out in time order; rolling features use prior days only.")
if __name__ == "__main__":
main()
Run the project
- Create and activate a virtual environment.
- Install dependencies with
python -m pip install numpy pandas scikit-learn. - Run
python ds_web_traffic_forecasting.py. - Reconcile row counts and totals before interpreting patterns.
- Read the limitations before substituting any real dataset.
Expected analytical output
Holdout MAE for a histogram gradient-boosting model, seasonal-baseline MAE, and test-row count.
Interpretation and responsible-use limits
Synthetic accuracy does not forecast real traffic. Production forecasts require anomaly handling, campaign calendars, bot definitions, outages, intervals, retraining policy, and ongoing baseline comparison.
Ways to extend the project
Add walk-forward validation, prediction intervals, campaign and holiday features, anomaly flags, drift monitoring, and forecast-versus-actual charts.
Continue learning Data Science
Try the next project, return to the Softenant project library, or explore the Data Science course in Vizag for guided data cleaning, analysis, visualisation, and portfolio feedback.