Skip to content

Latest commit

ย 

History

75 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Customer Churn Prediction ๐Ÿ“Š

Python TensorFlow Streamlit Scikit-learn Accuracy Status


An end-to-end machine learning system that predicts whether a bank customer will churn โ€”
built with Neural Networks and deployed as an interactive Streamlit web application.


๐Ÿ“Œ Project Overview

Customer churn costs banks millions annually. Identifying at-risk customers before they leave allows businesses to take targeted retention actions โ€” saving revenue and improving customer relationships ๐ŸŽฏ

This project answers the question:

"Given a customer's demographic and banking profile, how likely are they to leave?"


๐Ÿ“š Table of Contents


๐Ÿš€ Live Demo

Streamlit App Notebook Dataset

โš ๏ธ App may be sleeping โ€” Streamlit free tier may hibernate. Click Live App and wait ~30s.


๐Ÿ“‚ Project Structure

Customer-Churn-Prediction/
โ”‚
โ”œโ”€โ”€ app/
โ”‚   โ””โ”€โ”€ app.py                        # Streamlit web application
โ”‚
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ raw/
โ”‚   โ”‚   โ””โ”€โ”€ Churn_Modelling.csv       # Original dataset
โ”‚   โ””โ”€โ”€ processed/
โ”‚       โ””โ”€โ”€ Churn_processed.csv       # Cleaned and encoded dataset
โ”‚
โ”œโ”€โ”€ models/
โ”‚   โ”œโ”€โ”€ churn_prediction_model.h5     # Trained Neural Network
โ”‚   โ”œโ”€โ”€ scaler.pkl                    # Fitted StandardScaler
โ”‚   โ””โ”€โ”€ training_columns.pkl          # Column list for consistent inference
โ”‚
โ”œโ”€โ”€ notebooks/
โ”‚   โ””โ”€โ”€ churn_analysis.ipynb          # Full analysis notebook
โ”‚
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ preprocessing.py              # Data loading and preprocessing functions
โ”‚   โ”œโ”€โ”€ train_model.py                # Model building and training functions
โ”‚   โ””โ”€โ”€ evaluate.py                   # Model evaluation functions
โ”‚
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ README.md

๐Ÿ› ๏ธ Tech Stack

๐Ÿท๏ธ Category ๐Ÿ”ง Tools
๐Ÿ Language Python 3.11
๐Ÿง  Deep Learning TensorFlow, Keras
๐Ÿ“Š ML & Data Scikit-learn, Pandas, NumPy
๐Ÿ“ˆ Visualization Matplotlib, Seaborn
๐Ÿš€ Deployment Streamlit
๐Ÿ’พ Model Serialisation Joblib

๐Ÿ“Š Dataset Description

The dataset used is the Credit Card Customer Churn dataset from Kaggle.

  • Total Records: 10,000 customers
  • Features: 14 columns (RowNumber, CustomerId, Surname dropped before modelling)
  • Target Variable: Exited (1 = Churned, 0 = Stayed)
  • Class Distribution: 79.6% Stayed / 20.4% Churned โ€” handled via class weights
๐Ÿ”‘ Feature ๐Ÿ“ Description
CreditScore ๐Ÿ’ณ Customer credit score
Geography ๐ŸŒ Customer location (France, Germany, Spain) โ€” one-hot encoded
Gender ๐Ÿ‘ค Male / Female โ€” one-hot encoded
Age ๐ŸŽ‚ Customer age
Tenure ๐Ÿ“… Years with bank
Balance ๐Ÿ’ฐ Account balance
NumOfProducts ๐Ÿ“ฆ Number of bank products
HasCrCard ๐Ÿ’ณ Has credit card (1 = Yes, 0 = No)
IsActiveMember โœ… Active member status (1 = Yes, 0 = No)
EstimatedSalary ๐Ÿ’ต Customer estimated salary

๐Ÿ“ˆ Key Insights from EDA

๐Ÿ” Insight ๐Ÿ’ผ Business Implication
๐Ÿ“‰ 20.4% overall churn rate Significant revenue at risk โ€” retention efforts needed
๐ŸŒ Germany has highest churn Target German customers with special retention offers
๐Ÿ‘ด Age 50โ€“60 has 56.2% churn rate Highest risk group โ€” prioritise retention for middle-aged customers
๐Ÿง‘ Age 40โ€“50 has 34.0% churn rate Second highest risk group โ€” early intervention recommended
๐Ÿง’ Age <30 has only 7.5% churn rate Most loyal segment โ€” leverage for referrals and upselling
Churn Distribution
Fig 1 โ€” Churn Distribution: 79.6% stayed vs 20.4% churned
Churn Rate by Geography
Fig 2 โ€” Germany churns at 32.4%, nearly double non-German customers
Churn Rate by Age Group
Fig 3 โ€” Age 50โ€“60 has the highest churn rate at 56.2%; under-30s are the most loyal at 7.5%

โš™๏ธ Data Preprocessing

๐Ÿ”ข Step โš™๏ธ Detail
๐Ÿ—‘๏ธ Drop columns RowNumber, CustomerId, Surname removed
๐Ÿ”ค One-Hot Encoding Geography and Gender encoded using pd.get_dummies(drop_first=True)
๐Ÿ’พ Column list saved training_columns.pkl saved to ensure consistent inference
โœ‚๏ธ Train-Test Split 80% train / 20% test, random state = 1
๐Ÿ“ Feature Scaling StandardScaler โ€” fit_transform on train, transform on test

