Sales Forecasting with Python Machine Learning

Portfolio

Sales forecasting model comparison generated by a Python machine learning pipeline

I built a reusable sales forecasting pipeline in Python for business teams that need repeatable forecasts.

The project turns notebook-style analysis into a CLI workflow for data validation, feature engineering, five-model training, model comparison, future forecasting, and report generation.

The pipeline checks irregular data before training, keeps forecast runs reproducible, and uses advertising activity as an external driver.

View GitHub Repository

Project Background

Sales forecasts in many teams still depend on Excel moving averages or one person's judgment.

That process struggles to include marketing activity such as TV commercials, digital ads, and campaign spend.

Analysts often leave the code inside a Jupyter Notebook, so other team members cannot reproduce the result or connect it to a production workflow.

I modularized the workflow from data validation to report generation and packaged it as a pipeline that runs the same way each time.

Core Pipeline Stages

The forecasting system has five stages.

1. Input Data Validation (validate-data)

This stage checks the weekly sales CSV file before model training begins.

It validates required columns, duplicate dates, missing weeks, numerical ranges, and advertising data for the forecast period.

The validation step catches malformed input data before the training and forecasting commands run.

2. Automated Feature Engineering

This stage creates the lag features, moving averages, and calendar features needed for time-series forecasting.

It adds advertising spend and campaign variables across TV commercial GRP, digital ads, print media, and offline activity.

The feature generation process preserves time order and keeps future information out of historical training features.

3. Unified Model Training (train)

A common interface handles five forecasting models with different approaches.

One command trains a baseline model, statistical time-series models, and machine learning models such as LightGBM.

4. Future Forecasting (forecast)

The trained models forecast a 12-week sales horizon and write the results to a CSV file.

You can pass planned advertising budgets and other external variables to test campaign scenarios.

5. Automated Report Generation with Visualizations (report)

The reporting stage uses each model's evaluation metrics and prediction outputs to create date-stamped Markdown reports and charts.

The charts show non-technical stakeholders which model performed best and how the forecast moves over the next 12 weeks.

The Five Evaluated Forecasting Models

The pipeline includes five algorithms with different strengths.

  • baseline: A previous-period or moving average model that gives advanced models a minimum benchmark to beat.
  • linear_regression: A regression model that uses lag features and advertising variables, so you can inspect how each factor contributes to sales.
  • lightgbm: A gradient boosting model that learns non-linear advertising effects and interactions between variables.
  • prophet: A time-series model from Meta that separates trend changes, weekly and yearly seasonality, and holiday effects.
  • sarimax: A traditional statistical time-series model that combines autocorrelation, seasonal patterns, and external advertising variables in a single mathematical framework.

Model Performance & Evaluation

The pipeline evaluates forecasting accuracy with cross-validation that preserves the chronological order of the time-series data.

It calculates RMSE (Root Mean Squared Error), MAE (Mean Absolute Error), and MAPE (Mean Absolute Percentage Error), then ranks the models by those metrics.

The chart below shows the accuracy ranking created by the pipeline's report command.

Accuracy ranking comparison across five sales forecasting models

The reports show how each model behaves, including LightGBM's variable interactions and Prophet's seasonal patterns.

Actual vs. Predicted Comparison in the Test Period

The chart below compares actual sales with SARIMAX predictions during the test period.

SARIMAX actual versus predicted sales comparison during the test period

The predicted values follow the large peaks and troughs in the test data.

12-Week Future Sales Forecast

This chart shows the SARIMAX forecast after the final week of observed sales data.

SARIMAX 12-week future sales forecast visualization

The forecast extends sales 12 weeks into the future and keeps the recent trend and seasonal movement visible.

Generated Reports (GitHub)

Running the pipeline's report command creates an overall comparison summary and detailed model-level analysis reports in Markdown format.

It removes the manual work of collecting metrics, pasting charts, and assembling stakeholder-facing documents.

You can view the generated reports in the GitHub repository below.

Business Impact & Real-World Application

This pipeline can reduce sales forecasting and reporting work from days of manual work to one command.

Business leaders can pass future advertising plans as external variables, compare scenarios such as higher TV commercial exposure and lower digital ad spend, then use the forecasts to plan marketing investment.

The code uses loose coupling, so teams can adapt the same structure to inventory planning, demand forecasting, or store traffic prediction by changing the data source and feature generation logic.

Portfolio My Past Projects



See All