Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Telco Customer Churn Prediction

Predict which telecom customers are likely to churn using SQL-based exploratory analysis and machine learning — enabling targeted retention campaigns before customers leave.


Key Highlights

  • 26.5% overall churn rate identified across 7,043 customers
  • Customers in their first 6 months churn at 52.94% — over 3× higher than long-tenure customers
  • Fiber optic + month-to-month contract customers are the highest-risk segment (41.89% churn rate)
  • Senior citizens churn at 1.8× the rate of non-senior customers
  • LR + SMOTE selected as best model: Recall 0.797, F1 0.637, ROC-AUC 0.845

Project Structure

telco-churn-prediction/
├── data/
│   ├── raw/
│   │   └── Telco_Customer_Churn.csv
│   ├── processed/
│   │   ├── telco_feature_engineered.csv
│   │   └── customer_ids.csv
│   └── result/
│       └── top_100_customer_highest_churn_rate.csv
│
├── docs/
│   └── DATA_DICTIONARY.md
│
├── notebooks/
│   ├── data_preparation_and_loading.ipynb
│   ├── preprocessing.ipynb
│   └── scale_modeling.ipynb
│
├── sql/
│   ├── 01_who_is_churning.sql
│   ├── 02_how_services_affect_churn.sql
│   └── 03_how_customer_behavior_affects_churn.sql
│
└── README.md

Pipeline Overview

1. Data Preparation (data_preparation_and_loading.ipynb)

  • Load raw CSV, inspect data types, missing values, and target distribution
  • Convert TotalCharges from object to float; encode SeniorCitizen as Yes/No
  • Push cleaned data to MySQL for SQL-based analysis

2. SQL Exploratory Analysis (sql/)

01 — Who is churning? Segment churn rates by gender, senior citizen status, partner, and dependents. Key finding: senior citizens and single customers are significantly higher-risk groups.

SeniorCitizen Total Customers Churned Churn Rate (%)
Yes 1,142 476 41.68
No 5,901 1,393 23.61

02 — How do services affect churn? Analyze churn across phone service, internet type, add-on services (security, backup, tech support), and streaming. Key finding: fiber optic users without protective add-ons show the highest churn, and early-tenure customers are most at-risk.

Tenure Group Total Customers Churn Rate (%)
0–6 months 1,481 52.94
7–12 months 705 35.89
13–24 months 1,024 28.71
24+ months 3,833 14.04

03 — How does customer behavior affect churn? Examine contract type, payment method, and monthly/total charges. Key finding: month-to-month customers have both the highest churn rate and the highest monthly costs.

Contract Type Avg Monthly Charges ($) Churn Rate (%)
Month-to-month 66.40 42.71
One year 65.05 11.27
Two year 60.77 2.83

3. Preprocessing & EDA (preprocessing.ipynb)

  • Handle missing values: TotalCharges nulls filled with 0 for new customers (tenure = 0)
  • Save customerID separately before dropping, for use in final output
  • Visualize class imbalance (73% No / 27% Yes → handled with SMOTE)
  • Outlier detection via IQR on numerical features
  • Churn rate bar charts across all categorical features

Feature Engineering

Feature Description
tenure_group Tenure bucketed into 4 lifecycle stages (0–6, 7–12, 13–24, 24+ months)
TotalServices Count of active services per customer
AvgChargesPerService Monthly charges normalized by service count
ChargesToTenureRatio Total charges relative to tenure
IsSeniorSingle Flag: senior citizen with no partner (high-risk segment)
HighRiskPayment Flag: month-to-month contract + electronic check payment
HasFamily Flag: customer has partner or dependents

