NYC Data Science Academy| Blog
Bootcamps
Lifetime Job Support Available Financing Available
Bootcamps
Data Science with Machine Learning Flagship ๐Ÿ† Data Analytics Bootcamp Artificial Intelligence Bootcamp New Release ๐ŸŽ‰
Free Lesson
Intro to Data Science New Release ๐ŸŽ‰
Find Inspiration
Find Alumni with Similar Background
Job Outlook
Occupational Outlook Graduate Outcomes Must See ๐Ÿ”ฅ
Alumni
Success Stories Testimonials Alumni Directory Alumni Exclusive Study Program
Courses
View Bundled Courses
Financing Available
Bootcamp Prep Popular ๐Ÿ”ฅ Data Science Mastery Data Science Launchpad with Python View AI Courses Generative AI for Everyone New ๐ŸŽ‰ Generative AI for Finance New ๐ŸŽ‰ Generative AI for Marketing New ๐ŸŽ‰
Bundle Up
Learn More and Save More
Combination of data science courses.
View Data Science Courses
Beginner
Introductory Python
Intermediate
Data Science Python: Data Analysis and Visualization Popular ๐Ÿ”ฅ Data Science R: Data Analysis and Visualization
Advanced
Data Science Python: Machine Learning Popular ๐Ÿ”ฅ Data Science R: Machine Learning Designing and Implementing Production MLOps New ๐ŸŽ‰ Natural Language Processing for Production (NLP) New ๐ŸŽ‰
Find Inspiration
Get Course Recommendation Must Try ๐Ÿ’Ž An Ultimate Guide to Become a Data Scientist
For Companies
For Companies
Corporate Offerings Hiring Partners Candidate Portfolio Hire Our Graduates
Students Work
Students Work
All Posts Capstone Data Visualization Machine Learning Python Projects R Projects
Tutorials
About
About
About Us Accreditation Contact Us Join Us FAQ Webinars Subscription An Ultimate Guide to
Become a Data Scientist
    Login
NYC Data Science Acedemy
Bootcamps
Courses
Students Work
About
Bootcamps
Bootcamps
Data Science with Machine Learning Flagship
Data Analytics Bootcamp
Artificial Intelligence Bootcamp New Release ๐ŸŽ‰
Free Lessons
Intro to Data Science New Release ๐ŸŽ‰
Find Inspiration
Find Alumni with Similar Background
Job Outlook
Occupational Outlook
Graduate Outcomes Must See ๐Ÿ”ฅ
Alumni
Success Stories
Testimonials
Alumni Directory
Alumni Exclusive Study Program
Courses
Bundles
financing available
View All Bundles
Bootcamp Prep
Data Science Mastery
Data Science Launchpad with Python NEW!
View AI Courses
Generative AI for Everyone
Generative AI for Finance
Generative AI for Marketing
View Data Science Courses
View All Professional Development Courses
Beginner
Introductory Python
Intermediate
Python: Data Analysis and Visualization
R: Data Analysis and Visualization
Advanced
Python: Machine Learning
R: Machine Learning
Designing and Implementing Production MLOps
Natural Language Processing for Production (NLP)
For Companies
Corporate Offerings
Hiring Partners
Candidate Portfolio
Hire Our Graduates
Students Work
All Posts
Capstone
Data Visualization
Machine Learning
Python Projects
R Projects
About
Accreditation
About Us
Contact Us
Join Us
FAQ
Webinars
Subscription
An Ultimate Guide to Become a Data Scientist
Tutorials
Data Analytics
  • Learn Pandas
  • Learn NumPy
  • Learn SciPy
  • Learn Matplotlib
Machine Learning
  • Boosting
  • Random Forest
  • Linear Regression
  • Decision Tree
  • PCA
Interview by Companies
  • JPMC
  • Google
  • Facebook
Artificial Intelligence
  • Learn Generative AI
  • Learn ChatGPT-3.5
  • Learn ChatGPT-4
  • Learn Google Bard
Coding
  • Learn Python
  • Learn SQL
  • Learn MySQL
  • Learn NoSQL
  • Learn PySpark
  • Learn PyTorch
Interview Questions
  • Python Hard
  • R Easy
  • R Hard
  • SQL Easy
  • SQL Hard
  • Python Easy
Data Science Blog > Capstone > Predicting Customer Churn Using Python

Predicting Customer Churn Using Python

Robert Willoughby
Posted on Jan 10, 2022

The skills I demoed here are taught in NYC Data Science Academy's Data Science with Machine Learning bootcamp..

Data Predicting Customer Churn Using Python

Data Predicting Customer Churn Using Python

Data Science Introduction

