Start here

Run it

Two tables. census-picks.csv holds 3,000 people that the census model of ml-fairness never learned from: each person's age, whether they earned over $50,000, and the model's chance that they did. bank-months.csv holds the bank's calls in the order they were made: 200 from each of 2008's five busy months, and 60 from each month after.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

The bank's model of the earlier lessons: the twelve columns known before a call, prepared in one pipeline, then logistic regression.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

1One score for everyone

Run it

From Bias and fairness

The census model picks a person when its chance is one half or more. Run the cell: it prints how often its answer, picked or not, is right, over everyone and then for each age group.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

The under 30s look best of all. Most of them earn less, so "no" is right for most of them. The next step counts each kind of mistake.

2Each group apart

Your turn

From Bias and fairness

Write counts(rows). For a table with a truth column and a pick column, it gives back four numbers: the share picked, recall, precision and the false alarms. The cell then counts each age group apart.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

Count the right picks once: a pick that is also true. Recall divides them by the true ones, precision by the picks. False alarms are the picks that are not true, out of all that are not true.

One way to do it
def counts(rows):
    right_picks = (rows.pick & rows.truth).sum()
    return pd.Series({
        "picked": rows.pick.mean(),
        "recall": right_picks / rows.truth.sum(),
        "precision": right_picks / rows.pick.sum(),
        "false alarms": (rows.pick & ~rows.truth).sum() / (~rows.truth).sum(),
    })

by_group = people.groupby("group").apply(counts)
by_group.round(3)

3A line of its own

On your own

From Bias and fairness

On your own now. Say a pick gives people something, like an offer. Then the groups' recall should match. Give the under 30s their own line: the highest one, in steps of 0.01 down from 0.5, that finds as many of their high earners as the 30 to 59s' recall at one half. Keep it in young_line, and their counts at that line in young_counts. What fell?

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

Take the under 30s alone. Walk the line down from 0.5 in a loop, count with counts at each line, and stop at the first whose recall reaches the target.

One way to do it
young = people[people.group == "under 30"]
target = by_group.loc["30 to 59", "recall"]
for young_line in np.arange(0.5, 0, -0.01).round(2):
    young_counts = counts(young.assign(pick=young.chance >= young_line))
    if young_counts.recall >= target:
        break
print(young_line)
print(young_counts.round(3))

4The bank's model, month by month

Run it

From Drift

Now a model in use. The cell learns the bank's model from the calls of 2008, keeps it as it is, and gives every call its chance. Then it compares the model's average chance with the share who said yes, month by month.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

In 2010, more than half say yes, and the model still says two in a hundred. The video's model said about five: it learned from all 27,690 calls of 2008, and this one from 1,000.

5Each input's own line

Your turn

From Drift

The first cell puts the numbers in bands, as the video's code did. Write bars(months, col): the share of each value of col in those months' calls, from bands. Write moved(a, b): half the sum of the gaps between two sets of bars. Then each input gets its line, and the cell finds the first month one crosses.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

The video found housing too: with 60 calls a month, the bars wobble more, so the lines sit higher.

Hint

value_counts(normalize=True) gives the shares. For the gaps, a.sub(b, fill_value=0) counts a value one set lacks as 0. Take them without their sign, add them up, and halve it.

One way to do it
def bars(months, col):
    rows = bands[bands.month.isin(months)]
    return rows[col].value_counts(normalize=True)

def moved(a, b):
    return a.sub(b, fill_value=0).abs().sum() / 2

line = {c: max(moved(bars(learned, c), bars([m], c)) for m in learned) + 0.05 for c in inputs}
past = {m: [c for c in inputs if moved(bars(learned, c), bars([m], c)) > line[c]] for m in later}
first = next(m for m in later if past[m])
print(first, past[first])

6Learn it again

On your own

From Drift

On your own. For each month after 2008, learn a new bank_model() from the calls of the three months before it, and give that month's calls its chance, in a new column of calls, fresh. Then put each month's average of fresh beside said_yes and saved in watch, and look.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

enumerate(months) gives each month's place, so months[k - 3:k] is the three months before it. Fit on those calls, then predict_proba on this month's, and keep the second column.

One way to do it
for k, month in enumerate(months):
    if month in later:
        last_three = calls[calls.month.isin(months[k - 3:k])]
        now = calls.month == month
        model = bank_model().fit(last_three[inputs], last_three.yes)
        calls.loc[now, "fresh"] = model.predict_proba(calls.loc[now, inputs])[:, 1]
watch["fresh"] = calls.groupby("month", sort=False).fresh.mean()
watch.plot(rot=90)
print(watch.loc[later].round(2).tail(4))

7A check-up by group, in 2010

On your own

From Bias and fairness and Drift

On your own. Score the model learned again on the calls of 2010, for each age group: pick a call when its fresh chance is one half or more, and use counts with the calls' truth and pick. Keep the table in by_age_2010, one row a group.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

Make the truth and pick columns with assign, then group by age_group of the calls' ages, as the census people were grouped, and apply counts.

One way to do it
in_2010 = calls[calls.month >= "2010"]
rows = in_2010.assign(truth=in_2010.yes == 1, pick=in_2010.fresh >= 0.5)
by_age_2010 = rows.groupby(age_group(rows.age)).apply(counts)
print(by_age_2010.round(2))
print("The saved model picks", (in_2010.saved >= 0.5).sum(), "calls of 2010.")

Run on Pyodide 314, Python 3.14, pandas 3.0, matplotlib 3.10 and scikit-learn 1.8. Checked on .