Large-scale retail operations face two persistent risks:
- Return Fraud that erodes margins through coordinated abuse
- Product Defects that trigger costly return waves and damage customer trust
RetailGuard is a cloud-native machine learning system built on Google BigQuery ML that detects both problems using a hybrid analytical approach combining:
- Supervised fraud detection
- Unsupervised anomaly discovery
- Behavioral feature engineering
- Operational analytics visualization
The system analyzes 120,000 retail transactions and automatically surfaces high-risk patterns without requiring complex infrastructure or data movement outside BigQuery.
- Artificially elevated return rates
- Suspicious clustering by product and store
- Inflated transaction values to maximize refunds
- Coordinated behavioral abuse patterns
- Sudden spikes in Damaged returns
- Isolated product failures
- Short detection windows
- Price suppression caused by clearance reactions
Traditional rule-based monitoring struggles with evolving fraud behavior — this project demonstrates how ML-driven analytics can adapt automatically.
RetailGuard uses a Hybrid ML Architecture.
Detects known fraud patterns using labeled transactions.
Outputs:
- Fraud probability score (
fraud_prob) - Interpretable behavioral signals
Detects unknown or emerging anomalies by measuring distance from normal purchasing behavior.
Outputs:
anomaly_score(centroid distance)- Behavioral outliers are not present in training labels
Both models are merged into a single analytical dataset:
| Scenario | Detection Method |
|---|---|
| Known refund abuse | Logistic Regression |
| Emerging fraud tactics | KMeans anomaly scoring |
| Product quality failures | Time-series defect analysis |
flowchart TB
A["Retail Transactions CSV"]
B["BigQuery Bronze: transactions"]
C["BigQuery Silver: features (window metrics)"]
D["Logistic Regression (BQML)"]
E["KMeans Model (BQML)"]
F["kmeans_scored (anomaly_score)"]
G["Gold Table: hybrid_detection"]
H["Python + Seaborn Dashboard"]
A --> B
B --> C
C --> D
C --> E
E --> F
D --> G
F --> G
C --> G
G --> H
- Bronze: Raw transactions
- Silver: Engineered behavioral features
- Gold: Hybrid ML predictions + anomaly scores
All ML training occurs directly inside BigQuery.
- Loaded transaction logs into BigQuery
- Preserved product, store, and return metadata
- Rolling 30-day transaction counts
- Return frequency signals
- Average price behavior
- Logistic Regression for supervised fraud prediction
- KMeans clustering for anomaly detection
ML.PREDICTfor distance-based anomaly scoringML.DETECT_ANOMALIESfor automated outlier labeling
- Combined supervised + unsupervised signals
- Generated investigation-ready risk labels
- Clear separation between normal and fraudulent behavior
- High-risk clusters localized to specific stores and categories
- Automated detection of damaged-return spikes
- Identified clearance-price patterns linked to defective inventory
- Reduced manual investigation workload
- Faster supply chain response to defective batches
- Scalable SQL-first architecture
- Fraud probability distributions
- Anomaly score tail analysis
- Hybrid risk quadrant visualization
- Store × Category risk heatmap
- Defect spike time-series monitoring
(All charts generated via Python + Seaborn from the Gold table.)
| Component | Technology |
|---|---|
| Cloud Platform | Google Cloud Platform |
| Data Warehouse | BigQuery |
| ML Engine | BigQuery ML |
| Models | Logistic Regression + KMeans |
| Visualization | Python (Seaborn, Matplotlib) |
| Languages | SQL, Python |
| Dataset | 120k synthetic retail transactions |
This architecture can evolve into a reusable monitoring system capable of:
- Flagging high-risk returns automatically
- Detecting defective product batches early
- Reducing financial loss (“shrinkage”)
- Enabling targeted store audits instead of blanket investigations
- Gradient Boosted Trees for nonlinear fraud patterns
- ARIMA forecasting for automated defect alerts
- Streaming detection using Pub/Sub
- Active learning loop with analyst feedback
RetailGuard demonstrates practical Data Science beyond model training:
- SQL-based feature engineering
- Hybrid supervised + unsupervised ML
- Cloud-native analytics architecture
- Risk modeling translated into business decisions
- End-to-end reproducible pipeline