How to build a lift chart a.k.a gains chart in Python?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
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.
Related reading
- How to build a multiple input graph with tensor flow?
- How to build a simple recommendation system?
- How to build an image classification dataset in Azure?
- How to build and use Google TensorFlow C api
- How to cache data during the first epoch correctly Tensorflow, dataset?
- How to calculate a logistic sigmoid function in Python?
- How to build docker with non-root user privileges to setup python application with pipenv?
- How to cache a large machine learning model in Flask?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.