Same model, same user, different scores online. Where's the skew?
Question
A churn model gives each user a risk score. When the training data was built, user u3 scored 0.97. The online service loads the same model file but returns 0.27 for u3. Other users match.
The training data came from a batch job. The service rebuilds the features in its own code. Both functions are below, with u3's raw record.
Find the cause. How would you find it on real traffic without reading the code? How do you stop it from happening again?
def train_features(raw): # batch job that built the training data days = raw["days"] return {"days_since_order": 90 if days is None else days, "orders_90d": raw["orders"]} def serve_features(raw): # rewritten by hand in the online service return {"days_since_order": raw["days"] or 0, "orders_90d": raw["orders"]} u3 = {"days": None, "orders": 0} # raw record: u3 never ordered
Show hints (3)
- "RUN U3 TWICE"
Pass u3's record to both functions. Which feature differs?
- "WHAT 0 MEANS"
To the model,
days_since_order= 0 means "ordered today". Is that a high or a low churn risk? And what did a missing value become in training? - "COMPARE LOGS"
Log the features the service used for each request. Rebuild them with the training code for the same requests, and list the features that differ.
Show checked explanation
Training and serving built the features with different code. For a user who never ordered, the batch job set days_since_order to 90, but the service's or 0 set it to 0. So "never ordered" became "ordered today", and the score fell from 0.97 to 0.27. Share one feature function, log served features and compare them with offline ones.
from math import exp def train_features(raw): # batch job, as in the task days = raw["days"] return {"days_since_order": 90 if days is None else days, "orders_90d": raw["orders"]} def serve_features(raw): # online service, as in the task return {"days_since_order": raw["days"] or 0, "orders_90d": raw["orders"]} WEIGHTS = {"days_since_order": 0.05, "orders_90d": -0.4} # intercept -1.0 for user, days, orders in [("u1", 3, 5), ("u2", 40, 1), ("u3", None, 0)]: raw = {"days": days, "orders": orders} pair = train_features(raw), serve_features(raw) p = [1 / (1 + exp(1.0 - sum(WEIGHTS[k] * f[k] for k in f))) for f in pair] diff = [k for k in pair[0] if pair[0][k] != pair[1][k]] print(user, f"{p[0]:.2f} vs {p[1]:.2f}", diff)
u1 0.05 vs 0.05 [] u2 0.65 vs 0.65 [] u3 0.97 vs 0.27 ['days_since_order']
Code run on CPython 3.12 against the example above. Checked by an editor other than the author.
Walkthrough
This is training-serving skew: for the same user, the model gets different inputs online than in training. The model file is fine. The features are not.
The two functions agree for u1 and u2. They differ only when the raw days value is missing. raw["days"] or 0 turns None into 0. In training, missing meant 90 days, and the model learned that as high risk. Online, 0 days means "ordered today", which looks loyal:
| u3 | days_since_order | score |
|---|---|---|
| training | 90 | 0.97 |
| online | 0 | 0.27 |
The solution finds the cause by comparing outputs, not by reading code. On real traffic, the service logs the exact features it used. Later you rebuild them for the same requests with the training code and compare them feature by feature. Here that names days_since_order, and only for users who never ordered. Track the share of requests where each feature differs, and alert when it grows.
Missing values are one cause. Others show up the same way:
- units: seconds in training, milliseconds online;
- categories: a new country mapped to "other" in training but to code 0 online;
- stale features: fresh values in training, a value from yesterday's batch online;
- preprocessing: text lowercased in training but not online.
The lasting fix is one feature function, or a feature store, used by both the training job and the service. Then the two paths can't drift apart. Keep logging and comparing anyway. Shared code doesn't catch stale data or a broken upstream source.
Common mistakes
- Retraining the model to fix it. The model is fine. A new model trained on the same features meets the same wrong inputs online.
- Comparing only average scores. Few users never ordered, so the average online score barely moves. Compare features per request.
- Using
orfor defaults.days or 90would also turn a real 0 into 90. Test forNoneexplicitly. - Testing only typical users. Skew hides in edge cases: missing values, new categories, zeros. Keep a few such records as fixed tests for both paths.
Follow-up questions
- "How can training data avoid skew from the start?" Train on the features the service logged, not on features rebuilt later. Then training sees exactly what serving saw, including its defaults and delays. The cost: a new feature must be logged for a while before you can train on it.
- "How do you test a new model for skew before launch?" Run it in shadow mode: it scores real requests, but its scores are not used. Log its features and scores, and compare them with the offline pipeline for the same requests. Launch when they match.
What a strong answer covers
Says the model is fine and the inputs differ. Finds the cause by comparing logged served features with offline features for the same requests. Names typical causes: defaults for missing values, units, category mappings, stale data. Fixes it with shared feature code and keeps monitoring.
Open in interactive practice deck →