Skip to content

TalitaNLinWu/Sales_Forecasting_Machine_Learning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sales Forecasting with Machine Learning

Predicting future retail sales using Machine Learning techniques, store information, promotional campaigns, competition data, and temporal patterns.


Project Overview

Accurate sales forecasting is one of the most important challenges in retail. Reliable forecasts help companies optimize inventory levels, improve promotional planning, allocate resources efficiently, and support strategic business decisions.

This project applies supervised Machine Learning techniques to forecast future sales for Rossmann stores using historical sales records, store characteristics, promotional activities, competition information, and temporal features.

Two regression models were developed and compared: Random Forest Regressor and XGBoost Regressor. The final model was selected based on predictive performance using MAE, RMSE, and R² metrics.


Business Problem

Retail businesses must continuously estimate future demand to avoid stock shortages, reduce excess inventory, and improve operational efficiency.

The objective of this project is to build predictive models capable of estimating future store sales based on historical business data and store-related operational factors.

Reliable forecasts can support:

  • Inventory Planning
  • Demand Forecasting
  • Promotional Strategy Optimization
  • Workforce Allocation
  • Revenue Planning
  • Data-Driven Decision Making

Dataset

Rossmann Store Sales Dataset

The dataset contains:

  • Historical daily sales records
  • Store information
  • Promotional campaign details
  • Competition information
  • Holiday indicators
  • Assortment information

Dataset Size

  • 1,017,209 sales records
  • 1,115 stores
  • 41,088 unseen observations for future forecasting

Dataset Source:

https://www.kaggle.com/competitions/rossmann-store-sales/data


Technologies Used

  • Python
  • Pandas
  • NumPy
  • Matplotlib
  • Seaborn
  • Scikit-Learn
  • Random Forest Regressor
  • XGBoost Regressor
  • Jupyter Notebook

Project Workflow

  1. Data Loading
  2. Dataset Integration
  3. Initial Data Exploration
  4. Missing Values and Statistical Summary
  5. Data Cleaning
  6. Time-Based Feature Engineering
  7. Exploratory Data Analysis
  8. Data Preprocessing
  9. Temporal Train-Test Split
  10. Random Forest Sales Forecasting
  11. XGBoost Sales Forecasting
  12. Model Evaluation
  13. Actual vs Predicted Sales Analysis
  14. Feature Importance Analysis
  15. Model Performance Comparison
  16. Future Sales Forecasting
  17. Sales Forecast for Unseen Data
  18. Project Conclusion

Missing Values and Statistical Summary

Before model development, the dataset was inspected for missing values and descriptive statistics.

Missing information related to competition and promotion variables was identified and treated using appropriate imputation strategies. Statistical summaries were also analyzed to better understand sales variability, store behavior, and feature distributions.

This step ensured data quality and prepared the dataset for subsequent exploratory analysis and modeling.


Time-Based Feature Engineering

To improve forecasting performance, new temporal variables were created from the original date column.

Generated features included:

  • Year
  • Month
  • Day
  • Week
  • DayOfWeek
  • IsWeekend

These variables help the model capture recurring sales patterns, seasonality effects, and temporal business behavior.


Exploratory Data Analysis

Exploratory analysis was performed to understand sales behavior, identify seasonal patterns, evaluate promotional impacts, and investigate store characteristics.

The analysis revealed important business insights regarding seasonality, store formats, competition effects, and promotional performance, which later influenced the forecasting models.


Sales Distribution

The sales distribution presents a positively skewed pattern, where most observations are concentrated between moderate sales values while a smaller number of stores achieve significantly higher sales volumes.

This behavior is common in retail environments and highlights the presence of high-performing stores that can strongly influence total revenue. The long right tail indicates that forecasting models must be capable of handling substantial variability and nonlinear sales patterns.

Sales Distribution


Monthly Sales Trend

The monthly sales trend reveals recurring fluctuations throughout the analyzed period.

Several pronounced peaks and declines can be observed, suggesting the presence of seasonality, promotional cycles, holiday effects, and changing market conditions. Notably, sales increased considerably during specific periods while showing temporary declines in late 2014.

Capturing these temporal dynamics is essential for building reliable forecasting models capable of adapting to changing business conditions.

Monthly Sales Trend


