Stop 09 · Trees & Ensemble MethodsProjects · 1/1

Customer Churn Prediction With XGBoost

Part of the Machine Learning Roadmap

In short

Winning a new customer usually costs more than keeping an old one. A churn model flags who is about to leave, so the business can act before they go.

Predict which customers are likely to leave a telecom or subscription service using XGBoost. The project covers imbalanced classes, threshold choice, feature importance and turning predictions into a retention action.

The blob runs a small recharge shop and looks at a row of customer cards, a few of them walking away.

Think of it like this

The recharge shop uncle

The uncle at the mobile recharge shop knows his regulars. When someone who came every month hasn't shown up in weeks and complained about the network last time, he guesses they've switched SIMs. A churn model makes that same guess, for lakhs of customers.

Watch it happen

Your build plan

Load data
Check ratio
Encode
Train
Recall
Explain

1/6Use the public Telco Customer Churn dataset. Each row is a customer, Churn is Yes or No.

The big ideas

What you need to know

Classes are imbalanced

If 75% of customers stay, a model that says 'everyone stays' scores 75% accuracy and catches zero leavers.

Recall on churners matters

Recall answers: of the people who left, how many did we flag? That's usually what the business cares about.

Tune the threshold

predict_proba gives a churn probability. Lowering the cutoff from 0.5 catches more leavers at the cost of more false alarms.

Explain the why

Managers act on reasons, not scores. Show which features push churn risk up, using coefficients or feature importances.

See it in code

Walk through the code

python
1import pandas as pd2from sklearn.linear_model import LogisticRegression3from sklearn.metrics import classification_report4from sklearn.model_selection import train_test_split5 6df = pd.read_csv("telco_churn.csv")7y = (df["Churn"] == "Yes").astype(int)8X = pd.get_dummies(df[["tenure", "MonthlyCharges", "Contract"]], drop_first=True)9 10X_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=1)11model = LogisticRegression(class_weight="balanced", max_iter=1000)12model.fit(X_tr, y_tr)13print(classification_report(y_te, model.predict(X_te)))

1/4Turn Yes/No into 1/0, and expand Contract into 0/1 columns with get_dummies.

class_weight='balanced' makes each missed churner cost more during training, so the model stops ignoring the smaller class.

Save yourself the pain

Common mistakes

Oops

Celebrating 80% accuracy without checking how many churners were caught.

Do this instead

Look at recall for the churn class and the confusion matrix. Accuracy hides the minority class.

Oops

Keeping columns like 'cancellation_date' that only exist after a customer leaves.

Do this instead

Use only what you'd know before the churn happens. Future information is leakage and fakes a great score.

Your mission

Milestone 1: a fair first model

~40 min

Quick check

Your churn model has 85% accuracy but only 20% recall on churners. What does that mean?

Go deeper

  1. 1Use the Telco Customer Churn dataset from Kaggle.
  2. 2Report precision and recall at a chosen threshold, not just accuracy.
  3. 3Explain the top churn drivers and one action the business could take.

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge