Sub topic 6
Cause 3: Different Data Sources, Schemas, and Missing-Value Handling
Assume you know basic Python, pandas, and the train-validate-predict workflow. You should also understand that a deployed model receives new feature values through a batch job or an API.
Training-serving skew can begin before the model runs. The training job may read a clean warehouse table, while production reads an event stream or request payload. Both paths may claim to provide age, but they can disagree about its type, unit, freshness, or missing-value meaning.
How the two paths diverge
A training dataset often has months of cleaned historical data. A serving system must handle incomplete and unexpected input immediately.
| Feature detail | Training path | Serving path | Possible result |
|---|---|---|---|
| Missing age | Replaced with the training median, 40 | Replaced with `0` | The model sees different meanings |
| Income unit | Dollars | Cents | Predictions use values 100 times too large |
| Country field | `country_code` such as `US` | Full name such as `United States` | Categories do not match |
A missing value is not always equivalent to zero. A missing age means “unknown,” while age zero means something else. If the training path uses one rule and the serving path uses another, the model can produce valid-looking but unreliable predictions.
import pandas as pd
# Historical data used during training
train = pd.DataFrame({"age": [30, 40, None, 50]})
training_median = train["age"].median()
train["age"] = train["age"].fillna(training_median)
# Request data received by the online service
request = {"age": None}
serving_age = request["age"] if request["age"] is not None else 0
print("Training values:", train["age"].tolist())
print("Serving value:", serving_age)This code runs without an error, but it sends 40.0 to the model during training and 0 during serving. That is a skew bug.
Find the boundary where meaning changes
Trace each feature from its source to the model:
Compare these properties:
- Column names and required fields.
- Data types, units, and allowed ranges.
- Time zones and timestamp formats.
- Null, empty-string, and missing-key behavior.
- Default values and category vocabularies.
- Data freshness and the event used to compute each feature.
Keep a feature contract beside the code. For example, income might require a nonnegative floating-point value in dollars, while age might allow null input but require the same median-imputation rule in both paths.
Prevent source and schema skew
Use one transformation implementation whenever possible. Fit statistics such as medians and category vocabularies on training data, save them, and reuse them at serving time. Validate raw requests before transformation, then record the transformed features that the model actually received.
A parity test should send identical raw events through the offline and online transformations. It should compare every output feature, including values created from missing inputs.
Exercise
A training table fills missing income with the median, but the API fills it with zero. What should you investigate first?
Show answer
Confirm that both paths use the same feature contract and transformation. Then replace the duplicated serving rule with the fitted training transformation, and add a parity test for missing income.