Skip to content

Tinyiko-Mathebula/python-churn-prediction

Repository files navigation

Customer Churn Prediction

A telecom company loses revenue every time a customer cancels their subscription. This project builds a machine learning pipeline to identify customers at high risk of churning before they leave — giving the retention team time to intervene.


Results

Model Test AUC CV AUC (5-fold)
Logistic Regression 0.764 0.771 ± 0.014
Random Forest 0.768 0.762 ± 0.021
Gradient Boosting 0.767 0.758 ± 0.015

Selected model: Random Forest — best test AUC, robust to outliers, no feature scaling required, and fast to retrain on new data.


Key Findings

  • Contract type is the strongest predictor — month-to-month customers churn at 44% vs 10–15% for annual/bi-annual contracts
  • New customers are highest risk — churn rate drops significantly after the first 12 months
  • Fiber optic customers churn more — likely driven by higher monthly charges and price sensitivity
  • Recommended decision threshold: 0.35 (vs default 0.5) — catches ~65% of churners at an acceptable false-positive rate for a low-cost retention campaign (e.g. a discount email)

Project Structure

├── Customer_Churn_Prediction.ipynb   # Full notebook — EDA, modelling, evaluation
├── customer_churn_realistic.csv      # Dataset (5,880 customers, 20 features)
└── README.md

Notebook Walkthrough

Section What's covered
1. Data Inspection Shape, dtypes, missing values, class distribution
2. EDA 6 charts testing hypotheses about churn drivers
3. Preprocessing Encoding strategy, feature engineering, train/test split
4. Modelling Logistic Regression, Random Forest, Gradient Boosting
5. Evaluation ROC-AUC, confusion matrix, cross-validation
6. Feature Importance Top predictors from both tree-based models
7. Business Recommendations Threshold tuning, retention strategy, monitoring plan

Feature Engineering

Two features were engineered beyond the raw columns:

  • tenure_group — bucketed tenure into 4 bands (0–12, 12–24, 24–48, 48–72 months) to capture non-linear early churn risk
  • charge_ratio — monthly charges divided by tenure, capturing average spend rate and the tenure-charge interaction more cleanly than either feature alone

Tech Stack

  • Python 3 — pandas, numpy, matplotlib, seaborn
  • scikit-learn — preprocessing, modelling, cross-validation, evaluation

How to Run

git clone https://github.com/Tinyiko_Mathebula/customer-churn-prediction
cd customer-churn-prediction
pip install pandas numpy matplotlib seaborn scikit-learn
jupyter notebook Customer_Churn_Prediction.ipynb

Business Context

At a threshold of 0.35, the model flags approximately 1 in 3 customers as churn risk. For a retention campaign costing $5 per outreach and saving $200 in average customer lifetime value, the model generates positive ROI if it identifies churners with precision above ~2.5% — well within its demonstrated performance.


Author

Tinyiko Patience Mathebula

About

End-to-end customer churn prediction pipeline — 0.77 AUC · 5,880 customers · 3 models compared (Logistic Regression, Random Forest, Gradient Boosting) · threshold tuning catches 65% of churners. Python · Scikit-Learn · pandas.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors