How to build a lift chart a.k.a gains chart in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A lift chart, also called a gains chart, shows how much better a model performs than random selection when ranked by predicted probability. It is especially useful for binary classification tasks such as churn prediction, fraud detection, or campaign targeting. This guide walks through the full Python workflow from predictions to chart interpretation.
Prepare Model Scores and Labels
Lift analysis starts with two arrays: true binary labels and model probabilities for the positive class.
Scores should represent likelihood of positive outcome and must be sorted from highest to lowest.
Build Deciles and Cumulative Gains
Create quantile buckets, then compute cumulative positives and cumulative sample share.
cum_gain measures captured positives up to each bucket. lift compares that gain to random targeting.
Plot Gains and Lift Curves
Plot both curves to understand ranking quality and top-segment efficiency.
A strong model climbs quickly in the gains chart and shows lift greater than one in early buckets.
Interpret Results for Business Decisions
If the top ten percent of ranked users captures forty percent of positives, the model has strong prioritization value. Teams can target only top buckets when outreach budget is limited.
Compare lift curves across model versions, not just AUC scores. Two models can have similar AUC but different top-decile lift, which matters for campaign efficiency.
Also check calibration separately. Lift chart quality reflects ranking ability, not probability calibration.
Compare Lift Across Candidate Models
Lift charts are most useful when comparing competing models on the same holdout set. Build one gains table per model and overlay curves in a single figure. Focus on early buckets, since business programs often target only a small population segment. If Model A has slightly lower overall AUC but much stronger top-decile lift, it may still deliver better campaign outcomes. Always report sample counts per bucket so stakeholders understand statistical stability before making deployment decisions.
Common Pitfalls
A common mistake is using predicted class labels instead of probabilities. Lift charts require ranking signal, so class labels lose information.
Another issue is computing deciles before sorting by score. That breaks cumulative interpretation and yields misleading curves.
Teams also forget to evaluate on holdout data. Lift measured on training data is overly optimistic.
Finally, highly imbalanced datasets can produce unstable bucket metrics with small test sets. Use sufficient sample size and repeated validation splits.
Summary
- Lift and gains charts evaluate ranking effectiveness for binary models.
- Use true labels plus predicted probabilities, sorted by score descending.
- Compute cumulative sample share, cumulative gain, and lift by buckets.
- Plot both gains and lift curves to assess top-segment performance.
- Evaluate on holdout data and align interpretation with business targeting goals.

