Before the Monday Emails Go Out

MLOpsMedium+20 XP+20

Before the Monday Emails Go Out

Every Sunday night, a phone company's batch job loads its saved pipeline and scores every customer's chance of leaving. The scores land in a table, and on Monday morning an offer email goes to the customers most likely to leave. Nobody watches the job run, so before any email goes out, a quick health check reads the night's scores.

scores is the night's column: one predicted probability of leaving per customer. Some rows can be broken. The pipeline writes None when it fails on a row, NaN (float('nan')) when a calculation goes wrong, and occasionally a number outside 0 to 1. A score is valid when it is a number, not NaN, between 0 and 1 inclusive.

A prediction's confidence is the probability the model gives to the answer it picked. For a score ppp, the picked answer is "leave" when p≥0.5p \ge 0.5p≥0.5 and "stay" otherwise, so its confidence is

max⁡(p, 1−p)\max(p,\ 1 - p)max(p, 1−p)

Task: write batch_health(scores, cutoff) returning a dict with these five keys:

keyvalue
"invalid_rate"share of all rows that are invalid
"low"smallest valid score
"high"largest valid score
"mean_confidence"average confidence over the valid scores
"unsure_rate"share of the valid scores whose confidence is below cutoff

Round every number to 4 decimal places. If no score is valid, the last four values are None. scores always has at least one row.

Examples

scores = [0.91, 0.12, None, 0.55, 0.47, 0.08, 0.73, 1.3]
cutoff = 0.7
returns {'invalid_rate': 0.25, 'low': 0.08, 'high': 0.91, 'mean_confidence': 0.7533, 'unsure_rate': 0.3333}
scores = [0.15, float('nan'), 0.65, -0.1, 0.99, 0.35]
cutoff = 0.8
returns {'invalid_rate': 0.3333, 'low': 0.15, 'high': 0.99, 'mean_confidence': 0.785, 'unsure_rate': 0.5}

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run