Sub topic 1
Two Paths to the Same Model: Training and Serving
A deployed machine learning model usually uses two data paths. The training path prepares historical examples and fits the model. The serving path prepares new input and asks the model for a prediction. Both paths should create the same features in the same way.
This chapter assumes you know basic Python and pandas, understand the train-validate-predict workflow, and have seen a model exposed through an API or batch job.
Prerequisites
You should be comfortable with:
- DataFrames and missing values in pandas.
- The difference between a feature and a label.
- Fitting a model on historical data.
- Calling a model with new data.
The two paths
Suppose a model predicts whether a customer will cancel a subscription. During training, a batch job reads historical customer records, calculates features such as age and logins_30d, and fits a classifier.
In production, an API receives a request for one customer. A serving handler reads available fields, calculates the same features, and sends them to the classifier.
The model does not know where a feature came from. It only sees numbers or encoded categories. If age means years during training but months during serving, the request still looks valid to the software. The model can return a prediction that is confidently wrong.
A simple skew example
Training-serving skew is a difference between the features used during training and the features supplied during prediction. The difference can come from values, types, units, transformations, or timing.
Consider missing age values:
import pandas as pd
train = pd.DataFrame({"age": [30, 40, None, 50]})
train["age"] = train["age"].fillna(train["age"].mean())
def serve_features(request: dict) -> dict:
return {"age": request.get("age") or 0}
print(train["age"].tolist())
print(serve_features({"age": None}))The training path replaces the missing age with 40.0, the training mean. The serving path replaces it with 0. Both paths produce a numeric value, so no type or API error occurs. However, the model learned that a missing age behaves like an average customer and receives a value that represents a newborn or an invalid age.
Treat the paths as one contract
A feature contract describes what each model input means and how it is produced. For every feature, document:
- Name and data type.
- Unit and valid range.
- Missing-value rule.
- Source and refresh time.
- Transformation applied before prediction.
For example, age might be defined as “customer age in completed years, computed from the birth date available at request time; missing values use the median learned from training data.” The training and serving systems must both follow this definition.
A useful design goal is to make the paths visibly converge. The same transformation code, fitted preprocessing state, and validation rules should be reused wherever practical. Later chapters examine the main causes of divergence and the techniques that prevent it.
Exercise
A training job fills missing income with the training median, but the API fills it with zero. Is this training-serving skew, even if both outputs are numeric?
Show answer
Yes. The paths apply different missing-value rules, so the model receives features with different meanings.