The Weeks the Model Fell Behind

MLOpsMedium+20 XP+20

The Weeks the Model Fell Behind

A food-delivery app predicts how many minutes each delivery will take, mostly from the trip's distance in km. Each prediction is logged when the order is placed. The true minutes arrive as soon as the food does, so the team can score the model on recent, labelled deliveries.

They take the recent deliveries, oldest first, and cut them into consecutive, non-overlapping blocks of window deliveries, about one week each. Every full block gets a verdict.

1. Score the block on its own. R² compares the model's squared misses with those of always guessing the block's own average:

R2=1−∑(y−y^)2∑(y−yˉ)2R^2 = 1 - \frac{\sum (y - \hat y)^2}{\sum (y - \bar y)^2}R2=1−∑(y−yˉ​)2∑(y−y^​)2​

The sums run over the block's deliveries, yyy is the true minutes, y^\hat yy^​ the prediction, and yˉ\bar yyˉ​ the average true minutes in that block. Report R² as it comes out, even when it's negative.

2. Check the block's inputs. As a simple stand-in for the decile check, compare the block's average distance with the training distances train_x:

shift=block’s mean distance−training meantraining standard deviation\text{shift} = \frac{\text{block's mean distance} - \text{training mean}}{\text{training standard deviation}}shift=training standard deviationblock’s mean distance−training mean​

using the population standard deviation of train_x (divide by its length).

3. Give the verdict.

  • "healthy" if R² is at least r2_floor, whatever the inputs did.
  • Otherwise "concept drift" if ∣shift∣≤0.5|\text{shift}| \le 0.5∣shift∣≤0.5: the distances still look like training, yet the predictions got worse.
  • Otherwise "inputs moved".

Task: write weekly_scorecard(train_x, x, y_true, y_pred, window, r2_floor). x, y_true and y_pred line up: one distance, one true time and one prediction per recent delivery.

Return a list with one tuple (r2, shift, verdict) per full block, in order, with r2 and shift rounded to 4 decimal places. Leftover deliveries at the end that don't fill a whole block aren't scored yet. You may assume no block has all its true times equal.

100 purchases
onlinein store
Chance of fraud
0%2%4%6%
5%
1%
onlinein store
Online
20%
what the model learned from
Fraud per 1,000 purchases
18
10 online + 8 in store
Labels:
Already known

The model learned from this year, and its labels are already known. Pick a kind of drift to see a later year.

Dashed lines mark the training year's chances of fraud. Labels say which purchases really were fraud.

Examples

train_x = [1.5, 2.0, 3.5, 4.0, 2.5, 5.0, 3.0, 4.5, 2.0, 3.0]
x = [2.0, 3.5, 4.0, 3.0, 3.0, 2.5, 4.5, 2.0, 3.5, 2.0, 4.0, 3.5, 3.0, 2.5]
y_true = [16, 19, 23, 18, 18, 18, 24, 15, 29, 17, 26, 31, 30, 25]
y_pred = [15.0, 20.25, 22.0, 18.5, 18.5, 16.75, 23.75, 15.0, 20.25, 15.0, 22.0, 20.25, 18.5, 16.75]
window = 4
r2_floor = 0.5
returns [(0.8534, 0.0229, 'healthy'), (0.9561, -0.0917, 'healthy'), (-0.8486, 0.1375, 'concept drift')]
train_x = [1.5, 2.0, 3.5, 4.0, 2.5, 5.0, 3.0, 4.5, 2.0, 3.0]
x = [3.0, 2.0, 4.0, 3.5, 6.0, 7.5, 5.5, 8.0, 7.0, 6.5, 8.5, 6.0]
y_true = [19, 15, 22, 21, 29, 34, 27, 37, 40, 31, 33, 45]
y_pred = [18.5, 15.0, 22.0, 20.25, 29.0, 34.25, 27.25, 36.0, 32.5, 30.75, 37.75, 29.0]
window = 4
r2_floor = 0.5
returns [(0.9717, 0.0229, 'healthy'), (0.9821, 3.3459, 'healthy'), (-1.6844, 3.5751, 'inputs moved')]

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