Skip to content

E-Commerce Customer Risk ML Pipeline and API

Python · DuckDB · XGBoost · Optuna · SHAP · MLflow · Azure Machine Learning · FastAPI

Overview

This project demonstrates an end-to-end analytics and ML workflow using the Brazilian Olist e-commerce dataset: SQL-based feature preparation, statistical analysis, model tuning and explanation, experiment tracking, and an API that returns a risk score with an optional generated retention message.

Pipeline

  1. Read Olist CSV data and expose it as DuckDB views.
  2. Derive delivery, order, and product features with SQL.
  3. Compare review scores for on-time and delayed orders and prepare a classifier dataset.
  4. Tune an XGBoost model with Optuna, evaluate it using ROC AUC, and generate SHAP explanations.
  5. Log experiment metrics and artifacts with MLflow, using Azure Machine Learning tracking when configured and local MLflow tracking as a fallback.
  6. Serve predictions through FastAPI and Pydantic. For high-scoring cases, the API can request a suggested retention message from OpenAI.

Model Target and Limitations

Despite the project’s retention and churn framing, the training target in the code is a low customer review score (1–2 stars). It is a proxy for an adverse customer experience, not a label showing that a customer actually stopped purchasing. The score therefore should not be presented as a validated churn probability.

The API generates message text; it does not send a campaign to customers. The project demonstrates a technical workflow and does not establish business impact or production suitability by itself.

Technologies

Python, DuckDB, SQL, Pandas, NumPy, SciPy, scikit-learn, XGBoost, Optuna, SHAP, MLflow, Azure ML SDK, FastAPI, Pydantic, Uvicorn, OpenAI API, Docker, Render, GitHub Actions.