4. Modeling (scale_modeling.ipynb)

  • Stratified 80/20 train-test split to preserve class distribution
  • Preprocessing pipeline: StandardScaler for numerical, OneHotEncoder for categorical
  • Baseline models: Logistic Regression, Random Forest, Gradient Boosting
  • Resampling: SMOTE applied inside pipeline on training data only
  • Hyperparameter tuning: GridSearchCV with 5-fold stratified cross-validation on Logistic Regression
  • Evaluation metrics: Accuracy, Precision, Recall, F1-Score, ROC-AUC

Model Performance

Model Accuracy Precision Recall F1-Score ROC-AUC Category
Logistic Regression 0.8070 0.6796 0.5160 0.5866 0.8463 Baseline
Random Forest 0.7913 0.6449 0.4759 0.5477 0.8167 Baseline
Gradient Boosting 0.7977 0.6519 0.5107 0.5727 0.8401 Baseline
LR + SMOTE (tuned) 0.7594 0.5312 0.7968 0.6374 0.8452 SMOTE
RF + SMOTE 0.7821 0.5954 0.5588 0.5766 0.8137 SMOTE
GB + SMOTE 0.7871 0.5869 0.6684 0.6250 0.8416 SMOTE

Selected model: LR + SMOTE (tuned) — highest Recall (0.797) and F1-Score (0.637), most effective at identifying customers likely to churn.


Results — Top 100 Highest Churn Risk Customers

Customers are scored by churn probability and ranked. The top 100 are exported to data/result/top_100_customer_highest_churn_rate.csv for use by the retention team.

CustomerID Contract Tenure MonthlyCharges ChurnProbability Risk Segment
5178-LMXOP Month-to-month 1 95.10 0.9861 High
7216-EWTRS Month-to-month 1 100.80 0.9860 High
9497-QCMMS Month-to-month 1 93.55 0.9853 High
9300-AGZNL Month-to-month 1 94.00 0.9853 High
5419-JPRRN Month-to-month 1 101.45 0.9832 High
... ... ... ... ... ...
1143-NMNQJ Month-to-month 2 85.70 0.9488 High

All top 100 customers share the same pattern: month-to-month contract, tenure ≤ 3 months, and high monthly charges — consistent with SQL findings.


Key Business Findings

Finding Churn Rate Recommendation
Customers in first 6 months 52.94% Onboarding support program in months 1–3
Fiber optic + no security/backup ~46% Bundle protective add-ons with fiber plans
Month-to-month + electronic check 42.71% Incentivize auto-pay and annual contract upgrades
Senior citizens (no partner) 41.68% Simplified plans + dedicated support line
Customers without dependents ~2× average Personalized retention offers for single users

How to Run

# 1. Clone the repository
git clone https://github.com/your-username/telco-churn-prediction.git
cd telco-churn-prediction

# 2. Install dependencies
pip install -r requirements.txt

# 3. Run notebooks in order
jupyter notebook
# → notebooks/data_preparation_and_loading.ipynb  (loads data to MySQL)
# → notebooks/preprocessing.ipynb                 (EDA + feature engineering)
# → notebooks/scale_modeling.ipynb                (modeling + evaluation → outputs top_100 CSV)

Note: MySQL must be running locally. Update credentials in data_preparation_and_loading.ipynb before running.


Dataset

  • Source: Kaggle — IBM Telco Customer Churn
  • Size: 7,043 rows × 21 columns
  • Target: Churn (Yes / No)
  • Features: 3 numeric (tenure, MonthlyCharges, TotalCharges) + 18 categorical

Tech Stack

Category Tools
Language Python 3.x
Data processing pandas, NumPy
Visualization Matplotlib, Seaborn
ML & tuning scikit-learn, imbalanced-learn
Database MySQL, SQLAlchemy
Environment Jupyter Notebook

Future Improvements

  • Add SHAP values to explain individual predictions
  • Deploy a Streamlit dashboard for interactive risk scoring
  • Experiment with XGBoost / LightGBM for potential performance gains

About

Telco customer churn prediction using SQL analysis and machine learning (LR + SMOTE) — outputs top 100 high-risk customers for retention campaigns

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages