E-commerce businesses lose millions every year to cart abandonment and missed conversions. This project builds a binary classification model that predicts whether an online shopping session will end in a purchase — using behavioral signals like page views, bounce rates, exit rates, and session metadata.
The goal: give e-commerce teams a reliable signal to personalize UX or trigger real-time interventions for high-intent visitors.
- Task: Binary Classification (
Revenue: Purchase or No Purchase) - Dataset: Online Shoppers Purchasing Intention Dataset
- Best Model: Decision Tree Classifier (Tuned via GridSearchCV)
| Property | Value |
|---|---|
| Total Samples | 12,329 |
| Features | 17 (10 numerical, 7 categorical) |
| Target | Revenue (True/False → 1/0) |
| Class Imbalance | ~84.5% No Purchase / ~15.5% Purchase |
| Missing Values | None |
Key features:
PageValues— average value of pages visited before transactionExitRates— average exit rate of pages visitedBounceRates— average bounce rate of pages visitedProductRelated,ProductRelated_Duration— product page engagementMonth,VisitorType,Weekend— session context
Raw CSV → EDA → Feature Split → Train/Test Split → Preprocessing Pipeline → Model Training → GridSearchCV → Evaluation
1️⃣ Load & Inspect — Load CSV, check nulls, understand class distribution
2️⃣ Feature Separation — Split numerical vs categorical columns automatically using select_dtypes
3️⃣ Train/Test Split — 80/20 split with stratify=y to preserve class ratios
4️⃣ Preprocessing Pipeline — StandardScaler for numerics, OneHotEncoder for categoricals via ColumnTransformer
5️⃣ Model Training — DecisionTreeClassifier with class_weight="balanced" to handle imbalance
6️⃣ Hyperparameter Tuning — GridSearchCV over max_depth and min_samples_leaf
7️⃣ Evaluation — F1 Score, Precision, Recall, Classification Report, Confusion Matrix
PrePruning:
dtc_model = DecisionTreeClassifier(
max_depth=6,
min_samples_leaf=30,
class_weight="balanced",
random_state=42
)
pipe = Pipeline(steps=[
("preprocess", preprocessor),
("model", dtc_model)
])class_weight="balanced"corrects for the 84/16 class imbalance automaticallymax_depth=6prevents overfitting on noisy behavioral signalsmin_samples_leaf=30ensures stable leaf nodes with sufficient support- Best GridSearchCV params:
max_depth=4,min_samples_leaf=50
| Model | Precision | Recall | F1 Score | Accuracy |
|---|---|---|---|---|
| Decision Tree (Baseline) | 0.504 | 0.833 | 0.628 | 84.71% |
| 🏆 Decision Tree (Tuned) | 0.498 | 0.833 | 0.623 | 84.39% |
Note: High recall (83.3%) is the key optimization target — missing a real buyer (False Negative) costs more than flagging a non-buyer (False Positive). The
class_weight="balanced"parameter drives this recall-first behavior.
- 📈
PageValueshas a +0.49 positive correlation with Revenue — the single strongest predictor; users who browse high-value pages are far more likely to purchase - 📉
ExitRateshas a -0.21 negative correlation with Revenue — high exit rates are a clear signal of abandonment intent - 📉
BounceRateshas a -0.15 negative correlation with Revenue — single-page sessions rarely convert ProductRelatedpage engagement (both count and duration) correlates positively at +0.15, confirming deeper product browsing = higher purchase probabilitySpecialDayproximity slightly reduces purchase probability (-0.08) — possibly due to browsing-only behavior during holidaysclass_weight="balanced"was critical: without it, the model predicts mostly non-purchase due to 84/16 class split
shop-smart-ecommerce/
│
├── shop_smart_ecommerce.ipynb # Main notebook: EDA, training, evaluation
├── shop_smart_ecommerce.csv # Dataset (Online Shoppers Purchasing Intention)
└── README.md # Project documentation
# Clone the repository
git clone https://github.com/ronakrajput8882/Shop-Smart-Ecommerce.git
cd Shop-Smart-Ecommerce
# Install dependencies
pip install pandas scikit-learn seaborn matplotlib jupyter
# Launch notebook
jupyter notebook shop_smart_ecommerce.ipynb- Imbalanced classification requires intentional design —
class_weight="balanced"shifted the model from 0% recall on the minority class to 83%, without any oversampling - Sklearn Pipelines prevent data leakage — fitting the scaler only on train data, applied on test, is the correct production pattern
- High recall ≠ high precision in imbalanced tasks — depending on business cost, optimizing for recall over F1 or accuracy may be the right call
PageValuesdominates — a single engineered feature derived from Google Analytics carries more signal than all session-time features combined- GridSearchCV with
scoring="f1"ensures tuning aligns with actual business metric, not just accuracy
| Tool | Use |
|---|---|
| Python 3.10+ | Core language |
| Pandas | Data loading & manipulation |
| Scikit-learn | Pipeline, preprocessing, model, GridSearch |
| Seaborn / Matplotlib | EDA visualization |
| Jupyter Notebook | Interactive development |