Skip to main content

Episode 15 — Phase 4: Class Imbalance

Updated 3 min readMarkdown

Act 1 — The Problem With Getting 81% Right

Phase 3 ended with an uncomfortable truth.

Logistic Regression scored 81% accuracy and an ROC-AUC of 0.842. On paper, that looks good. But it was missing 44% of actual churners — customers who were about to leave, flagged as safe, and never intervened on.

The model wasn't bad. It was optimising for the wrong thing.

When your dataset is 73% one class and 27% another, the path of least resistance for any model is to get really good at predicting the majority class. The loss function rewards it. So the model learns: when in doubt, predict No Churn. It's right most of the time. And it misses the people who matter most.

Phase 4 is about fixing that — not by changing the model architecture, but by changing what the model is penalised for getting wrong.


Act 2 — Two Approaches, One Clear Winner

Approach 1: class_weight='balanced'

The surgical fix. Instead of treating every misclassification equally, you tell the model: a missed churner costs more than a missed non-churner. sklearn calculates the weights automatically — inversely proportional to class frequency.

No new data. No resampling. Just a reweighted loss function.

Result: churn recall jumped from 0.56 → 0.78. ROC-AUC held at 0.842. Precision dropped from 0.66 → 0.50 — the model is now casting a wider net, catching more churners but also flagging more false positives.

Approach 2: SMOTE

Synthetic Minority Oversampling Technique. Instead of reweighting the loss, SMOTE generates synthetic churner samples by interpolating between existing minority class points in feature space — effectively balancing the training set before the model ever sees it.

One critical implementation detail: SMOTE goes inside the pipeline, after preprocessing, applied only to training data. Never to the test set. imblearn's Pipeline handles this correctly — sklearn's doesn't.

Result: churn recall hit 0.80. ROC-AUC at 0.839. Nearly identical to class_weight on LR — which makes sense, because for a linear model both approaches are solving the same underlying problem through different mechanisms.

The ROC curves told the full story — all LR variants stacked almost perfectly on top of each other at 0.84. The discrimination ability was always there. Imbalance handling just shifted where the decision boundary sits.

The surprise: Random Forest barely benefited from either approach. RF + SMOTE only reached 0.57 recall vs 0.80 for LR + SMOTE. Random Forest already does internal bootstrapping during training — it has a built-in partial defence against imbalance. Adding SMOTE on top adds noise more than signal.


Act 3 — The Tradeoff Is the Point

Precision dropped. That's not a failure — it's an explicit design choice.

In a churn context, the cost of missing a churner (false negative) is almost always higher than the cost of flagging a loyal customer for retention outreach (false positive). You'd rather send a discount to someone who wasn't leaving than miss someone who was.

LR + SMOTE is the model going into Phase 5: churn recall 0.80, ROC-AUC 0.839, and a pipeline that handles class imbalance cleanly without leaking test data into training.

Phase 5 brings in gradient boosting — XGBoost or LightGBM — with hyperparameter tuning. The question now isn't whether we can beat 0.80 recall. It's whether we can beat it without sacrificing precision as much.


Next up: Phase 5 — Gradient Boosting with hyperparameter tuning. The baseline is set. Time to beat it.

Six projects in. One final chapter to go.

3 views

About the author

  • A learner building ML algorithms from scratch to actually understand what's happening under the hood. Writing about the math, the mistakes, and the process and not just the results.

No comments yet. Be the first to comment.