Sub topic 13
Reproducing a Training Set Months Later
Assume you know supervised learning, SQL joins, pandas, and timestamp handling. This chapter focuses on the difference between rerunning a training query and reproducing the exact dataset used before.
A training set is reproducible when the same source versions, time rules, code, and parameters produce the same rows and values later. Re-running a query against today’s feature table is often not enough. The table may contain corrected events, deleted records, changed feature logic, or newly arrived data.
What to version
Store a manifest with every training run. The manifest identifies the label data, feature-history data, query, code, and configuration.
| Item | Why it matters | Example |
|---|---|---|
| Label snapshot | Keeps outcomes and entity rows stable | `labels_2026_02_01` |
| Feature-history version | Preserves historical feature values | `features_v17` |
| Query or transformation code | Preserves join and filtering rules | Git commit `a81c2f4` |
| Parameters | Preserves cutoff and TTL choices | cutoff `2026-01-01`, TTL `30D` |
| Environment | Prevents dependency changes from altering results | Python 3.12, pandas 2.2 |
Feature history should be append-only when possible. An append-only table does not overwrite an old feature observation. Each row includes the entity, feature value, event time, and ingestion time. A new correction becomes a new version or a new recorded observation.
A run manifest can be a small JSON document:
import json
from pathlib import Path
manifest = {
"dataset_id": "churn_train_2026_02_01",
"labels_version": "labels_2026_02_01",
"features_version": "features_v17",
"query_file": "build_training_set.sql",
"git_commit": "a81c2f4",
"prediction_cutoff": "2026-01-01T00:00:00Z",
"feature_ttl": "30D",
"timezone": "UTC",
}
Path("training_manifest.json").write_text(
json.dumps(manifest, indent=2) + "\n",
encoding="utf-8",
)
print("Wrote training_manifest.json")The manifest does not store the data itself. It points to immutable snapshots, object versions, table snapshots, or other storage that prevents accidental replacement. If your storage system cannot provide immutable versions, export the final training set to durable storage and record its checksum.
Re-run versus reproduce
A re-run asks, “What does the current pipeline produce?” A reproduction asks, “What did the pipeline produce from the same inputs at that time?”
The difference matters when a late event arrives. Suppose a customer event occurred on January 10 but was ingested on January 20. A training run created on January 15 must not use that event if the pipeline filters by ingestion time. A later run against an unversioned table might include it and produce a different row.
Use the same point-in-time rules during reproduction:
- Read the same label snapshot.
- Read the same feature-history version.
- Apply
event_ts <= prediction_ts. - Apply
ingested_ts <= prediction_tswhen modeling system knowledge. - Apply the same freshness tolerance.
- Use the same time zone and cutoff boundaries.
- Record the output row count and checksum.
Verify the result
A checksum detects changes that are easy to miss in a visual inspection. Sort rows by stable keys, serialize selected columns, and hash the bytes.
import hashlib
import pandas as pd
def training_checksum(df: pd.DataFrame) -> str:
keys = ["customer_id", "prediction_ts"]
columns = keys + ["churned", "avg_spend_30d"]
ordered = df.sort_values(keys)[columns].copy()
text = ordered.to_csv(
index=False,
lineterminator="\n",
date_format="%Y-%m-%dT%H:%M:%S%z",
)
return hashlib.sha256(text.encode("utf-8")).hexdigest()
expected = "replace-with-the-original-checksum"
actual = training_checksum(train)
if actual != expected:
raise ValueError(f"Training set changed: {actual}")A checksum proves that the selected representation matches. It does not explain why two versions differ. Also record useful metadata such as row count, null counts, minimum and maximum prediction time, and feature-history version.
Exercise
A later reproduction has the same row count but a different checksum. Name two likely causes and one useful diagnostic.
Show answer
A feature snapshot or query version may differ, or timestamp and time-zone handling may have changed. Compare the manifests first, then compute row-level differences after sorting by the entity and prediction timestamp.