Mathew K Analytics

Lesson 22 · Data Mining

Customer Churn Prediction: Step-by-Step Python Tutorial Using Real Data

Welcome! This tutorial will guide you through a real-world data mining project: predicting customer churn with Python. You will learn step by step from data…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Week 56 Hands-on Project: Predicting Customer Churn#

Welcome! This tutorial will guide you through a real-world data mining project: predicting customer churn with Python.

You will learn step by step from data loading to model evaluation.

Let us get started!

import warnings
import numpy as np
np.random.seed(42)
warnings.filterwarnings("ignore")

# Suppress any warnings for a smoother experience

What is Customer Churn?#

Churn means when customers leave a service or company. Businesses want to predict churn so they can prevent it.

Data mining helps us find patterns and predict who may leave.

# Data setup (Telecom Customer Churn Dataset)
import pandas as pd
url = 'https://raw.githubusercontent.com/IBM/telco-customer-churn-on-icp4d/master/data/Telco-Customer-Churn.csv'
df = pd.read_csv(url)
print(df.shape)
print(df.head(3))
(7043, 21)
   customerID  gender  SeniorCitizen Partner Dependents  tenure PhoneService  \
0  7590-VHVEG  Female              0     Yes         No       1           No   
1  5575-GNVDE    Male              0      No         No      34          Yes   
2  3668-QPYBK    Male              0      No         No       2          Yes   

      MultipleLines InternetService OnlineSecurity  ... DeviceProtection  \
0  No phone service             DSL             No  ...               No   
1                No             DSL            Yes  ...              Yes   
2                No             DSL            Yes  ...               No   

  TechSupport StreamingTV StreamingMovies        Contract PaperlessBilling  \
0          No          No              No  Month-to-month              Yes   
1          No          No              No        One year               No   
2          No          No              No  Month-to-month              Yes   

      PaymentMethod MonthlyCharges  TotalCharges Churn  
0  Electronic check          29.85         29.85    No  
1      Mailed check          56.95        1889.5    No  
2      Mailed check          53.85        108.15   Yes  

[3 rows x 21 columns]

Step 1: Data Exploration#

First, let us see what features the dataset has.

Understanding your data is always the first step.

print(df.columns)

# Print the column names
Index(['customerID', 'gender', 'SeniorCitizen', 'Partner', 'Dependents',
       'tenure', 'PhoneService', 'MultipleLines', 'InternetService',
       'OnlineSecurity', 'OnlineBackup', 'DeviceProtection', 'TechSupport',
       'StreamingTV', 'StreamingMovies', 'Contract', 'PaperlessBilling',
       'PaymentMethod', 'MonthlyCharges', 'TotalCharges', 'Churn'],
      dtype='object')
df.info()

# Show summary info, including nulls
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 7043 entries, 0 to 7042
Data columns (total 21 columns):
 #   Column            Non-Null Count  Dtype  
---  ------            --------------  -----  
 0   customerID        7043 non-null   object 
 1   gender            7043 non-null   object 
 2   SeniorCitizen     7043 non-null   int64  
 3   Partner           7043 non-null   object 
 4   Dependents        7043 non-null   object 
 5   tenure            7043 non-null   int64  
 6   PhoneService      7043 non-null   object 
 7   MultipleLines     7043 non-null   object 
 8   InternetService   7043 non-null   object 
 9   OnlineSecurity    7043 non-null   object 
 10  OnlineBackup      7043 non-null   object 
 11  DeviceProtection  7043 non-null   object 
 12  TechSupport       7043 non-null   object 
 13  StreamingTV       7043 non-null   object 
 14  StreamingMovies   7043 non-null   object 
 15  Contract          7043 non-null   object 
 16  PaperlessBilling  7043 non-null   object 
 17  PaymentMethod     7043 non-null   object 
 18  MonthlyCharges    7043 non-null   float64
 19  TotalCharges      7043 non-null   object 
 20  Churn             7043 non-null   object 
dtypes: float64(1), int64(2), object(18)
memory usage: 1.1+ MB

Step 2: Data Cleaning#

Good models need clean data.

Let us handle missing values and tidy up columns for easier use.

# Quick cleaning: drop 'customerID', fill missing 'TotalCharges'
df = df.drop('customerID', axis=1)
df['TotalCharges'] = pd.to_numeric(df['TotalCharges'], errors='coerce')
df['TotalCharges'].fillna(df['TotalCharges'].median(), inplace=True)
# Encode categorical variables for modeling
df_encoded = pd.get_dummies(df, drop_first=True)
print(df_encoded.shape)
 
 
(7043, 31)

Step 3: Quick Data Visualization#

Let us look at the distribution of churned versus non-churned customers.

Visualization helps us spot imbalances and trends.

import matplotlib.pyplot as plt

df['Churn'].value_counts().plot(kind='bar', color=['skyblue', 'salmon'])
plt.title('Churn vs Non-Churn Customers')
plt.xlabel('Churn')
plt.ylabel('Count')
plt.show()
No description has been provided for this image

Step 4: Splitting Data for Training and Testing#

We must train on some data and test on new data to check our model.

Let us split the data now.

from sklearn.model_selection import train_test_split
X = df_encoded.drop('Churn_Yes', axis=1)
y = df_encoded['Churn_Yes']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Step 5: Training Our First Classification Model#

Let us use a decision tree for a first prediction.

This will help us see basic accuracy and set a baseline.

from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier(random_state=42)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
Accuracy: 0.71
# Try logistic regression for comparison
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(max_iter=1000, random_state=42)
lr.fit(X_train, y_train)
y_pred_lr = lr.predict(X_test)
print(f"Logistic Regression Accuracy: {accuracy_score(y_test, y_pred_lr):.2f}")
Logistic Regression Accuracy: 0.82
# Confusion matrix to show more detail
from sklearn.metrics import confusion_matrix
import seaborn as sns
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Decision Tree Confusion Matrix')
plt.show()
No description has been provided for this image

Step 6: Feature Importance#

Knowing which features are most important helps businesses act.

Let us look at the top drivers of churn from our tree.

import numpy as np
# Get top 5 features
feat_import = pd.Series(clf.feature_importances_, index=X_train.columns)
feat_import.nlargest(5).plot(kind='barh', color='purple')
plt.title('Top 5 Important Features for Churn')
plt.show()
No description has been provided for this image

Step 7: Try a Mini Challenge#

Now it is your turn! Change the model or features and see how accuracy changes.

Extra: Try adding a new column or dropping one.

# Practice: Try input() to choose test size
test_size = float(input("Type a test size fraction (like 0.3 for 30% test): "))
X_train2, X_test2, y_train2, y_test2 = train_test_split(X, y, test_size=test_size, random_state=42)
clf2 = DecisionTreeClassifier(random_state=42)
clf2.fit(X_train2, y_train2)
print("New accuracy:", accuracy_score(y_test2, clf2.predict(X_test2)))
New accuracy: 0.738760056791292

Recap: What We Learned#

  • Load and explore churn data
  • Clean and encode features
  • Train classifiers and check accuracy
  • Visualize feature importance

Learning by doing is the key. Well done!

Next Steps and Extra Tips#

  • Try other classifiers like Random Forest or XGBoost
  • Explore cross-validation for better reliability
  • Always visualize and check your predictions

Remember, practice is powerful!

Thank you for learning with us!

If you found this helpful, please subscribe and share the video.

See you in the next lesson!

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.