Average Sales by Month

Analyzing average sales by month reveals a clear seasonal pattern across the retail calendar.

December achieved the highest average sales levels, significantly outperforming the other months. This behavior is likely associated with holiday shopping periods and increased consumer spending.

Months such as August, September, and October showed comparatively lower average sales levels, indicating periods of reduced demand.

Understanding these seasonal patterns is crucial for inventory planning, workforce allocation, and promotional scheduling.

Average Sales by Month


Average Sales by Store Type

Store performance varies considerably across different store formats.

Store Type B achieved average sales above 10,000 units, substantially outperforming Types A, C, and D, which remained close to the 7,000-unit range.

This result suggests that store format, location characteristics, customer demographics, or operational strategies have a significant impact on sales performance.

The strong differences between store types also help explain why store-related variables became important predictors in the forecasting model.

Average Sales by Store Type


Promotion Impact Analysis

Promotions had a substantial impact on store performance.

Stores running promotional campaigns achieved average sales of approximately 8,200 units, while stores without promotions averaged around 5,900 units.

This represents an increase of nearly 39%, highlighting promotional campaigns as one of the strongest revenue drivers identified in the analysis.

For retail managers, this finding reinforces the importance of strategic campaign planning and targeted promotional initiatives to maximize sales performance.

Promotion Impact


Model Performance Comparison

Two Machine Learning regression models were developed and evaluated.

Model MAE RMSE R² Score
Random Forest 736.36 1078.64 0.8798
XGBoost 1113.61 1490.09 0.7706

The Random Forest model achieved the best predictive performance, producing lower forecasting errors and a significantly higher R² score.

This indicates that Random Forest was more effective at capturing nonlinear relationships between sales, promotions, store characteristics, competition factors, and temporal patterns.


Why Random Forest Performed Better

Random Forest outperformed XGBoost across all evaluation metrics, achieving substantially lower forecasting errors and stronger explanatory power.

The model was particularly effective at capturing nonlinear relationships between promotional activities, competition factors, store-specific characteristics, and temporal patterns.

Its lower MAE and RMSE values indicate more accurate sales estimates, while the higher R² score demonstrates a stronger ability to explain sales variability across different stores and time periods.

As a result, Random Forest was selected as the final forecasting model.


Actual vs Predicted Sales Analysis

The scatter plot compares actual sales values against model predictions.

Most observations are concentrated near the diagonal reference line, indicating that predicted values closely follow real sales behavior across a wide range of sales levels.

Some dispersion can be observed for very high sales volumes, which is expected in retail forecasting due to promotional spikes and unusual demand events.

Overall, the strong concentration around the ideal prediction line demonstrates that the Random Forest model captured the underlying sales patterns and generalized well to unseen observations.

Actual vs Predicted Sales


Feature Importance Analysis

Feature importance analysis revealed the variables that contributed most to forecasting performance.

Top Predictive Features

  1. Competition Distance
  2. Store ID
  3. Promotional Campaigns (Promo)
  4. Competition Opening Month
  5. Competition Opening Year

Competition Distance emerged as the most influential feature, suggesting that local competitive pressure plays a major role in store performance.

Stores located closer to competitors may experience stronger competitive pressure, directly affecting customer demand and sales behavior.

Store-specific characteristics also proved highly relevant, indicating that individual stores exhibit unique sales patterns that strongly affect forecasting accuracy.

Promotional activities ranked among the top predictors, reinforcing previous findings that marketing campaigns have a substantial impact on revenue generation.

Feature Importance


Future Sales Forecasting

The forecast comparison demonstrates that the Random Forest model captured both the overall sales level and short-term fluctuations throughout the evaluation period.

Predicted values closely followed actual sales behavior, including recurring peaks and declines observed across multiple weeks.

Although minor deviations occur during extreme sales events, the model consistently reproduces the underlying demand pattern, making it suitable for operational forecasting and business planning applications.

Future Sales Forecast


Sales Forecast for Unseen Data

After model validation, the final Random Forest model was applied to the Rossmann test dataset containing 41,088 previously unseen observations.

The resulting forecasts were exported as:

sales_forecast_predictions.csv

Prediction Summary

