Predictive Modelling with scikit-learn

Building and evaluating simple churn or conversion prediction models, and knowing when to hand off to data scientists.

Advanced · about 8 hours · difficulty 4/5

Learn first: Linear and Logistic Regression for Analysts, Customer Segmentation and Clustering

Analysts are often asked "which customers are likely to churn?" or "which leads will convert?" A simple, well-evaluated classification model can answer that and is a natural extension of the regression you already know.

The workflow

  1. Define the target precisely: churned within 60 days of the snapshot date.
  2. Build features only from information available before the snapshot to avoid leakage.
  3. Split into train and test sets (time-based when possible).
  4. Fit a baseline (logistic regression), then a tree model (random forest or gradient boosting).
  5. Evaluate on the test set.
  6. Interpret and communicate.
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.25, stratify=y, random_state=0)
model = RandomForestClassifier(n_estimators=300, random_state=0).fit(X_tr, y_tr)
print(classification_report(y_te, model.predict(X_te)))

Choosing metrics

Accuracy misleads when only 5% churn. Look at precision (of those flagged, how many churned), recall (of churners, how many we caught), ROC AUC and a confusion matrix. Tie the threshold to the business cost of a missed churner vs a wasted discount.

Interpretation

Use feature importance or permutation importance, and partial dependence plots, to explain drivers. Remember these show association, not causation.

When to hand off

Production models that retrain automatically, serve predictions in apps, or need rigorous monitoring belong with data science or ML engineering.

Pitfall: data leakage, such as using "number of support tickets in the churn month" as a feature. You are ready to move on when you can build a leak-free churn model, beat a logistic regression baseline and explain the precision-recall trade-off to a stakeholder.

Key concepts

  • Defining a prediction target
  • Feature engineering without leakage
  • Train/test splits and cross-validation
  • Logistic regression baseline
  • Tree-based models
  • Precision, recall, ROC AUC
  • Feature importance and interpretation
  • Handing off to data science

Mini project

Build a churn model on the IBM Telco Customer Churn dataset from Kaggle: logistic baseline, random forest, confusion matrix at a chosen threshold, and a one-page summary for a retention manager.

Check yourself

Why is accuracy a poor metric for a churn model when only 5% of customers churn?

Free resources