A production-ready, end-to-end machine learning pipeline that predicts whether a telecom customer will churn β from raw data all the way to a live REST API.
- π Project Overview
- ποΈ Architecture
- π Project Structure
- βοΈ Tech Stack
- π Getting Started
- π Pipeline Stages
- π Models & Metrics
- π API Reference
- π§ͺ Experiment Tracking
- π¦ Data Versioning with DVC
- π€ Contributing
Customer churn β when a customer stops using a service β is one of the most critical challenges in the telecom industry. This project builds a fully automated ML pipeline that:
- π₯ Ingests and cleans the Telco Customer Churn dataset
- π οΈ Engineers meaningful features (tenure groups, charge ratios, encoding & scaling)
- π€ Trains and compares 3 classifiers: Logistic Regression, LightGBM, and XGBoost
- π― Tunes hyperparameters with Optuna
- π Evaluates models and registers the best one to MLflow Model Registry
- β‘ Serves real-time predictions via a FastAPI REST API
Raw CSV Data
β
βΌ
βββββββββββββββββββββββ
β Feature Engineering β β Cleaning, Encoding, Scaling, Train/Test Split
βββββββββββ¬ββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β Model Training β β Logistic Regression | LightGBM | XGBoost
β (MLflow tracked) β
βββββββββββ¬ββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β Hyperparameter β β Optuna (50 trials)
β Tuning β
βββββββββββ¬ββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β Evaluation & β β Best model β MLflow Model Registry β Production
β Registration β
βββββββββββ¬ββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β FastAPI REST API β β /predict | /predict_batch
βββββββββββββββββββββββ
Customer-chunk-prediction-end-to-end-ml-system/
β
βββ src/
β βββ data_ingestion.py # Load & inspect raw data
β βββ feature_engineering.py # Clean, encode, scale, split & save
β βββ train.py # Train 3 models with MLflow logging
β βββ tune.py # Hyperparameter tuning with Optuna
β βββ evaluate.py # Evaluate best model & register to MLflow
β βββ app.py # FastAPI prediction API
β
βββ data/
β βββ raw/ # Original Telco CSV (DVC tracked)
β βββ processed/ # Train/test splits (DVC tracked)
β
βββ notebooks/
β βββ 01_eda.ipynb # Exploratory Data Analysis
β
βββ evaluation_results/ # Metrics JSON & confusion matrix plots
βββ dvc.yaml # DVC pipeline definition
βββ dvc.lock # DVC pipeline lock file
βββ params.yaml # Model hyperparameters
βββ best_lgbm_params.yaml # Best LightGBM params from tuning
βββ environment.yml # Conda environment
βββ requirements.txt # Python dependencies
βββ .env.example # Environment variable template
| Category | Tools & Libraries |
|---|---|
| Language | Python 3.12 |
| ML / Modeling | scikit-learn, XGBoost, LightGBM |
| Hyperparameter Tuning | Optuna |
| Experiment Tracking | MLflow, DagsHub |
| Data Versioning | DVC (with S3/HTTP remote) |
| API Serving | FastAPI, Uvicorn |
| Data Processing | Pandas, NumPy |
| Visualization | Matplotlib, Seaborn |
| Config / Env | python-dotenv, PyYAML |
git clone https://github.com/shovo896/Customer-chunk-prediction-end-to-end-ml-system.git
cd Customer-chunk-prediction-end-to-end-ml-systemconda env create -f environment.yml
conda activate churn-envOr using pip:
pip install -r requirements.txtcp .env.example .env
# Edit .env and fill in your DagsHub credentials| Variable | Description |
|---|---|
DAGSHUB_USERNAME |
Your DagsHub username |
DAGSHUB_REPO |
Repository name on DagsHub |
DAGSHUB_USER_TOKEN |
DagsHub personal access token |
MLFLOW_TRACKING_URI |
Auto-set via DagsHub MLflow integration |
dvc pulldvc reproThis runs all stages: feature engineering β training β evaluation.
The DVC pipeline (dvc.yaml) defines three reproducible stages:
python src/feature_engineering.py- Loads raw Telco CSV and handles missing values in
TotalCharges - Engineers
tenure_group(binned) andcharge_ratiofeatures - Encodes binary/categorical columns and applies
StandardScaler - Splits data (80/20) and saves to
data/processed/
python src/train.py- Trains Logistic Regression, LightGBM, and XGBoost
- Logs parameters, metrics, and model artifacts to MLflow via DagsHub
- Tracks:
accuracy,f1_score,roc_auc
python src/evaluate.py- Fetches the best run by
roc_aucfrom MLflow - Generates a Confusion Matrix plot saved to
evaluation_results/ - Saves metrics as JSON
- Registers the best model to MLflow Model Registry and transitions it to
Production
python src/tune.py- Uses Optuna to run 50 trials for XGBoost
- Each trial is logged to MLflow as a nested run
- Best params are saved to
best_lgbm_params.yaml
Three classifiers are trained and compared:
| Model | Key Hyperparameters |
|---|---|
| Logistic Regression | C=1.0, max_iter=100 |
| XGBoost | n_estimators=100, learning_rate=0.1 |
| LightGBM | n_estimators=100, learning_rate=0.1 |
Evaluation Metrics:
- β Accuracy β overall correctness
- β F1 Score β balance of precision & recall (important for imbalanced churn data)
- β ROC-AUC β model's ability to separate churners from non-churners
The model with the highest roc_auc is automatically selected and promoted to Production.
Start the API server:
python src/app.pyThe server starts on http://localhost:8001. Interactive docs at http://localhost:8001/docs.
Returns a welcome message.
{
"message": "Welcome to Customer Churn Prediction API",
"docs": "/docs"
}Health check endpoint.
{ "status": "ok" }Predicts churn for a single customer.
Request Body:
{
"tenure": 12,
"MonthlyCharges": 65.5,
"TotalCharges": 786.0,
"SeniorCitizen": 0,
"Partner": 1,
"gender": 1,
"Dependents": 0,
"PhoneService": 1,
"PaperlessBilling": 1,
"charge_ratio": 0.083
}Response:
{
"churn_probability": 0.7321,
"churn_prediction": 1,
"risk_level": "High",
"message": "Prediction successful"
}risk_level |
Probability Range |
|---|---|
Low |
β€ 0.4 |
Medium |
0.4 β 0.7 |
High |
> 0.7 |
Predicts churn for multiple customers at once.
Request Body: Array of customer objects (same schema as /predict).
Response:
{
"predictions": [
{ "churn_probability": 0.72, "churn_prediction": 1, "risk_level": "High" },
{ "churn_probability": 0.21, "churn_prediction": 0, "risk_level": "Low" }
],
"message": "Batch prediction successful"
}All experiments are tracked with MLflow synced to DagsHub.
π View live experiments: DagsHub MLflow Dashboard
Each training run logs:
- π Model parameters
- π Metrics (accuracy, F1, ROC-AUC)
- ποΈ Model artifact (serialized sklearn-compatible model)
The best model is registered in MLflow Model Registry under CustomerChurnModel and automatically promoted to the Production stage.
This project uses DVC to version datasets and pipeline outputs.
# Pull latest data from remote
dvc pull
# Run the full reproducible pipeline
dvc repro
# Check pipeline DAG
dvc dagPipeline stages and their dependencies are defined in dvc.yaml. Parameters are stored in params.yaml and are tracked as part of the pipeline.
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature - Commit your changes:
git commit -m "feat: add your feature" - Push and open a Pull Request
Built with β€οΈ by shovo896