Metric Value
Total Forecasts 41,088
Average Predicted Sales 6,902
Median Predicted Sales 6,410
Standard Deviation 2,787
Minimum Predicted Sales 973
Maximum Predicted Sales 31,205

The prediction distribution indicates substantial variability across stores and time periods.

Most forecasts were concentrated between approximately 5,000 and 8,200 sales units, while a smaller number of observations reached significantly higher values above 30,000 units.

These results are consistent with the historical sales distribution observed during exploratory analysis and suggest that the model is capable of generating realistic forecasts across both average-performing and high-performing stores.


Key Business Insights

  • Promotional campaigns increased average sales by approximately 39%.
  • Competition distance was the most influential factor affecting store performance.
  • Store-specific characteristics significantly contributed to sales variability.
  • Strong seasonal patterns were observed throughout the dataset.
  • December consistently generated the highest average sales volumes.
  • Different store formats exhibited significantly different sales behaviors.
  • Machine Learning models can effectively support retail demand forecasting and operational planning.

Project Limitations

Although the results are promising, some limitations should be considered:

  • External variables such as weather conditions and local events were not included.
  • Economic factors and inflation effects were not available in the dataset.
  • XGBoost was implemented without extensive hyperparameter optimization.
  • Forecasting performance may decrease during unusual market conditions.
  • A single temporal train-test split was used instead of rolling validation windows.

Future Improvements

Potential enhancements for future versions include:

  • Hyperparameter optimization using GridSearchCV or Optuna.
  • Time-series cross-validation with rolling windows.
  • Evaluation of LightGBM, CatBoost, Prophet, and SARIMA models.
  • Integration of weather and economic indicators.
  • Development of an interactive forecasting dashboard.
  • Deployment as a real-time forecasting API.

Project Conclusion

This project developed a complete sales forecasting pipeline using Machine Learning techniques.

Among the evaluated models, Random Forest achieved the best performance, reaching an R² Score of 0.88 while significantly outperforming XGBoost.

The analysis demonstrated that competition intensity, store characteristics, promotional activities, and seasonal patterns are key drivers of retail sales performance.

The resulting forecasting system can support inventory planning, promotional strategy optimization, resource allocation, and data-driven business decisions, demonstrating the practical value of Machine Learning in retail operations.


Project Structure

Sales-Forecasting/
│
├── data/
│   ├── train.csv
│   ├── store.csv
│   ├── test.csv
│   └── sales_forecast_predictions.csv
│
├── images/
│   ├── sales_distribution.png
│   ├── monthly_sales_trend.png
│   ├── average_sales_by_month.png
│   ├── store_type_sales.png
│   ├── promotion_impact.png
│   ├── actual_vs_predicted_sales.png
│   ├── feature_importance.png
│   └── future_sales_forecast.png
│
├── notebooks/
│   └── sales_forecasting.ipynb
│
├── README.md
├── requirements.txt
└── .gitignore

Author

Talita Niqian Lin Wu

LinkedIn: https://www.linkedin.com/in/talita-niqian-lin-wu/

GitHub:

Sales Forecasting with Machine Learning

Project Overview

This project applies supervised Machine Learning techniques to forecast future sales for Rossmann stores using historical sales data, store characteristics, promotional activities, competition information, and temporal patterns.

The objective is to develop predictive models capable of estimating future sales and uncovering the key factors that influence store performance. The resulting forecasts can support inventory planning, promotional strategy optimization, demand forecasting, and data-driven business decisions.


Objectives

  • Analyze historical sales behavior
  • Identify seasonal and promotional sales patterns
  • Engineer time-based features for forecasting
  • Develop Machine Learning forecasting models
  • Compare model performance
  • Generate future sales predictions
  • Extract business insights from sales data

Dataset

Rossmann Store Sales Dataset

The dataset contains:

  • Historical daily sales records
  • Store information
  • Promotional campaign details
  • Competition information
  • Holiday indicators
  • Assortment information

Dataset source:

https://www.kaggle.com/competitions/rossmann-store-sales/data


Technologies Used

  • Python
  • Pandas
  • NumPy
  • Matplotlib
  • Seaborn
  • Scikit-learn
  • Random Forest Regressor
  • XGBoost Regressor
  • Jupyter Notebook

