Train-Serving Skew: Spotting Mismatched Features

MLOpsMedium+15 XP+15

Train-Serving Skew: Spotting Mismatched Features

A model is trained on one view of the data and served another. The feature pipelines are supposed to match — but one team patches a unit conversion, or a currency field changes meaning, and suddenly the model is being fed numbers unlike anything it trained on. Accuracy falls and nothing has errored. This is train-serving skew, and you catch it by comparing feature statistics on both sides.

Task: write skew_report(train_stats, serve_stats, threshold) returning the sorted list of feature names that look wrong.

Both arguments are dictionaries mapping a feature name to its mean. Flag a feature if any of these holds:

  • It appears in train_stats but is missing from serve_stats — the model needs a feature serving isn't producing.
  • It appears in serve_stats but not in train_stats — serving is producing something the model never saw.
  • Its mean moved by more than threshold, measured relatively:
∣serve−train∣∣train∣>threshold\frac{|serve - train|}{|train|} > threshold∣train∣∣serve−train∣​>threshold
  • Training means are never zero, so the division is safe.
  • Use abs on the bottom too, so negative-valued features behave the same way as positive ones.
  • Return names sorted alphabetically, with no duplicates, and an empty list when everything is fine.
  • The comparison is strictly greater than: a feature that moved by exactly the threshold is not flagged.

The relative comparison is what makes one threshold work across every feature at once. A mean shifting from 30 to 31 is a shrug; the same absolute shift on a feature whose mean is 0.5 would be a catastrophe. Dividing by the training mean puts age in years and income in dollars on the same footing.

A live customer spends $40 a month. How does the app prepare that row?

What the model reads (training units)
averagetraininglive$10$15$20$30$40+40, far off the chart$40−2−10+1+2
(40 − 23) ÷ 10.77 = 1.58
training mean 23, training standard deviation 10.77
App log: ✓ no errorsModel reads it as $40 a month

Well above average, as the model learned it. The live customer lands exactly on the $40 training customer.

Examples

train_stats = {'age': 30.0, 'income': 50000.0}
serve_stats = {'age': 31.0, 'income': 80000.0}
threshold = 0.1
returns ['income']
train_stats = {'a': 1.0, 'b': 2.0}
serve_stats = {'a': 1.0}
threshold = 0.5
returns ['b']

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