Sunday , November 29 2026
End-to-end ML project: churn model with a scikit-learn Pipeline, metrics, FastAPI service, Docker, deployment and monitoring.

End-to-End Customer Churn Prediction Project (Train, Serve, Deploy)

Updated: 28 November 2026Level: AdvancedReading time: about 15 minutesscikit-learn · FastAPI · Docker

This end-to-end project takes a customer-churn model all the way from raw data to a deployed, monitored API – the full lifecycle you are expected to show in data science and ML engineering interviews.

What You Will Learn

  • Structure an ML project like a professional
  • Build a preprocessing + model Pipeline
  • Track experiments
  • Serve predictions with FastAPI and Docker
  • Monitor and retrain

Prerequisites

MLOps series.

Project Structure

Example
churn-project/
├── data/raw/telco_churn.csv
├── src/
│   ├── train.py        # train + evaluate + save
│   └── api.py          # FastAPI service
├── tests/test_model.py
├── models/churn.joblib
├── requirements.txt
├── Dockerfile
└── README.md

Dataset: the public Telco Customer Churn dataset (about 7,000 customers with tenure, contract, monthly charges and a Churn column).

Step 1: Train with a Pipeline

src/train.py
import joblib, pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, roc_auc_score

df = pd.read_csv("data/raw/telco_churn.csv")
df["TotalCharges"] = pd.to_numeric(df["TotalCharges"], errors="coerce").fillna(0)
y = (df.pop("Churn") == "Yes").astype(int)
X = df.drop(columns=["customerID"])

num = ["tenure", "MonthlyCharges", "TotalCharges"]
cat = [c for c in X.columns if c not in num]
pre = ColumnTransformer([("num", StandardScaler(), num),
                         ("cat", OneHotEncoder(handle_unknown="ignore"), cat)])
pipe = Pipeline([("pre", pre), ("model", GradientBoostingClassifier(random_state=0))])

X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, stratify=y, random_state=0)
pipe.fit(X_tr, y_tr)
proba = pipe.predict_proba(X_te)[:, 1]
print(classification_report(y_te, proba > 0.5))
print("ROC-AUC:", round(roc_auc_score(y_te, proba), 3))
joblib.dump(pipe, "models/churn.joblib")

Explanation: The Pipeline bundles scaling, encoding and the model, so the API applies exactly the same preprocessing. stratify=y keeps the churn ratio equal in train and test. Report precision, recall and ROC-AUC – accuracy alone is misleading because most customers do not churn. Log each run with MLflow.

Step 2: Serve with FastAPI

src/api.py
import joblib, pandas as pd
from fastapi import FastAPI

app = FastAPI(title="Churn API")
pipe = joblib.load("models/churn.joblib")

@app.post("/predict")
def predict(customer: dict):
    p = float(pipe.predict_proba(pd.DataFrame([customer]))[0, 1])
    risk = "high" if p > 0.6 else "medium" if p > 0.3 else "low"
    return {"churn_probability": round(p, 3), "risk": risk}

Explanation: For a real service, replace dict with a Pydantic model listing every field, as in the FastAPI tutorial.

Step 3: Test, Containerise, Deploy

  • Write tests for the model and the API (CI/CD for ML).
  • Add a Dockerfile and build an image (Docker).
  • Deploy to Cloud Run, AWS or a VM; add a /health endpoint.

Step 4: Monitor and Retrain

  • Log every request and prediction.
  • Check feature drift weekly (ML Deployment).
  • When actual churn labels arrive, compute real precision and recall.
  • Retrain on recent data and promote only if it beats the current model.

README Checklist

  • Problem and business impact
  • Data source and licence
  • Approach and results table
  • How to run: train, test, serve
  • Screenshot or demo link

All Projects

Projects: Beginner: House Price Prediction · Intermediate: Customer Segmentation · Advanced: PDF Question-Answering Assistant · End-to-End: Churn Prediction App · Industry Project Ideas · GitHub Portfolio Guide

Interview Questions

Q: Why use a Pipeline?
It prevents training/serving skew and data leakage by bundling preprocessing with the model.

Q: Which metric for churn?
Recall and precision for the churn class, plus ROC-AUC or PR-AUC; choose the threshold based on the cost of missing a churner vs. a wasted offer.

Practice Exercises

  1. Complete the project and push it to GitHub with a README.
  2. Add a Streamlit front-end that calls the API.
  3. Tune the threshold to catch 80% of churners and report the precision.

Summary

An end-to-end project covers data, a reproducible pipeline, proper metrics, an API, a container, deployment and monitoring – exactly what employers want to see.

Next: GitHub Portfolio Guide

About Machine Learning

Check Also

House Price Prediction Project with Python (Beginner ML Project)

Beginner machine learning project: predict house prices with pandas and scikit-learn, compare Linear Regression and Random Forest, explain and predict.