Project Pipeline

  1. Data Loading
  2. Dataset Integration
  3. Data Understanding
  4. Exploratory Data Analysis (EDA)
  5. Data Cleaning
  6. Missing Values Treatment
  7. Feature Engineering
  8. Time-Based Feature Creation
  9. Data Preprocessing
  10. Temporal Train-Test Split
  11. Random Forest Modeling
  12. XGBoost Modeling
  13. Model Evaluation
  14. Forecast Analysis
  15. Feature Importance Analysis
  16. Future Sales Forecasting
  17. Business Insights and Conclusions

Sales Distribution

The sales distribution shows a positively skewed pattern, where most stores generate moderate sales volumes while a smaller number of observations reach significantly higher sales values.

This behavior is common in retail datasets and highlights the importance of using robust Machine Learning models capable of handling nonlinear relationships.

Sales Distribution


Monthly Sales Trend

Monthly sales analysis reveals fluctuations over time, indicating the presence of seasonality and changing business conditions throughout the observation period.

Understanding these patterns is essential for building reliable forecasting models.

Monthly Sales Trend


Promotion Impact Analysis

Promotional campaigns have a strong positive effect on sales performance.

Stores running promotions achieved substantially higher average sales compared to periods without promotions, confirming that marketing actions play a significant role in revenue generation.

Promotion Impact


Feature Importance Analysis

The Random Forest model identified the variables that contributed most to forecasting performance.

The most influential features were:

  • Competition Distance
  • Store Identification
  • Promotional Campaigns (Promo)
  • Competition Opening Information
  • Time-Based Variables

The results suggest that competitive pressure, store-specific behavior, promotional activities, and temporal patterns are key drivers of sales performance.

Feature Importance


Forecasting Models

Two Machine Learning models were developed and compared:

Random Forest Regressor

An ensemble learning algorithm based on decision trees capable of capturing nonlinear relationships and complex sales patterns.

XGBoost Regressor

A gradient boosting algorithm optimized for predictive performance and efficiency.

After evaluation, Random Forest achieved the best forecasting performance and was selected as the final model.


Future Sales Forecast

The selected forecasting model successfully captured the overall sales trend and recurring seasonal patterns observed in the historical data.

Predicted sales closely followed actual sales throughout the evaluation period, demonstrating the model's ability to generalize to future observations and generate realistic forecasts.

Future Sales Forecast


Forecasting on Unseen Data

After validation, the final model was applied to the Rossmann test dataset to generate sales predictions for previously unseen observations.

The resulting forecasts were exported as:

sales_forecast_predictions.csv

This simulates a real-world forecasting scenario where future sales must be estimated using only historical information and business-related features.


Key Business Insights

  • Promotional campaigns significantly increase sales performance.
  • Competition proximity strongly influences store revenue.
  • Individual store characteristics contribute to sales variability.
  • Temporal features help capture seasonality and recurring sales patterns.
  • Machine Learning can effectively support demand forecasting and business planning.

Conclusion

This project successfully developed a complete sales forecasting pipeline using Machine Learning techniques.

Historical sales data, promotional activities, competition information, and temporal patterns were combined to build predictive models capable of estimating future store sales.

The final Random Forest model demonstrated strong forecasting capability by accurately capturing sales trends and generating realistic predictions for future periods.

These results highlight the value of Machine Learning for:

  • Demand Forecasting
  • Inventory Planning
  • Promotional Strategy Optimization
  • Resource Allocation
  • Data-Driven Decision Making

Project Structure

Sales-Forecasting/
│
├── data/
│   ├── train.csv
│   ├── store.csv
│   └──test.csv
│
├── images/
│   ├── sales_distribution.png
│   ├── monthly_sales_trend.png
│   ├── promotion_impact.png
│   ├── feature_importance.png
│   └── future_sales_forecast.png
│
├── notebooks/
│   ├── sales_forecasting.ipynb
│   └── sales_forecast_predictions.csv
│
├── README.md
├── requirements.txt

Author

Talita Niqian Lin Wu

LinkedIn: https://www.linkedin.com/in/talita-niqian-lin-wu/

GitHub: https://github.com/TalitaNLinWu

About

Machine Learning project for forecasting future sales using Rossmann Store Sales data, feature engineering, Random Forest, XGBoost, and business insights.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors