Sub topic 2
What Training-Serving Skew Is and Why It Fails Silently
Prerequisites and the two data paths
This chapter assumes basic Python and pandas, familiarity with training and validating a supervised machine-learning model, and a general idea of serving predictions through an API or batch job. You should also know what a mean, histogram, and distribution are.
A machine-learning model usually uses two data paths:
- Training path: reads historical data, creates features, and fits the model.
- Serving path: reads new production data, creates features, and requests predictions.
Training-serving skew is a difference between the features produced by these paths for equivalent inputs. The model can still receive the expected column names and numeric types, so the request succeeds. However, the values no longer have the meaning the model learned.
Why skew fails silently
Suppose a churn model learns that age is measured in years and that missing ages are replaced with the training mean. If the serving handler replaces a missing age with zero, the model receives a valid number but an impossible age. No type check needs to fail. The prediction may simply become less accurate.
import pandas as pd
# Training path: the missing age becomes the training mean, 40.0.
train = pd.DataFrame({"age": [30, 40, None, 50]})
train["age"] = train["age"].fillna(train["age"].mean())
# Serving path: a different rule turns a missing age into 0.
def serve_features(request: dict) -> dict:
return {"age": request.get("age") or 0}
print(train["age"].tolist())
print(serve_features({"age": None}))The output is:
[30.0, 40.0, 40.0, 50.0]
{'age': 0}Both paths return a feature named age, and both values are numbers. Their meanings differ. This makes skew more dangerous than an obvious pipeline error: monitoring may show normal request rates, valid schemas, and successful predictions while model quality declines.
A useful working model
Treat every feature as a contract with at least four parts:
- Its name and data type.
- Its unit and semantic meaning.
- Its missing-value rule.
- Its time reference, such as “orders in the previous 30 days.”
A model learns from the complete contract, not only from a column name.
| Situation | Training feature | Serving feature | Likely result |
|---|---|---|---|
| Missing age | Training mean, 40 | Zero | Biased prediction |
| Income unit | Dollars | Cents | Extreme prediction |
| Recent logins | Data available at label time | Future events included | Leakage or unstable quality |
Skew can affect one feature, a group of features, or the entire input. Its impact depends on how strongly the model relies on the changed values. A small difference may be harmless for one model and damaging for another.
Exercise
Why can a missing-value bug create training-serving skew without causing an application error?
Show answer
Both paths can return a valid value with the expected name and type, while using different rules. The model receives valid software inputs with the wrong statistical meaning.