Customer churn, also known as attrition, is the bane of businesses that struggle to not just win but retain customers to maximize their lifetime value. Churn is defined as the number of customers that exited and stop doing business with a company. For this project, we studied bank customer attrition. The work flow is: Define the problem by understanding stakeholders requirements, preprocess and data engineering, visualization analysis, modeling , conclusion and recommendations.

Customer churn is one of the causes of higher banks expenditure and a major contributor to loss of revenue. The cost of acquiring new customers is higher than keeping existing ones due to the cost of time and resources used for onboarding. Fostering loyalty pay because it helps customers feel connected to the business, so they are less likely to churn.

Focus of project

My analysis focuses on bank customers behavior, those that are more likely to exit the bank by closing their accounts. The goal of this project is to predict customer churn using machine learning techniques, identify potential high risk customers that will churn and analyze the model to maximize business value and solve the  business challenges which are:  identifying the main factors that cause a customer to churn, the odds of churn for particular customers, and what strategies can be implemented based on findings to reducer churn in the future.

The dataset is downloaded from Kaggle (https://www.kaggle.com/mathchi/churn-for-bank-customers ). It is a Bank dataset. Using Python, I show visualizations and analyzed  four models  to predict customer attrition: K Nearest Neighbors, Logistic Regression, Random Forest and Gradient Boosting.

Data Visualization and Analysis                                                                                

Data Predicting Customer Churn Using Python

Data Predicting Customer Churn Using Python

Data Predicting Customer Churn Using Python

Data Predicting Customer Churn Using Python

The above Pie chart shows the distribution of the target variable (Exited); There are more retained customers than churn, 79.6% of customers stayed , while 20.4% churned. The bar chart shows customers by Geography; France has the most customers, followed by Spain with a small difference than Germany.  In terms of churn, Germany has the highest levels of customer, while Spain shows the lowest.

               

The above Bar chart shows that there are more male customers, and female customers are more likely to churn than males. In terms of age, the histogram shows, most of the customers are between the ages 30 to 40 years, and the lowest number of customers are over 70 years-old. The highest churn is between the ages of 40 to 50 years, and the least churn is at the extreme ages of  20 to 30 years and 70+ years. There are few customers that are 90+ years that churned.

         

The Bar chart shows, there are more customers holding two products, followed by those holding one product. The highest churn rate is by customers holding one product and the least is by customers holding four products. For customers churn based on balance, the histogram shows, there are more customers with zero balance; this category of customers also has the highest churn rate.

                     

The bar chart shows that there are more active customers, and inactive customers are more likely to churn. From the histogram, more customers are within 2 to 9 months tenure range, and the least customers have a 10 months tenure.  The highest churn is by customers that have less than two months tenure, and the least churn are customers with 10 months tenure.

Dataset and preprocessing

Before fitting the models, I did data wrangling, preprocessing, exploration and visualization. The dataset used in this project is the Churn for Bank Customers data from Kaggle https://www.kaggle.com/mathchi/churn-for-bank-customers. It comprises 10,000 observations and 14 variables. There are no missing values. The target variable is Exited, with 1 indicating churn and 0 non-churn. I dropped irrelevant variables(Row number, Customer Id and Surname),  standardized features to the same scale. I did encoding and one hot encoding for categorical variables to create dummy variables and also  examined the correlation between the features. There is no high correlation between them.

Model Fitting

After preprocessing, I split the data into 80% training and 20% testing sets, next, I conduct feature scaling by normalizing variables within a range of zero and 1. I chose the following  model classifiers for this project: K Nearest  Neighbors, Logistic Regression, Random Forest and Gradient Boosting.  In modeling, I setup hyperparameter grid for tuning, tuned hyperparameters, fit model on training data, get information about best hyperparameters, use 5 folds cross validation , predict model on test data and obtain model performance metrics as discussed below.

Model1-K Nearest Neighbors

I fit the first model KNN and tuned the hyperparameters using grid searching to maximize model performance; used K=24 and did cross-validation of 5-folds to reduce model overfitting. This model gives a cross-validation training score of 79.4% and for the sample set, it is 80.7%. Since the training and test scores are close enough, this indicates there is no overfitting. The Area Under the Receiver Operation Characteristics Curve (AUROC)  is 56%, which indicates that the model doesn't perform well.

Model2-Logistic Regression

With Logistic Regression, the predicted probability of the model was obtained, where 1 represents a customer churn and 0 non-churn. To get model accuracy, we look at  probability greater than 0.5 as customer churn and less than 0.5 is non-churn. The cross-validation used is 5-folds. Grid search was used to select the highest number of regularization parameter  to avoid overfitting. From the result output, training score is 79% and test score is 80%. The Area under the Receiver Operating Characteristics Curve is 68%, making this model's performance slightly better than that of the KNN.

Model3-Random Forest

With this ensemble method, hyperparameter space is tuned with n estimators and max features. I implemented randomized searching to tune 20 hyperparameters and the best was selected with an AUROC score of 87%.  The score on the training is 86% and 88% for the test . This model performed far better than the KNN and LR.

Model4-Gradient Boosting

I fit the Gradient Boosting and tuned the hyperparameters by setting up hyperparameter grid for tuning. The SGB of the training  score is 86% and the test score is 88%. This scores indicates no overfitting. The AUROC score is 88%, making this is the best performing model. The feature importance below shows age and number of products are the strongest features in the prediction.

Confusion Matrix

    SGB Confusion Matrix

I use a confusion matrix on the test set to evaluate correct and incorrect predictions of customers. There are 1,544 true negatives and 215 true positives customers; the true positives and true negatives are the customers that are correctly classified. There are 169 False negatives customers that are wrongly classified, since our model predicts that they will stay, though they  churned.

The impact is that, because the model predicts they will stay, no incentives were given to them. In this case, the Bank lost revenue because nothing is done to keep them. The false positives are 72, these are the customers that the model predicts will churn but they actually stayed. In this case, the Bank lost revenue because they offered them incentives. Predicted yes to Churn are 287 customers and no to Churn are 1,713 customers. The total error rate is 12%; this is low and indicates the model performs better.

Compare Models

 

To compare the models, we use AUROC to get an estimation of customers who churn with higher probabilities than non-churn. The top right corner on the curve shows a decision threshold of 0 and the bottom left shows a threshold of 1. The Gradient Boosting is the best-performing model that predicts customer churn with the highest ROC curve of 0.88 as shown above.

Hight risk Churn customers                              Low risk  no Churn customers

   

Using the Gradient Boosting Classifier, high risk customers to Churn are identified with probabilities greater than 0.9 and low risk customers are also identified as no Churn with probabilities. By identifying these customers, incentives can be offered to the high risk customers to motivate them to stay, thereby increasing revenue. On the other hand, with the low risk customers, no incentives will be offered, but high quality products and good services can be offered to all customers to reduce the Churn rate.

Data Conclusion

Key findings in this project are: long tenure customers are less likely to churn, inactive customers are more likely to churn, customers with zero balance are more likely to churn, female customers are more likely to churn, age and number of products are the strongest features to predict churn, customers with three or four products are less likely to churn, older customers above 60  are less likely to churn and those  young customers between 40 to 50 years are more likely to churn.

The Gradient Boosting is the model that predicts churn rate with the best ROC (0.88) given the optimal trade-off of True positive rate and False positive rate . From the confusion matrix, no  incentives was given to 169 false negative customers , and they ended up churning, causing a loss in revenue.

Also,  incentives offered to 72 false positive customers , since model predict they will churn but they actually stayed  reduced revenue. It is recommended that management should focus on high risk customers to churn and offer them incentives to stay. They should also ensure products and services offered should be of high quality. There also should be minimal false negative rate and true positive rate should be maximized.

You can find the code in project link below.

https://github.com/tedebalogun/Predicting-Customer-Churn-Using-Pytho

References:

  1. Davenport T.H ; Predictive Analytics: The power to predict who will click, buy, lie or die
  2. O'reilly J.V; Python Data Science Handbook

About Author

Robert Willoughby

I live in Columbus, Ohio; working as a Data Analyst
View all posts by Robert Willoughby >

Leave a Comment

No comments found.

View Posts by Categories

All Posts 2399 posts
AI 7 posts
AI Agent 2 posts
AI-based hotel recommendation 1 posts
AIForGood 1 posts
Alumni 60 posts
Animated Maps 1 posts
APIs 41 posts
Artificial Intelligence 2 posts
Artificial Intelligence 2 posts
AWS 13 posts
Banking 1 posts
Big Data 50 posts
Branch Analysis 1 posts
Capstone 206 posts
Career Education 7 posts
CLIP 1 posts
Community 72 posts
Congestion Zone 1 posts
Content Recommendation 1 posts
Cosine SImilarity 1 posts
Data Analysis 5 posts
Data Engineering 1 posts
Data Engineering 3 posts
Data Science 7 posts
Data Science News and Sharing 73 posts
Data Visualization 324 posts
Events 5 posts
Featured 37 posts
Function calling 1 posts
FutureTech 1 posts
Generative AI 5 posts
Hadoop 13 posts
Image Classification 1 posts
Innovation 2 posts
Kmeans Cluster 1 posts
LLM 6 posts
Machine Learning 364 posts
Marketing 1 posts
Meetup 144 posts
MLOPs 1 posts
Model Deployment 1 posts
Nagamas69 1 posts
NLP 1 posts
OpenAI 5 posts
OpenNYC Data 1 posts
pySpark 1 posts
Python 16 posts
Python 458 posts
Python data analysis 4 posts
Python Shiny 2 posts
R 404 posts
R Data Analysis 1 posts
R Shiny 560 posts
R Visualization 445 posts
RAG 1 posts
RoBERTa 1 posts
semantic rearch 2 posts
Spark 17 posts
SQL 1 posts
Streamlit 2 posts
Student Works 1687 posts
Tableau 12 posts
TensorFlow 3 posts
Traffic 1 posts
User Preference Modeling 1 posts
Vector database 2 posts
Web Scraping 483 posts
wukong138 1 posts

Our Recent Popular Posts

AI 4 AI: ChatGPT Unifies My Blog Posts
by Vinod Chugani
Dec 18, 2022
Meet Your Machine Learning Mentors: Kyle Gallatin
by Vivian Zhang
Nov 4, 2020
NICU Admissions and CCHD: Predicting Based on Data Analysis
by Paul Lee, Aron Berke, Bee Kim, Bettina Meier and Ira Villar
Jan 7, 2020

View Posts by Tags

#python #trainwithnycdsa 2019 2020 Revenue 3-points agriculture air quality airbnb airline alcohol Alex Baransky algorithm alumni Alumni Interview Alumni Reviews Alumni Spotlight alumni story Alumnus ames dataset ames housing dataset apartment rent API Application artist aws bank loans beautiful soup Best Bootcamp Best Data Science 2019 Best Data Science Bootcamp Best Data Science Bootcamp 2020 Best Ranked Big Data Book Launch Book-Signing bootcamp Bootcamp Alumni Bootcamp Prep boston safety Bundles cake recipe California Cancer Research capstone car price Career Career Day ChatGPT citibike classic cars classpass clustering Coding Course Demo Course Report covid 19 credit credit card crime frequency crops D3.js data data analysis Data Analyst data analytics data for tripadvisor reviews data science Data Science Academy Data Science Bootcamp Data science jobs Data Science Reviews Data Scientist Data Scientist Jobs data visualization database Deep Learning Demo Day Discount disney dplyr drug data e-commerce economy employee employee burnout employer networking environment feature engineering Finance Financial Data Science fitness studio Flask flight delay football gbm Get Hired ggplot2 googleVis H20 Hadoop hallmark holiday movie happiness healthcare frauds higgs boson Hiring hiring partner events Hiring Partners hotels housing housing data housing predictions housing price hy-vee Income industry Industry Experts Injuries Instructor Blog Instructor Interview insurance italki Job Job Placement Jobs Jon Krohn JP Morgan Chase Kaggle Kickstarter las vegas airport lasso regression Lead Data Scienctist Lead Data Scientist leaflet league linear regression Logistic Regression machine learning Maps market matplotlib Medical Research Meet the team meetup methal health miami beach movie music Napoli NBA netflix Networking neural network Neural networks New Courses NHL nlp NYC NYC Data Science nyc data science academy NYC Open Data nyc property NYCDSA NYCDSA Alumni Online Online Bootcamp Online Training Open Data painter pandas Part-time performance phoenix pollutants Portfolio Development precision measurement prediction Prework Programming public safety PwC python Python Data Analysis python machine learning python scrapy python web scraping python webscraping Python Workshop R R Data Analysis R language R Programming R Shiny r studio R Visualization R Workshop R-bloggers random forest Ranking recommendation recommendation system regression Remote remote data science bootcamp Scrapy scrapy visualization seaborn seafood type Selenium sentiment analysis sentiment classification Shiny Shiny Dashboard Spark Special Special Summer Sports statistics streaming Student Interview Student Showcase SVM Switchup Tableau teachers team team performance TensorFlow Testimonial tf-idf Top Data Science Bootcamp Top manufacturing companies Transfers tweets twitter videos visualization wallstreet wallstreetbets web scraping Weekend Course What to expect whiskey whiskeyadvocate wildfire word cloud word2vec XGBoost yelp youtube trending ZORI

NYC Data Science Academy

NYC Data Science Academy teaches data science, trains companies and their employees to better profit from data, excels at big data project consulting, and connects trained Data Scientists to our industry.

NYC Data Science Academy is licensed by New York State Education Department.

Get detailed curriculum information about our
amazing bootcamp!

Please enter a valid email address
Sign up completed. Thank you!

Offerings

  • HOME
  • DATA SCIENCE BOOTCAMP
  • ONLINE DATA SCIENCE BOOTCAMP
  • Professional Development Courses
  • CORPORATE OFFERINGS
  • HIRING PARTNERS
  • About

  • About Us
  • Alumni
  • Blog
  • FAQ
  • Contact Us
  • Refund Policy
  • Join Us
  • SOCIAL MEDIA

    ยฉ 2025 NYC Data Science Academy
    All rights reserved. | Site Map
    Privacy Policy | Terms of Service
    Bootcamp Application