Sub topic 4
Cause 1: The Same Feature Written Twice
Assume you know basic Python, pandas, and the train-validate-predict workflow. You should also know that a feature is an input value calculated from raw data.
How duplicated feature logic creates skew
A model usually has two data paths:
- The training path reads historical data and creates features for model fitting.
- The serving path reads a new request and creates features for prediction.
Training-serving skew occurs when these paths calculate the same feature differently. The model still receives valid numbers, so the error may not raise an exception. However, the model was fitted on one meaning of a feature and receives another meaning in production.
For example, suppose age is missing. The training code fills missing ages with the training mean, which is 40. A separately written serving handler fills missing ages with 0.
import pandas as pd
# Training path: fill missing ages with the training mean.
train = pd.DataFrame({"age": [30, 40, None, 50]})
train["age"] = train["age"].fillna(train["age"].mean())
# Serving path: a separately written rule.
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}The model sees 40 for a missing age during training but 0 during serving. If age affects the prediction, the result can be wrong even though the API request succeeds.
What can differ
Duplicated code often drifts in small ways:
- One path uses days while the other uses seconds.
- One path lowercases a country code while the other preserves capitalization.
- One path treats an empty string as missing while the other keeps it.
- One path calculates a 30-day count with an inclusive end date while the other excludes it.
- One path uses a newer column name or data source.
These differences are especially dangerous because each implementation may look reasonable by itself.
Trace the two paths
Map each raw input to the feature value that reaches the model. A simple diagram makes duplicated logic visible:
The safest correction is to define the transformation once and call it from both paths. The shared function should describe the feature contract. Any statistics it needs, such as a mean, must be learned from training data and passed into the function rather than recomputed from a request.
def transform_age(age, training_mean: float) -> float:
if age is None:
return training_mean
return float(age)
training_mean = 40.0
print(transform_age(None, training_mean)) # 40.0In a larger system, keep this transformation in a shared package or bundle it with the model's preprocessing pipeline. Test both paths with the same raw examples, including missing, empty, boundary, and unusual values.
Exercise
A training job converts distances from meters to kilometers, but the API handler sends meters directly to the model. What kind of problem is this, and what is the first fix?
Show answer
It is training-serving skew caused by duplicated feature logic. First define one shared conversion and use it in both paths, then add a parity test.