Sub topic 7
Telling Real Slice Gaps from Noise
Assume you know Python, pandas, NumPy, accuracy, precision, recall, and F1. This chapter assumes you already have predictions on a held-out test set. A slice is a subgroup of examples defined by a property, such as text length, language, product type, or presence of negation.
Why slice scores can mislead
Suppose short texts have 90% accuracy and long texts have 75%. That gap may indicate a real weakness. It may also come from a small number of examples. If the long-text slice contains only eight items, one changed prediction moves its accuracy by 12.5 percentage points.
Always report the score with the slice size:
| Slice | Correct | Total | Accuracy |
|---|---|---|---|
| Short text | 90 | 100 | 0.90 |
| Long text | 6 | 8 | 0.75 |
A small slice gives an uncertain estimate of the model's performance in that subgroup. A large slice usually gives a more stable estimate, although a large biased test set can still produce a misleading result.
Use bootstrap intervals
A confidence interval gives a range of plausible values for a metric. The bootstrap estimates this range by repeatedly sampling examples from the slice with replacement.
For accuracy, each example contributes either 1 for correct or 0 for incorrect. The bootstrap resamples these values and recalculates the mean:
import numpy as np
rng = np.random.default_rng(0)
correct = np.array([1, 1, 0, 1, 0, 1, 1, 0, 1, 1])
boot = [
rng.choice(correct, size=len(correct), replace=True).mean()
for _ in range(2000)
]
low, high = np.percentile(boot, [2.5, 97.5])
print(f"accuracy={correct.mean():.2f}")
print(f"95% bootstrap CI=({low:.2f}, {high:.2f})")The interval is not a guarantee that the true accuracy lies inside it. It describes uncertainty under the assumption that the observed slice represents the population you care about.
For a difference between slices, bootstrap both slices in the same loop and store the difference between their sampled accuracies. If the interval for the difference includes zero, the observed gap is compatible with no difference. If it stays far from zero, the gap deserves more attention.
Compare slices carefully
Use the same evaluation rules, preprocessing, label definitions, and model version for every slice. Then follow this process:
- Record the metric and support, which is the number of examples.
- Compute an interval for each important slice or for the difference.
- Read the examples in both slices, especially confident errors.
- Repeat the evaluation on another test sample or a later time period.
- Treat tiny slices as exploratory evidence.
Testing many properties creates another problem. If you inspect dozens of slices, some will look unusually good or bad by chance. Preselect important slices, record all comparisons, and avoid presenting the most extreme result as if it were the only test.
Avoid common errors
Do not compare overlapping slices as though they were independent groups. Do not infer a cause from a correlation, such as blaming text length when long texts also contain more named entities. Do not hide the count behind a percentage. A 50% error rate from two examples is less actionable than a 20% error rate from 1,000 examples.
Exercise
A slice has accuracy 0.80 on five examples, while the full test set has accuracy 0.90. What should you do first?
Show answer
Report the slice count, inspect its examples and labels, and estimate uncertainty. Do not conclude that the model has a reliable slice-specific weakness from five examples.