Sub topic 12
Spending a Labeling Budget Where It Helps Most
Assume you know binary classification metrics, can work with pandas, and have a prediction log containing scores and features. This chapter focuses on production data whose labels are delayed or missing.
When labels arrive late, you may be able to label only a small sample manually. The sampling strategy affects both what you learn and whether your estimate is trustworthy. A sample chosen only from unusual or uncertain cases can improve debugging, but it cannot directly represent overall accuracy unless you account for the sampling design.
Start with a representative sample
A random sample gives an approximately unbiased estimate of the current error rate. If you sample 1,000 predictions uniformly and label them, the fraction of errors estimates production error for the sampled period.
Random sampling may waste labels when errors are rare. It may also miss important groups, such as a new country, device type, or high-value customer segment. Stratified sampling divides predictions into groups and samples from each group. You can then combine the results with weights based on each group's production frequency.
| Strategy | Best use | Main risk |
|---|---|---|
| Random sampling | Estimate overall performance | Few examples of rare failures |
| Stratified sampling | Compare known segments | Incorrect weighting |
| Uncertainty sampling | Find boundary errors | Biased metric estimate |
| Drift-focused sampling | Investigate a changed population | Misses stable populations |
Combine estimation and investigation
A practical labeling batch often has two parts:
- An estimation sample, selected randomly or with known strata, for unbiased metrics.
- An investigation sample, selected for high uncertainty, drift, high business cost, or suspected failures.
Keep these purposes separate. The investigation sample tells you where to look. It should not be treated as an ordinary accuracy sample.
For stratified sampling, calculate each stratum's error rate and multiply it by that stratum's share of production:
\widehat{error} = \sum_s P(s)\widehat{error}_sIf you sample 500 predictions from a segment that represents 5% of traffic, its measured error must be weighted by 0.05, not by its 50% share of the labeled batch.
import numpy as np
import pandas as pd
def weighted_error_rate(labeled, stratum_col="segment"):
"""Estimate production error from a stratified labeled sample.
Required columns: segment, is_error, production_share.
production_share is the fraction of all predictions in each segment.
"""
by_stratum = (
labeled.groupby(stratum_col)
.agg(sample_error=("is_error", "mean"),
production_share=("production_share", "first"))
)
if not np.isclose(by_stratum["production_share"].sum(), 1.0):
raise ValueError("Production shares must sum to 1.")
return float(
(by_stratum["sample_error"] * by_stratum["production_share"]).sum()
)
labeled = pd.DataFrame({
"segment": ["old", "old", "new", "new", "new"],
"is_error": [0, 1, 1, 0, 1],
"production_share": [0.8, 0.8, 0.2, 0.2, 0.2],
})
print(weighted_error_rate(labeled))Allocate labels deliberately
Use uncertainty sampling when the goal is to improve the model or discover boundary mistakes. For a binary classifier, scores near the decision threshold are often informative. Also reserve labels for:
- New or drifting feature distributions.
- High-cost decisions.
- Rare but severe outcomes.
- Segments with poor historical performance.
- A random holdout for unbiased monitoring.
Do not spend the entire budget on high-score or low-score cases. That can hide failures in the middle or make the measured error look worse than production reality.
Use delayed labels to improve future batches
When manually acquired labels eventually receive their natural ground truth, compare the manual label with the final outcome. Measure disagreement by segment and sampling strategy. A useful policy is to maintain a small random sample every period, then adjust the remaining budget toward uncertainty and drift.
Exercise
Exercise
A segment makes up 10% of traffic. In a targeted batch, 40% of labels come from that segment, and its observed error rate is 25%. Should you report 25% as the production error rate for the segment?
Show answer
Yes for that segment's estimated error rate, if the labels within the segment are representative. Do not multiply the overall batch error by 40%. For the overall production estimate, weight this segment's rate by its 10% production share and combine it with the other segments.