Sub topic 2
Documenting Training Data
Prerequisites
This chapter assumes you know train, validation, and test splits, accuracy and F1 score, and basic Python. You should also be comfortable reading Markdown.
What to record about training data
Training data is the set of examples a model learns from. Its model card should state where the data came from, when it was collected, how many examples it contains, and how labels were assigned. Record any filtering, anonymization, or known gaps. Without this provenance, a reader cannot judge whether the model fits their own population.
Choosing and reporting evaluation data and metrics
Evaluate on data the model never saw during training. Report the source and size of the test set, and say whether it resembles production traffic. Choose metrics that match the cost of errors. Accuracy can look high on imbalanced classes, so report precision, recall, or F1 alongside it, and state the decision threshold used.
Disaggregated evaluation across groups and conditions
An overall score can hide failures on a subgroup. Compute metrics per group, such as language, region, or ticket length. Also report the number of examples in each group, because a score based on three examples is weak evidence.
from collections import defaultdict
def accuracy_by_group(y_true, y_pred, groups):
correct = defaultdict(int)
total = defaultdict(int)
for truth, pred, group in zip(y_true, y_pred, groups):
total[group] += 1
correct[group] += int(truth == pred)
return {group: correct[group] / total[group] for group in total}
y_true = [1, 0, 1, 1, 0, 0]
y_pred = [1, 0, 1, 1, 1, 1]
groups = ["a", "a", "a", "b", "b", "b"]
overall = sum(t == p for t, p in zip(y_true, y_pred)) / len(y_true)
print(f"overall: {overall:.2f}")
for group, acc in accuracy_by_group(y_true, y_pred, groups).items():
print(f"group {group}: {acc:.2f}")The output shows an overall accuracy of 0.67, but group b scores 0.33.
| Group | Examples | Accuracy |
|---|---|---|
| a | 3 | 1.00 |
| b | 3 | 0.33 |
| Overall | 6 | 0.67 |
Exercise
A model scores 0.90 overall on 1,000 tickets. Group X has 980 tickets with accuracy 0.92, and group Y has 20 tickets with accuracy 0.00. What should the model card report, and why?
Show answer
Report the overall score, the per-group accuracy, and the group sizes. The overall 0.90 hides that group Y, with 20 examples, is always wrong. The card should flag this failure and state how many examples support the estimate.