Sub topic 3
Defining Entities, Labels, and the Prediction Timestamp
Assume you know basic supervised machine learning, pandas DataFrames, SQL joins, window functions, and timestamps with time zones. This chapter defines the row structure that makes a training example point-in-time correct.
Start with the prediction question
A training row represents one prediction that could have been made in the past. Before building features, write the prediction question precisely:
- Who or what receives the prediction?
- When is the prediction made?
- What outcome will be measured?
- How long after prediction time will that outcome be observed?
For example:
> For customer C101, at 2026-01-10 09:00 UTC, will the customer churn during the next 30 days?
This question defines the training row:
| entity_id | prediction_ts | label | Label meaning |
|---|---|---|---|
| C101 | 2026-01-10 09:00 UTC | 1 | Customer churned by 2026-02-09 09:00 UTC |
The label is the answer to the prediction question. It is not a feature. At prediction time, the label is usually unknown. It becomes known only after the outcome window ends.
Define the entity and row grain
An entity is the object for which the model makes a prediction. Common entities include:
- A customer
- A bank transaction
- A device
- An insurance claim
- An account at a particular point in time
The row grain states what one row means. For this example, the grain is:
> One customer at one prediction timestamp.
The entity identifier alone is not enough. A customer may appear in many rows because the system may make predictions repeatedly.
| customer_id | prediction_ts | churned |
|---|---|---|
| C101 | 2026-01-10 09:00 UTC | 1 |
| C101 | 2026-02-10 09:00 UTC | 0 |
| C205 | 2026-01-10 09:00 UTC | 0 |
Each row is a separate prediction opportunity. The pair (customer_id, prediction_ts) should identify one label row. If that pair appears twice, a later join can duplicate examples and distort evaluation.
Separate prediction time from label time
The prediction timestamp is the cutoff for information that features may use. A feature is valid only if it was available by that time.
The label has a different time boundary. Suppose the horizon is 30 days:
- Set
prediction_tsto the time the prediction would be made. - Set
label_end_tstoprediction_ts + 30 days. - Set the label to 1 if the outcome occurs during that interval.
- Do not use events after
prediction_tsas features, even if they help determine the label.
For churn, the label could be defined as:
import pandas as pd
labels = pd.DataFrame({
"customer_id": ["C101", "C205"],
"prediction_ts": pd.to_datetime([
"2026-01-10T09:00:00Z",
"2026-01-10T09:00:00Z",
]),
"churned": [1, 0],
})
horizon = pd.Timedelta(days=30)
labels["label_end_ts"] = labels["prediction_ts"] + horizon
assert labels[["customer_id", "prediction_ts"]].duplicated().sum() == 0
assert (labels["label_end_ts"] > labels["prediction_ts"]).all()The assertion checks the intended row grain. The second check ensures that the outcome window starts after prediction time.
Make time zones explicit
Store timestamps with an explicit time zone, usually UTC. Do not compare a timezone-aware timestamp with a timezone-naive timestamp. A silent conversion can move an event across the prediction boundary.
A useful mental model is:
The model may use information before point B. The label is calculated using what happens between points B and D. Training code must keep those roles separate.
Exercise
A model predicts whether an order will be refunded within seven days. What should the prediction timestamp and label window represent?
Show answer
The prediction timestamp is when the order is eligible for the prediction. The label window is the following seven days, and the label is 1 only if a refund occurs during that interval.