Skip to content
svglift-gain

Cumulative gain and lift

If we act on the top-scored share of cases, what share of the positives do we reach?

Demo data · seed 20261101 · 42 rows · default settings · 640 × 360SVG, lightSVG, darkDemo CSV

How to read it

  1. Read the x axis as how much of the list you act on, top-scored first.
  2. Read the y axis as the share of all positives reached.
  3. Compare with the diagonal: random targeting.
  1. Callout 1: Acting on the top 20% reaches this share of the positives.
  2. Callout 2: Diagonal: random targeting.
  3. Callout 3: Everything reached, eventually.

Watch out

  • It ignores cost. Choose where to stop using the value of a hit and the cost of acting.

Defaults

Show
Cumulative gain
Which view to draw.
Also: Lift

Pinned in the code

  • Draws the run's numbers as given (computed by the host's cs-ml evaluation or the Python/R engine); the chart computes nothing statistical.
  • Cases are ranked by predicted probability, highest first; gain at x is the share of all positives among the top x of cases (deciles or every case, as the run sends).
  • Lift = gain ÷ share targeted; when the run sends no lift column the chart derives it from the two.
  • The diagonal (gain) or the line at 1 (lift) is random targeting.
# Cumulative gain and lift — Codesplash Chart Lab (recipe "lift-gain")
# controls: mode=gain
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

try:
    import codesplash as cs  # optional: the app's chart theme
except ImportError:
    cs = None
if cs:
    cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]

df = pd.read_csv("lift-gain-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["model"] = pd.Categorical(df["model"].astype("string"), categories=["Gradient boosting", "Random forest"])

def levels_of(s):
    """Declared level order (categoricals), else order of first appearance."""
    if isinstance(s.dtype, pd.CategoricalDtype):
        return [v for v in s.cat.categories if (s == v).any()]
    return list(dict.fromkeys(s.dropna().astype(str)))
F = "share of customers contacted"
GAIN = "share of churners reached"
LIFT = None
CURVE = "model"
MODE = "gain"  # "gain" (share of positives captured) or "lift" (× random)
groups = levels_of(df[CURVE]) if CURVE else [None]
series = []
for name in groups:
    d = df if name is None else df[df[CURVE].astype(str) == str(name)]
    series.append((name if name is not None else "", d.dropna(subset=[F]).sort_values([F], kind="mergesort")))
fig, ax = plt.subplots(figsize=(7, 4.8))
if MODE == "gain":
    ax.plot([0, 1], [0, 1], color="grey", linestyle="--", linewidth=1, label="random")
else:
    ax.axhline(1, color="grey", linestyle="--", linewidth=1, label="random")
for i, (name, d) in enumerate(series):
    if MODE == "gain":
        y = d[GAIN]
    else:
        d = d[d[F] > 0]
        y = d[LIFT] if LIFT else d[GAIN] / d[F]
    ax.plot(d[F], y, color=PALETTE[i % len(PALETTE)], linewidth=2, marker="o", ms=3, label=name or GAIN)
ax.set_xlabel(F)
ax.set_ylabel(GAIN if MODE == "gain" else "lift (× random)")
ax.legend(fontsize=8)
plt.show()

Needs pandas, numpy, matplotlib · reads lift-gain-demo.csv · app theme from the optional codesplash helper