Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files- README.md +38 -0
- app.py +93 -0
- forecasting-sticker-sales.ipynb +0 -0
- model_columns.pkl +3 -0
- requirements.txt +5 -0
- submission.csv +0 -0
- xgboost_model.json +0 -0
README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Sales Forecaster - Kaggle Sticker Challenge
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: streamlit
|
| 7 |
+
sdk_version: 1.32.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
python_version: "3.10"
|
| 11 |
+
license: gpl-3.0
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# Sticker Sales Prediction Project
|
| 15 |
+
|
| 16 |
+
## π Project Overview
|
| 17 |
+
This project predicts the sales volume (`num_sold`) for various Kaggle-branded products across different stores and countries. It was developed as part of a career transition into **Data Science**, focusing on Time-Series regression and feature engineering.
|
| 18 |
+
|
| 19 |
+
## π Technical Highlights
|
| 20 |
+
- **Model:** XGBoost Regressor.
|
| 21 |
+
- **Metric:** Achieved a **6.42% MAPE** (Mean Absolute Percentage Error) on the validation set.
|
| 22 |
+
- **Key Strategy:** Utilized **Log Transformation** ($y = \log(1+x)$) to handle the right-skewed distribution of sales data, which improved the MAPE from 16% to 6.4%.
|
| 23 |
+
- **Feature Engineering:** Extracted temporal features such as `day_of_week`, `is_weekend`, and `month` to capture seasonality and the "Weekend Effect."
|
| 24 |
+
|
| 25 |
+
## π File Structure
|
| 26 |
+
- `app.py`: Streamlit application code.
|
| 27 |
+
- `xgboost_model.json`: Pre-trained XGBoost model.
|
| 28 |
+
- `model_columns.pkl`: List of features used during training (to ensure input alignment).
|
| 29 |
+
- `requirements.txt`: Python dependencies.
|
| 30 |
+
|
| 31 |
+
## π οΈ Local Setup
|
| 32 |
+
To run this project locally with **Python 3.10**:
|
| 33 |
+
|
| 34 |
+
1. Clone the repository.
|
| 35 |
+
2. Create a virtual environment:
|
| 36 |
+
```bash
|
| 37 |
+
python -m venv venv
|
| 38 |
+
source venv/bin/activate # On Windows: venv\Scripts\activate
|
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import xgboost as xgb
|
| 5 |
+
import joblib
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
# Set page config for a professional look
|
| 9 |
+
st.set_page_config(page_title="Sales Forecaster", layout="centered")
|
| 10 |
+
|
| 11 |
+
@st.cache_resource
|
| 12 |
+
def load_assets():
|
| 13 |
+
# Initialize the Regressor (Must match the training model type)
|
| 14 |
+
model = xgb.XGBRegressor()
|
| 15 |
+
model.load_model("xgboost_model.json")
|
| 16 |
+
|
| 17 |
+
# Load the column list we saved during training
|
| 18 |
+
model_columns = joblib.load("model_columns.pkl")
|
| 19 |
+
return model, model_columns
|
| 20 |
+
|
| 21 |
+
# Load assets
|
| 22 |
+
try:
|
| 23 |
+
model, model_columns = load_assets()
|
| 24 |
+
except Exception as e:
|
| 25 |
+
st.error(f"Error loading model assets: {e}")
|
| 26 |
+
st.stop()
|
| 27 |
+
|
| 28 |
+
st.title("π Kaggle Sticker Sales Predictor")
|
| 29 |
+
st.markdown("""
|
| 30 |
+
This application predicts sales volume for stickers, mugs, and t-shirts across various European stores.
|
| 31 |
+
**Current Model MAPE:** 6.42%
|
| 32 |
+
""")
|
| 33 |
+
|
| 34 |
+
# User Inputs
|
| 35 |
+
with st.sidebar:
|
| 36 |
+
st.header("Input Parameters")
|
| 37 |
+
date = st.date_input("Select Date", datetime(2022, 1, 1))
|
| 38 |
+
country = st.selectbox("Country", ["Finland", "Italy", "Norway", "Spain", "Sweden"])
|
| 39 |
+
store = st.selectbox("Store", ["Kagglart", "Kaggle Rama"])
|
| 40 |
+
product = st.selectbox("Product", ["Kaggle Mug", "Kaggle Sticker", "Kaggle T-shirt"])
|
| 41 |
+
|
| 42 |
+
# Prediction Logic
|
| 43 |
+
if st.button("Predict Sales"):
|
| 44 |
+
# 1. Create a dataframe from input
|
| 45 |
+
input_data = pd.DataFrame({
|
| 46 |
+
'date': [pd.to_datetime(date)],
|
| 47 |
+
'country': [country],
|
| 48 |
+
'store': [store],
|
| 49 |
+
'product': [product]
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
# 2. Feature Engineering (Exact same as training)
|
| 53 |
+
input_data['year'] = input_data['date'].dt.year
|
| 54 |
+
input_data['month'] = input_data['date'].dt.month
|
| 55 |
+
input_data['day'] = input_data['date'].dt.day
|
| 56 |
+
input_data['dayofweek'] = input_data['date'].dt.dayofweek
|
| 57 |
+
input_data['is_weekend'] = input_data['dayofweek'].apply(lambda x: 1 if x >= 5 else 0)
|
| 58 |
+
|
| 59 |
+
# 3. One-Hot Encoding
|
| 60 |
+
# This creates columns like 'country_Italy', 'store_Kagglart', etc.
|
| 61 |
+
input_data = pd.get_dummies(input_data, columns=['country', 'store', 'product'])
|
| 62 |
+
|
| 63 |
+
# 4. Align with training columns (The "model_columns" check)
|
| 64 |
+
# This ensures columns are in the right order and missing dummies are set to 0
|
| 65 |
+
final_input = pd.DataFrame(columns=model_columns)
|
| 66 |
+
for col in model_columns:
|
| 67 |
+
if col in input_data.columns:
|
| 68 |
+
final_input.loc[0, col] = input_data.loc[0, col]
|
| 69 |
+
else:
|
| 70 |
+
final_input.loc[0, col] = 0
|
| 71 |
+
|
| 72 |
+
# Ensure all columns are numeric
|
| 73 |
+
final_input = final_input.astype(float)
|
| 74 |
+
|
| 75 |
+
# 5. Prediction (Reverse the Log Transformation)
|
| 76 |
+
# The model predicts log(1+sales), so we use expm1 to get the real number
|
| 77 |
+
log_pred = model.predict(final_input)
|
| 78 |
+
prediction = np.expm1(log_pred)[0]
|
| 79 |
+
|
| 80 |
+
# Result Display
|
| 81 |
+
st.divider()
|
| 82 |
+
cols = st.columns(2)
|
| 83 |
+
cols[0].metric("Predicted Sales", f"{int(np.round(prediction))} units")
|
| 84 |
+
|
| 85 |
+
# Contextual Insights
|
| 86 |
+
if input_data['is_weekend'].values[0] == 1:
|
| 87 |
+
st.info("π
**Weekend Effect:** Sales are typically higher on this day.")
|
| 88 |
+
|
| 89 |
+
if input_data['month'].values[0] == 12:
|
| 90 |
+
st.warning("π **Holiday Season:** Expect high volatility in December sales.")
|
| 91 |
+
|
| 92 |
+
st.markdown("---")
|
| 93 |
+
st.caption("Developed by Basak Tamer | Data Science Portfolio Project")
|
forecasting-sticker-sales.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model_columns.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c8d504653c5fd65f299f5b55983dc623b93b8351a15c8a88305dd00118f1ba2b
|
| 3 |
+
size 292
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.32.0
|
| 2 |
+
pandas==2.1.4
|
| 3 |
+
numpy==1.26.0
|
| 4 |
+
xgboost==2.0.3
|
| 5 |
+
joblib==1.3.2
|
submission.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
xgboost_model.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|