This notebook details the process of forecasting weekly sales for Walmart stores and departments, leveraging a comprehensive dataset that includes historical sales, store information, and various economic indicators. The project involved extensive data preprocessing, feature engineering, exploratory data analysis (EDA), and the development of both machine learning (XGBoost) and time series (SARIMAX) models.
The dataset comprises:
stores.csv: Anonymized store information (Type and Size).train.csv: Historical training data (2010-02-05 to 2012-11-01) withStore,Dept,Date,Weekly_Sales, andIsHoliday.test.csv: Identical totrain.csvbutWeekly_Salesare withheld for prediction.features.csv: Additional data includingTemperature,Fuel_Price,MarkDown1-5,CPI,Unemployment, andIsHoliday.
Key challenge: Modeling promotional markdown events preceding major holidays (Super Bowl, Labor Day, Thanksgiving, Christmas), which are weighted five times higher in evaluation.
- Master Merge:
trainandtestdatasets were concatenated, then merged withstoresandfeaturesbased onStore,Date, andIsHoliday. - Missing Values: Markdown-related columns (
MarkDown1-5),CPI, andUnemploymentwere initially filled with0. - Date Conversion: The
Datecolumn was converted to datetime objects. - Temporal Features:
Day,Month,Year, andWeekOfYearwere extracted from theDate. - Days to Holiday: A
Days_To_Holidayfeature was created to count days until the next major holiday (Christmas dates were used as proxies). - Lag Features:
Sales_Last_Yearwas created by shiftingWeekly_Salesby 52 weeks to capture strong seasonal patterns. Initial missing values were filled with0. - Categorical Encoding: The
Typecolumn (A, B, C) was numerically mapped (1, 2, 3) andIsHolidaywas converted to integer (0 or 1). - Additional Features:
IsMajorHoliday: Binary flag for approximate major holiday weeks.Promo_Intensity: Sum of all MarkDown values.Size_Bin: CategorizedSizeinto 'Small', 'Medium', 'Large'.Size_x_Holiday: Interaction term betweenSizeandIsHoliday.IsWeekend: Binary flag if the day is a weekend.
- Weekly Sales Distribution: Highly skewed, with a long tail indicating high sales volumes during peak periods, especially holidays. Minimum sales could be negative (returns).
- Seasonal Patterns: Strong seasonal trends observed; sales peak in Q4 (November–December) due to holidays and promotions, with lows in Q1.
- Holiday Impact: Average sales during holiday weeks (
$17035) are significantly higher (approx. 20-50%) than non-holiday weeks ($15901), confirming their importance. - Promotional Impact:
Promo_Intensityshows a moderate correlation (r~0.2–0.4) with sales, particularly noticeable in data post-2011. - Store Type and Size: Larger stores (Type A) exhibit significantly higher average sales compared to Type C stores. Store
Sizeis a key predictor (r~0.25). - Economic Factors:
Unemploymentnegatively impacts sales (r~-0.1), whileFuel_Pricehas a minimal direct effect.CPIandFuel_Priceshow gradual changes over time, contrasting with the more dramatic fluctuations in sales. - Top Performers: Departments 92 and 95 in Store 14 (and others) consistently generate the highest revenue.
- Time Series Analysis Pre-requisite: For classical time series models, Store 14, Department 92 was identified as a promising candidate due to its consistent data and significant holiday weeks.
-
Baseline Model: An initial XGBoost model was trained and evaluated.
- Mean Absolute Error (MAE):
3891.06 - R-squared Score:
0.9111
- Mean Absolute Error (MAE):
-
Hyperparameter Tuning (GridSearchCV):
- Best parameters found:
learning_rate: 0.1,max_depth: 9,n_estimators: 500. - Tuned Model Performance:
- Mean Absolute Error (MAE):
1592.77 - R-squared Score:
0.9806
- Mean Absolute Error (MAE):
- Comparison with Baseline:
- MAE Improvement:
2674.64 - R2 Improvement:
0.0836
- MAE Improvement:
- Best parameters found:
The tuned XGBoost model demonstrated a significant improvement, explaining over 98% of the variance in weekly sales, and its predictions aligned much more closely with actual sales values.
- Feature Importance (XGBoost):
The feature importance analysis highlighted the most influential factors in predicting weekly sales, with
Sales_Last_Yearlikely being a top predictor due to its strong seasonal signal, followed by other temporal and store-specific features.
-
Stationarity Check: The
Weekly_Salesseries for Store 14, Dept 92 was found to be non-stationary (ADF p-value > 0.05). First-order differencing (shift(2)) was applied to achieve stationarity. -
SARIMAX Model: A SARIMAX model was initially fitted with
order=(1,1,1)andseasonal_order=(1,0,1,52)using selected exogenous variables.- Validation WMAE (original scale):
21532.16 - Regular MAE (original scale):
22241.95
- Validation WMAE (original scale):
-
Hyperparameter Tuning (
auto_arima):auto_arimawas used to find optimal(p,d,q)(P,D,Q,s)orders, incorporating exogenous variables.- Best SARIMAX order found:
(0, 1, 2) - Best seasonal order found:
(0, 1, 0, 52)
-
Retuned SARIMAX Model Performance:
- The model was refitted with the optimal parameters from
auto_arima. - Retuned Validation WMAE (original scale):
17248.17 - Retuned Regular MAE (original scale):
18791.82 - WMAE Improvement:
4283.98(compared to the original SARIMAX).
- The model was refitted with the optimal parameters from
The project successfully developed and evaluated models for Walmart sales forecasting. The XGBoost model performed exceptionally well across the entire dataset after hyperparameter tuning, achieving a high R-squared and significantly reducing MAE. The SARIMAX model, applied to a specific store-department combination, also showed strong performance, especially after auto_arima tuning, demonstrating the effectiveness of time series methods for individual series forecasting. The combination of strong feature engineering and robust modeling techniques proved effective in capturing the complex patterns of retail sales, including seasonal and holiday-driven fluctuations.