Bank Churn Prediction on Azure

Ten account fields in, a churn probability and the reasons for it out, retrained on Azure whenever new labelled rows land.

ML / DL2026All work

Fig. 01Churn predictor page with a section rail on the left, a ten-field profile form and a 94.5 percent churn probability with driver bars beside it.
Churn predictor on Azure Static Web Apps, an at-risk customer scored at 94.5% with the factors that moved it.

What it is

What it does

The churn dataset started at 10,000 rows and the modelling is ordinary, so the interesting part is everything around it: a service that scales to zero, scores that come with a reason attached, a page that scores a CSV of customers as readily as one at a time, a history scoped to one browser, and a retrain that starts when a new CSV lands rather than when somebody remembers. The API is FastAPI on Azure Container Apps, the page is a Static Web App, scores go to Cosmos DB, and a Function plus an Azure ML job handle the retraining.

Why it exists

Every churn model I could find was a notebook. A notebook is fine right up to the moment the data moves, and then it is a dead end. I wanted the whole loop instead: a model anyone can call, a reason attached to every score, and a retrain that happens because new rows arrived rather than because somebody remembered to run a script.

0.913
ROC AUC
88.8%
Held-out accuracy
0→1
Replicas, idle to busy

Repo Amit-kr26/churn-azureDemo proud-grass-091798800.3.azurestaticapps.netStack Python · FastAPI · scikit-learn · Azure ML · Container Apps · Azure Functions · Event Grid · Cosmos DB · Bicep · GitHub Actions

Runs on Azure Container Apps, which scales to zero, so the first request after a quiet period takes about 15 seconds.

Build9decisions

How it is built

Model bake-off

Every run fits five models on 80% of the rows: logistic regression, random forest, extra trees, gradient boosting and a small neural network. All five go through probability calibration, so a score of 30% means roughly 30% of such customers churn. The winner is the best cross-validated F1, a tie going to the higher cross-validated AUC. Right now that is a random forest, 0.715 F1 and 0.913 AUC on the held-out 20%.

F1-tuned cut-off

Only about one customer in five churns, so accuracy on its own would reward a model that never flags anybody. Each model's threshold is swept across 91 values on out-of-fold predictions and the best F1 wins. The model in service flags at 37%, and that number travels inside the bundle next to the algorithm, so the API and the page can never disagree about it.

Hashed holdout

Splitting at random on every run would mean a retrained model gets judged against a different test set, and the comparison would be meaningless. So customers are assigned to the held-out set by hashing CustomerId: the same 20% stays out of training as the data grows, and a retrained model is scored against the model already in service on exactly those rows.

Model-agnostic explain

/explain swaps each feature for the training-set median or mode and reports how far the churn probability moves. No SHAP dependency, and it runs against the calibrated pipeline rather than a wrapper bolted around it. It works whichever model won that week, which matters, because the winner does change.

Data-triggered retrain

A CSV lands in the raw container, Event Grid fires, and a Function counts the rows it has not trained on yet. At 1,000 untrained rows it submits training/train.py as an Azure ML job on a CPU cluster that scales to zero, then polls the job to completion and publishes the result.

Promotion gate

Before anything is written the job scores the model in service on the new held-out rows. If the new model has a lower F1, or widens the recall gap between countries or genders by more than 0.10, nothing is published and the current model stays exactly where it is.

Pointer publication

A bundle goes to a new folder under models/versions/<id>/ and latest.json is written last, so the API never sees half a bundle. Every bundle carries a SHA-256 manifest, and the API refuses one that does not match it.

Hot swap, no restart

The service reads latest.json every five minutes, downloads a strictly newer bundle, checks it against its manifest and swaps the model in place. No redeploy, no downtime, and a bundle that fails the check is ignored rather than loaded.

Managed identity

Container Apps, the Function and the ML workspace reach storage and Cosmos DB with managed identity and RBAC, and GitHub Actions deploys with OIDC and no stored credentials. The one shared secret left is the Container Apps connection string that writes logs to Log Analytics.

How the pieces connect

Retrain when data arrives Serve, explain and observe raw container new labelled CSV Event Grid BlobCreated Function counts untrained rows Azure ML job CPU cluster 0 to 1 models container versions + pointer Browser Static Web App Container Apps FastAPI service Cosmos DB scores, 30 days App Insights 5xx and job alerts Log Analytics 30 day retention gated on the incumbent latest.json, manifest checked, hot swap logs and traces