๐Ÿค– Model Training & Performance

Multiple models were evaluated with class_weight='balanced' applied to handle the 80/20 class imbalance.

๐Ÿค– Model ๐ŸŽฏ Accuracy
๐Ÿ“ˆ Logistic Regression ~72%
๐ŸŒณ Decision Tree ~76%
โœ… Neural Network ~80%

The Neural Network was selected as the final model as it significantly outperforms the baseline models and captures complex, non-linear patterns in customer behaviour that simpler models miss. Decision Trees tend to overfit, and Logistic Regression assumes linear relationships โ€” both limitations the Neural Network overcomes with its layered architecture and Dropout regularisation.

Model Loss, Accuracy, Confusion Matrix and Feature Importance
Fig 4 โ€” (Top) Training vs Validation Loss & Accuracy over 50 epochs ย |ย  (Bottom) Confusion Matrix & Permutation Feature Importance

The model converges steadily across 50 epochs with no signs of overfitting. The confusion matrix confirms strong performance on the majority class, while the permutation feature importance chart reveals that NumOfProducts and Age are the most influential predictors of churn.


๐Ÿง  Neural Network Architecture

๐Ÿ—๏ธ Layer โš™๏ธ Configuration
โžก๏ธ Input Layer 11 features
๐Ÿ”ต Dense Layer 64 neurons (ReLU)
๐Ÿ”„ Dropout 0.3
๐Ÿ”ต Dense Layer 32 neurons (ReLU)
๐Ÿ”„ Dropout 0.3
๐ŸŽฏ Output Layer 1 neuron (Sigmoid)
โšก Training Config ๐Ÿ“Š Value
๐Ÿ“‰ Loss Function Binary Crossentropy
โš™๏ธ Optimizer Adam
๐Ÿ” Epochs 50
๐Ÿ“ฆ Batch Size 32
โš–๏ธ Class Weights Applied to address 80/20 imbalance

๐Ÿ”ฎ Inference Pipeline

At inference time three saved assets are loaded:

  1. churn_prediction_model.h5 โ€” the trained neural network
  2. scaler.pkl โ€” the fitted StandardScaler
  3. training_columns.pkl โ€” the exact column list after one-hot encoding

This guarantees new customer data is encoded identically to how the training data was processed โ€” no silent column mismatches.


๐Ÿš€ Application Features

The deployed Streamlit dashboard includes:

  • ๐ŸŽ›๏ธ Customer Input Panel โ€” credit score, age, tenure, balance, number of products, credit card status, active membership, estimated salary, geography, and gender
  • ๐Ÿค– Real Model Inference โ€” predictions from the trained neural network
  • ๐Ÿšฆ Three Risk Tiers โ€” High (>50%), Medium (30โ€“50%), Low (<30%) with visual progress bar
  • ๐Ÿ’ก Retention Suggestions โ€” contextual tips based on the customer's specific risk factors

โ–ถ๏ธ Run Locally

# 1. Clone the repository
git clone https://github.com/mysticalayushi/Customer-Churn-Prediction.git
cd Customer-Churn-Prediction

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

# 3. Launch the Streamlit app
streamlit run app/app.py

Or explore the analysis notebook:

jupyter notebook notebooks/churn_analysis.ipynb

๐Ÿ’ก Business Recommendations

  1. ๐ŸŽฏ Target German customers with special retention offers as they show higher churn probability
  2. ๐Ÿ’ค Engage inactive members through loyalty programs, personalised emails, or incentives
  3. ๐Ÿ‘ด Focus on customers aged 50โ€“60 as this group has the highest churn rate at 56.2%
  4. ๐Ÿง‘ Monitor customers aged 40โ€“50 as they represent the second highest risk group at 34.0%
  5. ๐ŸŽ Introduce personalised financial products based on customer behaviour and risk score

๐Ÿ”ญ Future Improvements

  • ๐ŸŒฒ Improve model performance using Gradient Boosting or XGBoost
  • ๐Ÿ”ง Perform hyperparameter tuning to further optimise prediction accuracy
  • ๐Ÿ“Š Incorporate additional customer behaviour features such as transaction frequency
  • ๐Ÿงช Implement A/B testing strategies to evaluate churn reduction campaigns
  • โšก Build a real-time data pipeline for automated churn monitoring
  • ๐Ÿ” Add SHAP values for deeper model explainability

๐Ÿ“‹ Project Information

๐Ÿ“Œ Field ๐Ÿ“ Detail
๐Ÿ‘ฉโ€๐Ÿ’ป Created by Ayushi Rai
๐Ÿง  Model Neural Network (TensorFlow / Keras)
๐ŸŽฏ Test Accuracy ~80%
๐Ÿ“Š Dataset Credit Card Customer Churn โ€” Kaggle (10,000 records)
๐Ÿ“… Date March 2026

๐Ÿ‘ฉโ€๐Ÿ’ป Author

Ayushi Rai
GitHub


If you found this project helpful, consider giving it a โญ on GitHub!

About

An end-to-end machine learning project built using exploratory data analysis, data preprocessing, and machine learning classification models, deployed through an interactive Streamlit web application. ๐Ÿš€

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages