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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
library_name: scikit-learn
|
| 4 |
+
tags:
|
| 5 |
+
- tabular-regression
|
| 6 |
+
- tabular-classification
|
| 7 |
+
- e-commerce
|
| 8 |
+
- customer-behavior
|
| 9 |
+
- sales-analysis
|
| 10 |
+
- feature-engineering
|
| 11 |
+
- clustering
|
| 12 |
+
- business-insights
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# Total Amount Prediction Model
|
| 16 |
+
### E-Commerce Customer Behavior and Sales Analysis
|
| 17 |
+
|
| 18 |
+
This project presents a complete data science workflow for analyzing e-commerce customer behavior and predicting customer purchase value.
|
| 19 |
+
|
| 20 |
+
The project includes exploratory data analysis, feature engineering, clustering, regression modeling, classification modeling, model evaluation, feature importance analysis, advanced business visualizations, and business insights.
|
| 21 |
+
|
| 22 |
+
The final trained models were saved as pickle files and uploaded to this Hugging Face repository.
|
| 23 |
+
|
| 24 |
+
---
|
| 25 |
+
|
| 26 |
+
## Project Summary
|
| 27 |
+
|
| 28 |
+
The main goal of this project is to understand what drives customer spending in an e-commerce environment and to build machine learning models that can predict purchase behavior.
|
| 29 |
+
|
| 30 |
+
The original target variable is:
|
| 31 |
+
|
| 32 |
+
`Total_Amount`
|
| 33 |
+
|
| 34 |
+
Two prediction tasks were completed:
|
| 35 |
+
|
| 36 |
+
| Task | Goal |
|
| 37 |
+
|---|---|
|
| 38 |
+
| Regression | Predict the continuous purchase amount, `Total_Amount` |
|
| 39 |
+
| Classification | Classify purchases into Low, Medium, or High Purchase |
|
| 40 |
+
|
| 41 |
+
The project also includes advanced business analysis to identify valuable product categories, city-category opportunities, and customer purchase patterns.
|
| 42 |
+
|
| 43 |
+
---
|
| 44 |
+
|
| 45 |
+
## Repository Files
|
| 46 |
+
|
| 47 |
+
| File | Description |
|
| 48 |
+
|---|---|
|
| 49 |
+
| `Maya's_Assignment_2.ipynb` | Full notebook containing EDA, feature engineering, modeling, evaluation, and insights |
|
| 50 |
+
| `winning_model.pkl` | Saved winning regression model |
|
| 51 |
+
| `winning_classification_model.pkl` | Saved winning classification model |
|
| 52 |
+
| `README.md` | Project documentation and model card |
|
| 53 |
+
|
| 54 |
+
---
|
| 55 |
+
|
| 56 |
+
## Dataset Description
|
| 57 |
+
|
| 58 |
+
The dataset used in this project is the **E-Commerce Customer Behavior and Sales Analysis Dataset** from Kaggle.
|
| 59 |
+
|
| 60 |
+
It contains customer, product, transaction, discount, payment, device, and delivery-related information.
|
| 61 |
+
|
| 62 |
+
### Main Features
|
| 63 |
+
|
| 64 |
+
| Feature | Description |
|
| 65 |
+
|---|---|
|
| 66 |
+
| `Order_ID` | Unique order identifier |
|
| 67 |
+
| `Customer_ID` | Unique customer identifier |
|
| 68 |
+
| `Age` | Customer age |
|
| 69 |
+
| `Gender` | Customer gender |
|
| 70 |
+
| `City` | Customer city |
|
| 71 |
+
| `Product_Category` | Product category purchased |
|
| 72 |
+
| `Unit_Price` | Price per unit |
|
| 73 |
+
| `Quantity` | Number of units purchased |
|
| 74 |
+
| `Discount_Amount` | Discount amount |
|
| 75 |
+
| `Discount_Percentage` | Discount percentage |
|
| 76 |
+
| `Total_Amount` | Final purchase amount |
|
| 77 |
+
| `Payment_Method` | Payment method used |
|
| 78 |
+
| `Device_Type` | Device used for purchase |
|
| 79 |
+
| `Is_Returning_Customer` | Indicates whether the customer is returning |
|
| 80 |
+
| `Customer_Rating` | Customer rating |
|
| 81 |
+
| `Delivery_Time_Days` | Delivery time in days |
|
| 82 |
+
| `Date` | Transaction date |
|
| 83 |
+
|
| 84 |
+
---
|
| 85 |
+
|
| 86 |
+
## Project Workflow
|
| 87 |
+
|
| 88 |
+
The project follows a full machine learning workflow:
|
| 89 |
+
|
| 90 |
+
1. Dataset description
|
| 91 |
+
2. Exploratory Data Analysis
|
| 92 |
+
3. Data cleaning
|
| 93 |
+
4. Outlier analysis
|
| 94 |
+
5. Feature engineering
|
| 95 |
+
6. Clustering
|
| 96 |
+
7. Baseline regression modeling
|
| 97 |
+
8. Improved regression modeling
|
| 98 |
+
9. Feature importance analysis
|
| 99 |
+
10. Regression-to-classification conversion
|
| 100 |
+
11. Classification modeling
|
| 101 |
+
12. Classification evaluation
|
| 102 |
+
13. Advanced business insights
|
| 103 |
+
14. Model export and upload to Hugging Face
|
| 104 |
+
|
| 105 |
+
---
|
| 106 |
+
|
| 107 |
+
# 1. Exploratory Data Analysis
|
| 108 |
+
|
| 109 |
+
Exploratory Data Analysis was performed to understand the dataset before building machine learning models.
|
| 110 |
+
|
| 111 |
+
The EDA included:
|
| 112 |
+
|
| 113 |
+
- Checking missing values
|
| 114 |
+
- Checking duplicate records
|
| 115 |
+
- Understanding numerical and categorical variables
|
| 116 |
+
- Analyzing distributions
|
| 117 |
+
- Detecting outliers
|
| 118 |
+
- Studying relationships with `Total_Amount`
|
| 119 |
+
- Creating visualizations to understand spending behavior
|
| 120 |
+
|
| 121 |
+
---
|
| 122 |
+
|
| 123 |
+
## Main EDA Insights
|
| 124 |
+
|
| 125 |
+
The EDA showed that customer spending is mainly influenced by product-related and transaction-related factors.
|
| 126 |
+
|
| 127 |
+
### Key Findings
|
| 128 |
+
|
| 129 |
+
| Insight | Explanation |
|
| 130 |
+
|---|---|
|
| 131 |
+
| Product category strongly affects spending | Electronics generated the highest average purchase amounts |
|
| 132 |
+
| Unit price and quantity are major drivers | Higher unit price and higher quantity lead to higher total purchase value |
|
| 133 |
+
| City has moderate influence | Spending differs slightly across cities, but no single city dominates completely |
|
| 134 |
+
| Age and gender have weak influence | Spending behavior is relatively similar across age and gender groups |
|
| 135 |
+
| Payment method and device type show small differences | Digital Wallet and Bank Transfer were associated with slightly higher spending |
|
| 136 |
+
| Discounts show a negative relationship | Higher discounts were generally associated with lower total purchase amounts |
|
| 137 |
+
|
| 138 |
+
Overall, `Product_Category`, `Unit_Price`, `Quantity`, and discount-related features were the most important factors for understanding customer spending.
|
| 139 |
+
|
| 140 |
+
---
|
| 141 |
+
|
| 142 |
+
# 2. Advanced EDA and Business Insights
|
| 143 |
+
|
| 144 |
+
In addition to standard EDA, advanced visualizations were added to create deeper business insights.
|
| 145 |
+
|
| 146 |
+
---
|
| 147 |
+
|
| 148 |
+
## City and Product Category Heatmap
|
| 149 |
+
|
| 150 |
+
A heatmap was created to analyze the average `Total_Amount` for each combination of:
|
| 151 |
+
|
| 152 |
+
`City + Product_Category`
|
| 153 |
+
|
| 154 |
+
This helped answer the question:
|
| 155 |
+
|
| 156 |
+
**How does the average purchase amount change across cities and product categories?**
|
| 157 |
+
|
| 158 |
+
### Main Insight
|
| 159 |
+
|
| 160 |
+
Electronics had the highest average spending across almost all cities.
|
| 161 |
+
Home & Garden and Sports also showed relatively high purchase amounts in several cities.
|
| 162 |
+
|
| 163 |
+
This insight can support:
|
| 164 |
+
|
| 165 |
+
- Targeted marketing
|
| 166 |
+
- Inventory planning
|
| 167 |
+
- City-based promotions
|
| 168 |
+
- Product strategy
|
| 169 |
+
|
| 170 |
+
---
|
| 171 |
+
|
| 172 |
+
## High Purchase Rate Analysis
|
| 173 |
+
|
| 174 |
+
An interactive visualization was created to analyze which product categories are most likely to generate high-value purchases.
|
| 175 |
+
|
| 176 |
+
Instead of only looking at the average purchase amount, this analysis focused on the percentage of transactions classified as `High Purchase`.
|
| 177 |
+
|
| 178 |
+
This helped identify which product categories are more strongly connected to expensive transactions.
|
| 179 |
+
|
| 180 |
+
---
|
| 181 |
+
|
| 182 |
+
## Business Opportunity Analysis
|
| 183 |
+
|
| 184 |
+
A bonus business opportunity dashboard was created to identify the strongest combinations of:
|
| 185 |
+
|
| 186 |
+
`City + Product_Category`
|
| 187 |
+
|
| 188 |
+
The dashboard combined several business metrics:
|
| 189 |
+
|
| 190 |
+
| Metric | Meaning |
|
| 191 |
+
|---|---|
|
| 192 |
+
| Average Total Amount | Average purchase value |
|
| 193 |
+
| High Purchase Rate | Percentage of high-value transactions |
|
| 194 |
+
| Total Revenue | Total income generated |
|
| 195 |
+
| Number of Transactions | Transaction volume |
|
| 196 |
+
| Business Score | Combined score for ranking opportunities |
|
| 197 |
+
|
| 198 |
+
### Business Purpose
|
| 199 |
+
|
| 200 |
+
This analysis helps identify where the business should focus its marketing, inventory planning, and growth strategy.
|
| 201 |
+
|
| 202 |
+
The strongest opportunities are city-category combinations with:
|
| 203 |
+
|
| 204 |
+
- High average spending
|
| 205 |
+
- High percentage of high-value purchases
|
| 206 |
+
- High total revenue
|
| 207 |
+
- Strong transaction volume
|
| 208 |
+
|
| 209 |
+
---
|
| 210 |
+
|
| 211 |
+
# 3. Baseline Regression Model
|
| 212 |
+
|
| 213 |
+
The first machine learning model was a baseline Linear Regression model.
|
| 214 |
+
|
| 215 |
+
The target variable was:
|
| 216 |
+
|
| 217 |
+
`Total_Amount`
|
| 218 |
+
|
| 219 |
+
### Baseline Features
|
| 220 |
+
|
| 221 |
+
The baseline model used selected features such as:
|
| 222 |
+
|
| 223 |
+
- `Unit_Price`
|
| 224 |
+
- `Quantity`
|
| 225 |
+
- `Product_Category`
|
| 226 |
+
- `Discount_Percentage`
|
| 227 |
+
- `City`
|
| 228 |
+
- `Payment_Method`
|
| 229 |
+
- `Device_Type`
|
| 230 |
+
- `Is_Returning_Customer`
|
| 231 |
+
- `Customer_Rating`
|
| 232 |
+
- `Delivery_Time_Days`
|
| 233 |
+
|
| 234 |
+
`Age` was not included as a main feature because the EDA showed that it had a weak relationship with `Total_Amount`.
|
| 235 |
+
|
| 236 |
+
### Baseline Model Performance
|
| 237 |
+
|
| 238 |
+
| Model | MAE | MSE | RMSE | R² |
|
| 239 |
+
|---|---:|---:|---:|---:|
|
| 240 |
+
| Baseline Linear Regression | 165.91 | 63,466.31 | 251.93 | 0.9307 |
|
| 241 |
+
|
| 242 |
+
The baseline model already performed well, but feature engineering was used to improve the predictive power further.
|
| 243 |
+
|
| 244 |
+
---
|
| 245 |
+
|
| 246 |
+
# 4. Feature Engineering
|
| 247 |
+
|
| 248 |
+
Feature engineering was one of the most important parts of this project.
|
| 249 |
+
|
| 250 |
+
The goal was to create more meaningful features that help the models understand the relationship between price, quantity, discounts, product category, city, and purchase value.
|
| 251 |
+
|
| 252 |
+
---
|
| 253 |
+
|
| 254 |
+
## 4.1 Transaction-Based Features
|
| 255 |
+
|
| 256 |
+
The following transaction-based features were created:
|
| 257 |
+
|
| 258 |
+
| Feature | Description |
|
| 259 |
+
|---|---|
|
| 260 |
+
| `Total_Before_Discount` | Unit price multiplied by quantity |
|
| 261 |
+
| `Discount_Amount` | Estimated discount value |
|
| 262 |
+
| `Estimated_Final_Amount` | Estimated total amount after discount |
|
| 263 |
+
| `Discounted_Unit_Price` | Unit price after discount |
|
| 264 |
+
|
| 265 |
+
These features are important because they directly describe the purchase calculation logic.
|
| 266 |
+
|
| 267 |
+
---
|
| 268 |
+
|
| 269 |
+
## 4.2 Interaction Features
|
| 270 |
+
|
| 271 |
+
Interaction features were created to capture combined effects between categorical variables.
|
| 272 |
+
|
| 273 |
+
| Feature | Description |
|
| 274 |
+
|---|---|
|
| 275 |
+
| `City_Category` | Combination of city and product category |
|
| 276 |
+
| `City_Payment_Method` | Combination of city and payment method |
|
| 277 |
+
| `Category_Device_Type` | Combination of product category and device type |
|
| 278 |
+
| `Category_Discount` | Combination of product category and discount level |
|
| 279 |
+
| `City_Discount_Level` | Combination of city and discount level |
|
| 280 |
+
|
| 281 |
+
These features helped the models understand that customer behavior can change based on combinations of variables, not only individual features.
|
| 282 |
+
|
| 283 |
+
---
|
| 284 |
+
|
| 285 |
+
## 4.3 Behavioral Features
|
| 286 |
+
|
| 287 |
+
Additional behavioral features were created:
|
| 288 |
+
|
| 289 |
+
| Feature | Description |
|
| 290 |
+
|---|---|
|
| 291 |
+
| `Is_High_Discount` | Indicates whether the discount is high |
|
| 292 |
+
| `Is_Bulk_Order` | Indicates whether the quantity is high |
|
| 293 |
+
| `Is_Premium_Product` | Indicates whether the unit price is in the premium range |
|
| 294 |
+
| `Discount_Level` | Groups discount percentage into low, medium, and high levels |
|
| 295 |
+
|
| 296 |
+
These features add business meaning to the dataset and help describe purchasing behavior more clearly.
|
| 297 |
+
|
| 298 |
+
---
|
| 299 |
+
|
| 300 |
+
## 4.4 Clustering Features
|
| 301 |
+
|
| 302 |
+
K-Means clustering was applied to identify different purchase behavior groups.
|
| 303 |
+
|
| 304 |
+
The clustering was based on:
|
| 305 |
+
|
| 306 |
+
- `Unit_Price`
|
| 307 |
+
- `Quantity`
|
| 308 |
+
- `Discount_Percentage`
|
| 309 |
+
- `Total_Before_Discount`
|
| 310 |
+
|
| 311 |
+
Three clusters were created:
|
| 312 |
+
|
| 313 |
+
| Cluster | Interpretation |
|
| 314 |
+
|---|---|
|
| 315 |
+
| Cluster 0 | Regular or lower-value purchases |
|
| 316 |
+
| Cluster 1 | High-value purchases |
|
| 317 |
+
| Cluster 2 | Discount-driven purchases |
|
| 318 |
+
|
| 319 |
+
Additional clustering features were added:
|
| 320 |
+
|
| 321 |
+
- `Purchase_Cluster`
|
| 322 |
+
- `Distance_To_Cluster_Center`
|
| 323 |
+
- `Cluster_Confidence`
|
| 324 |
+
|
| 325 |
+
These features allowed the models to use customer purchase segments as part of the prediction process.
|
| 326 |
+
|
| 327 |
+
---
|
| 328 |
+
|
| 329 |
+
## 4.5 Numerical Interaction Terms
|
| 330 |
+
|
| 331 |
+
Numerical interaction terms were created using:
|
| 332 |
+
|
| 333 |
+
- `Unit_Price`
|
| 334 |
+
- `Quantity`
|
| 335 |
+
- `Discount_Percentage`
|
| 336 |
+
|
| 337 |
+
Examples of interaction terms:
|
| 338 |
+
|
| 339 |
+
- Unit Price × Quantity
|
| 340 |
+
- Unit Price × Discount Percentage
|
| 341 |
+
- Quantity × Discount Percentage
|
| 342 |
+
|
| 343 |
+
These features helped capture combined numerical effects that may influence `Total_Amount`.
|
| 344 |
+
|
| 345 |
+
---
|
| 346 |
+
|
| 347 |
+
## 4.6 Scaling and PCA
|
| 348 |
+
|
| 349 |
+
StandardScaler was used to scale numerical features.
|
| 350 |
+
|
| 351 |
+
PCA was also applied to reduce dimensionality while preserving 95% of the variance.
|
| 352 |
+
|
| 353 |
+
This helped reduce feature complexity while keeping most of the important information.
|
| 354 |
+
|
| 355 |
+
---
|
| 356 |
+
|
| 357 |
+
# 5. Improved Regression Models
|
| 358 |
+
|
| 359 |
+
After feature engineering, three improved regression models were trained and evaluated.
|
| 360 |
+
|
| 361 |
+
| Model | Type |
|
| 362 |
+
|---|---|
|
| 363 |
+
| Linear Regression | Linear regression model |
|
| 364 |
+
| Random Forest Regressor | Tree-based ensemble model |
|
| 365 |
+
| Gradient Boosting Regressor | Boosting ensemble model |
|
| 366 |
+
|
| 367 |
+
---
|
| 368 |
+
|
| 369 |
+
## Regression Evaluation Metrics
|
| 370 |
+
|
| 371 |
+
The regression models were evaluated using:
|
| 372 |
+
|
| 373 |
+
| Metric | Meaning |
|
| 374 |
+
|---|---|
|
| 375 |
+
| MAE | Mean Absolute Error |
|
| 376 |
+
| MSE | Mean Squared Error |
|
| 377 |
+
| RMSE | Root Mean Squared Error |
|
| 378 |
+
| R² | Percentage of variance explained by the model |
|
| 379 |
+
|
| 380 |
+
A better regression model has lower error values and a higher R² score.
|
| 381 |
+
|
| 382 |
+
---
|
| 383 |
+
|
| 384 |
+
## Regression Model Results
|
| 385 |
+
|
| 386 |
+
| Model | MAE | MSE | RMSE | R² |
|
| 387 |
+
|---|---:|---:|---:|---:|
|
| 388 |
+
| Improved Linear Regression | 160.07 | 50,623.41 | 225.00 | 0.9447 |
|
| 389 |
+
| Random Forest Regressor | 21.80 | 9,397.69 | 96.94 | 0.9897 |
|
| 390 |
+
| Gradient Boosting Regressor | 30.75 | 9,134.01 | 95.57 | 0.9900 |
|
| 391 |
+
|
| 392 |
+
### Regression Result Interpretation
|
| 393 |
+
|
| 394 |
+
The engineered features significantly improved the models.
|
| 395 |
+
|
| 396 |
+
The tree-based models performed much better than Linear Regression because they can capture complex and non-linear relationships between features.
|
| 397 |
+
|
| 398 |
+
Random Forest achieved the lowest MAE, meaning it had the smallest average prediction error.
|
| 399 |
+
Gradient Boosting achieved a slightly higher R² score, meaning it explained slightly more variance.
|
| 400 |
+
|
| 401 |
+
---
|
| 402 |
+
|
| 403 |
+
## Winning Regression Model
|
| 404 |
+
|
| 405 |
+
The selected winning regression model was:
|
| 406 |
+
|
| 407 |
+
**Random Forest Regressor**
|
| 408 |
+
|
| 409 |
+
It was selected because it achieved very strong overall performance and the lowest MAE among the tested regression models.
|
| 410 |
+
|
| 411 |
+
The model was saved as:
|
| 412 |
+
|
| 413 |
+
`winning_model.pkl`
|
| 414 |
+
|
| 415 |
+
---
|
| 416 |
+
|
| 417 |
+
# 6. Feature Importance Analysis
|
| 418 |
+
|
| 419 |
+
Feature importance analysis was performed to understand which variables were most important for prediction.
|
| 420 |
+
|
| 421 |
+
---
|
| 422 |
+
|
| 423 |
+
## Linear Regression Feature Importance
|
| 424 |
+
|
| 425 |
+
For Linear Regression, coefficients were analyzed.
|
| 426 |
+
|
| 427 |
+
- Positive coefficients increase the predicted value.
|
| 428 |
+
- Negative coefficients decrease the predicted value.
|
| 429 |
+
|
| 430 |
+
---
|
| 431 |
+
|
| 432 |
+
## Tree-Based Feature Importance
|
| 433 |
+
|
| 434 |
+
For Random Forest and Gradient Boosting, feature importance values were used.
|
| 435 |
+
|
| 436 |
+
These values show how much each feature contributes to the model’s predictions.
|
| 437 |
+
|
| 438 |
+
Unlike Linear Regression coefficients, tree-based feature importance does not show direction.
|
| 439 |
+
It only shows the strength of importance.
|
| 440 |
+
|
| 441 |
+
---
|
| 442 |
+
|
| 443 |
+
## Feature Group Importance
|
| 444 |
+
|
| 445 |
+
Because one-hot encoding created many separate columns, features were also grouped into larger categories.
|
| 446 |
+
|
| 447 |
+
Feature groups included:
|
| 448 |
+
|
| 449 |
+
- Price features
|
| 450 |
+
- Quantity features
|
| 451 |
+
- Discount features
|
| 452 |
+
- Transaction value features
|
| 453 |
+
- Product category features
|
| 454 |
+
- City features
|
| 455 |
+
- Payment method features
|
| 456 |
+
- Device type features
|
| 457 |
+
- Interaction features
|
| 458 |
+
- Cluster features
|
| 459 |
+
|
| 460 |
+
This gave a clearer explanation of which types of features were most important.
|
| 461 |
+
|
| 462 |
+
---
|
| 463 |
+
|
| 464 |
+
# 7. Regression to Classification
|
| 465 |
+
|
| 466 |
+
The continuous target variable `Total_Amount` was converted into three purchase classes.
|
| 467 |
+
|
| 468 |
+
The goal was to classify each transaction as:
|
| 469 |
+
|
| 470 |
+
| Class | Label |
|
| 471 |
+
|---|---|
|
| 472 |
+
| 0 | Low Purchase |
|
| 473 |
+
| 1 | Medium Purchase |
|
| 474 |
+
| 2 | High Purchase |
|
| 475 |
+
|
| 476 |
+
Quantile binning was used to create the classes.
|
| 477 |
+
|
| 478 |
+
The thresholds were calculated from the training data only to avoid data leakage.
|
| 479 |
+
|
| 480 |
+
### Why Quantile Binning?
|
| 481 |
+
|
| 482 |
+
Quantile binning was chosen because it creates relatively balanced classes.
|
| 483 |
+
This helps the classification models learn all classes more fairly.
|
| 484 |
+
|
| 485 |
+
---
|
| 486 |
+
|
| 487 |
+
# 8. Classification Models
|
| 488 |
+
|
| 489 |
+
Three classification models were trained:
|
| 490 |
+
|
| 491 |
+
| Model | Type |
|
| 492 |
+
|---|---|
|
| 493 |
+
| Logistic Regression | Linear classification model |
|
| 494 |
+
| Random Forest Classifier | Tree-based ensemble classifier |
|
| 495 |
+
| Gradient Boosting Classifier | Boosting ensemble classifier |
|
| 496 |
+
|
| 497 |
+
The models were trained to predict whether a transaction is:
|
| 498 |
+
|
| 499 |
+
`Low Purchase`, `Medium Purchase`, or `High Purchase`
|
| 500 |
+
|
| 501 |
+
---
|
| 502 |
+
|
| 503 |
+
## Classification Evaluation Priorities
|
| 504 |
+
|
| 505 |
+
For this business problem, recall is especially important for the `High Purchase` class.
|
| 506 |
+
|
| 507 |
+
A false negative is more serious than a false positive because it means the model missed an actual high-value purchase.
|
| 508 |
+
|
| 509 |
+
Missing high-value purchases can cause the business to miss:
|
| 510 |
+
|
| 511 |
+
- Valuable customers
|
| 512 |
+
- Revenue opportunities
|
| 513 |
+
- Premium transactions
|
| 514 |
+
- Marketing opportunities
|
| 515 |
+
|
| 516 |
+
---
|
| 517 |
+
|
| 518 |
+
## Classification Metrics
|
| 519 |
+
|
| 520 |
+
The classification models were evaluated using:
|
| 521 |
+
|
| 522 |
+
| Metric | Meaning |
|
| 523 |
+
|---|---|
|
| 524 |
+
| Accuracy | Overall percentage of correct predictions |
|
| 525 |
+
| Precision Macro | Average precision across all classes |
|
| 526 |
+
| Recall Macro | Average recall across all classes |
|
| 527 |
+
| F1 Macro | Balance between precision and recall |
|
| 528 |
+
|
| 529 |
+
Macro metrics were used because this is a multi-class classification problem and each class should receive equal importance.
|
| 530 |
+
|
| 531 |
+
---
|
| 532 |
+
|
| 533 |
+
## Classification Model Results
|
| 534 |
+
|
| 535 |
+
| Model | Accuracy | Precision Macro | Recall Macro | F1 Macro |
|
| 536 |
+
|---|---:|---:|---:|---:|
|
| 537 |
+
| Logistic Regression | 0.9633 | 0.9634 | 0.9635 | 0.9634 |
|
| 538 |
+
| Random Forest Classifier | 0.9947 | 0.9947 | 0.9948 | 0.9947 |
|
| 539 |
+
| Gradient Boosting Classifier | 0.9947 | 0.9947 | 0.9948 | 0.9947 |
|
| 540 |
+
|
| 541 |
+
---
|
| 542 |
+
|
| 543 |
+
## Classification Result Interpretation
|
| 544 |
+
|
| 545 |
+
All classification models performed well, but the tree-based models performed better than Logistic Regression.
|
| 546 |
+
|
| 547 |
+
Most classification mistakes occurred between neighboring classes:
|
| 548 |
+
|
| 549 |
+
- Low Purchase vs Medium Purchase
|
| 550 |
+
- Medium Purchase vs High Purchase
|
| 551 |
+
|
| 552 |
+
This is expected because the classes were created from a continuous target variable.
|
| 553 |
+
|
| 554 |
+
Random Forest and Gradient Boosting made fewer mistakes because they can capture more complex relationships between the engineered features.
|
| 555 |
+
|
| 556 |
+
---
|
| 557 |
+
|
| 558 |
+
## Winning Classification Model
|
| 559 |
+
|
| 560 |
+
The selected winning classification model was:
|
| 561 |
+
|
| 562 |
+
**Gradient Boosting Classifier**
|
| 563 |
+
|
| 564 |
+
It achieved the strongest overall classification performance based on accuracy, precision, recall, and macro F1-score.
|
| 565 |
+
|
| 566 |
+
The model was saved as:
|
| 567 |
+
|
| 568 |
+
`winning_classification_model.pkl`
|
| 569 |
+
|
| 570 |
+
---
|
| 571 |
+
|
| 572 |
+
# 9. Bonus Work
|
| 573 |
+
|
| 574 |
+
This project includes extra work for bonus credit.
|
| 575 |
+
|
| 576 |
+
The bonus work includes:
|
| 577 |
+
|
| 578 |
+
| Bonus Element | Description |
|
| 579 |
+
|---|---|
|
| 580 |
+
| Creative visualizations | Clear and professional visual analysis |
|
| 581 |
+
| Interactive dashboards | Plotly-based interactive charts |
|
| 582 |
+
| Business opportunity analysis | Identifying valuable city-category combinations |
|
| 583 |
+
| Clustering | Purchase behavior segmentation |
|
| 584 |
+
| Feature group importance | Higher-level model interpretation |
|
| 585 |
+
| Advanced business insights | Translating model results into business recommendations |
|
| 586 |
+
|
| 587 |
+
---
|
| 588 |
+
|
| 589 |
+
## Bonus: Advanced Business Opportunity Analysis
|
| 590 |
+
|
| 591 |
+
An advanced business opportunity analysis was created to identify the best combinations of city and product category.
|
| 592 |
+
|
| 593 |
+
The analysis combined:
|
| 594 |
+
|
| 595 |
+
- Average purchase amount
|
| 596 |
+
- High purchase rate
|
| 597 |
+
- Total revenue
|
| 598 |
+
- Number of transactions
|
| 599 |
+
- Business score
|
| 600 |
+
|
| 601 |
+
### Business Value
|
| 602 |
+
|
| 603 |
+
This analysis helps answer:
|
| 604 |
+
|
| 605 |
+
**Which city and product category combinations should the company prioritize?**
|
| 606 |
+
|
| 607 |
+
The best opportunities are combinations with:
|
| 608 |
+
|
| 609 |
+
- High average spending
|
| 610 |
+
- High high-purchase rate
|
| 611 |
+
- High total revenue
|
| 612 |
+
- Strong transaction volume
|
| 613 |
+
|
| 614 |
+
This can support:
|
| 615 |
+
|
| 616 |
+
- Targeted marketing
|
| 617 |
+
- Inventory planning
|
| 618 |
+
- Product strategy
|
| 619 |
+
- City-based promotions
|
| 620 |
+
- Revenue growth
|
| 621 |
+
|
| 622 |
+
---
|
| 623 |
+
|
| 624 |
+
# 10. Saved Models
|
| 625 |
+
|
| 626 |
+
The final models were saved as pickle files.
|
| 627 |
+
|
| 628 |
+
| File | Model | Purpose |
|
| 629 |
+
|---|---|---|
|
| 630 |
+
| `winning_model.pkl` | Random Forest Regressor | Predict continuous `Total_Amount` |
|
| 631 |
+
| `winning_classification_model.pkl` | Gradient Boosting Classifier | Predict Low, Medium, or High Purchase class |
|
| 632 |
+
|
| 633 |
+
---
|
| 634 |
+
|
| 635 |
+
# 11. How to Load the Models
|
| 636 |
+
|
| 637 |
+
The saved models can be loaded using Python and pickle.
|
| 638 |
+
|
| 639 |
+
```python
|
| 640 |
+
import pickle
|
| 641 |
+
|
| 642 |
+
with open("winning_model.pkl", "rb") as file:
|
| 643 |
+
regression_model = pickle.load(file)
|
| 644 |
+
|
| 645 |
+
with open("winning_classification_model.pkl", "rb") as file:
|
| 646 |
+
classification_model = pickle.load(file)
|