AI & Machine Learning Master Course — 069. Slice-based evaluation and robustness
Smartyfy AI & Machine Learning Master Course
Course ID: smartyfy-ai-ml-master-200
Lesson 069: Slice-based evaluation and robustness
Prerequisites: Lesson 068. Learning outcome: Measure performance across relevant conditions with adequate sample-size caveats.
1. Aggregate performance can hide a weak operating condition
A parcel-image model may work well under normal lighting and poorly on a new camera. An overall score averages across the conditions present in the evaluation sample. If the difficult condition is rare, its failures can be nearly invisible in that average even though they matter greatly after deployment changes.
Slice-based evaluation examines subsets defined by relevant conditions: lighting, device type, document length, language, time period, missingness, or other task-specific factors. Choose slices because they represent plausible operational differences or known risks, not only because searching many partitions eventually reveals an interesting number.
2. Reconcile slice rates with the overall score
Our synthetic audit contains three mutually exclusive, exhaustive conditions. Normal lighting has 72 correct decisions out of 80, or 90%. Low light has 9 of 15, or 60%. A new camera has 2 of 5, or 40%. The total is 83 correct out of 100, giving 83% aggregate accuracy.
Because these slices partition the sample, the overall score is their support-weighted average:0.8×0.9+0.15×0.6+0.05×0.4=0.83. If slices overlap, such as low light and new camera, their counts cannot simply be added as if they were a partition. Report overlap and intersections to prevent double counting.
A future mixture with 30% normal lighting,40% low light, and 30% new-camera observations would have expected accuracy 0.63 if the three slice-specific rates stayed fixed. This is a conditional scenario, not a forecast. The rates themselves may also change, especially when the new conditions differ from the small observed examples.
Executed slice audit
| Condition | Cases | Accuracy | 95% interval |
|---|---|---|---|
| Normal | 80 | 90% | 81.49%–94.85% |
| Low light | 15 | 60% | 35.75%–80.18% |
| New camera | 5 | 40% | 11.76%–76.93% |

3. Small slices need uncertainty and more evidence
The new-camera estimate rests on only five cases. One changed outcome moves accuracy by twenty percentage points. Its wide interval reflects limited evidence under the stated independent-observation model. It would be irresponsible either to declare the camera hopeless from 40% alone or to dismiss the issue because the uncertainty is wide.
A Wilson interval is a repeated-sampling interval for a binomial proportion under its assumptions, not a probability that this fixed unknown parameter lies inside the computed bounds. Correlated frames from the same parcel or camera session violate the simple independent-case calculation. Use grouped or otherwise appropriate uncertainty methods when observations share a source.
Multiple slice searches create additional selection issues. If dozens of exploratory slices are examined, the most extreme observed rate may partly reflect chance. Prespecify important slices where possible, label discovered patterns exploratory, and confirm them on new data. Do not treat overlapping confidence intervals as a complete test of equality or nonoverlap as a substitute for a planned comparison.
4. Test robustness with meaningful changes
Perturbation tests can ask whether predictions behave sensibly after controlled changes, such as a realistic brightness adjustment or a missing optional field. The transformation must preserve the true task label if unchanged-label robustness is being assessed. An edit that makes a code unreadable or changes the actual object can legitimately change the answer.
Keep paired original and perturbed examples together in evaluation accounting. Measure prediction changes, error changes, and affected operating conditions. A system that is consistently wrong can appear stable, so prediction invariance alone is not robustness. Compare accuracy or task loss against valid labels as well.
Synthetic perturbations cover only the transformations designed. They do not prove resilience to every real shift or adversarial input. Record perturbation severity, data validity, and the domain reason it represents a plausible deployment condition.
5. Turn findings into an actionable review
A useful slice report separates observed evidence, plausible explanations, and next actions. “Two of five new-camera cases were correct” is evidence. “Sensor processing may be different” is a hypothesis. Collecting a larger labeled sample or checking image normalization is an action. Do not present the hypothesis as already established by the metric.
Prioritize investigation by severity, prevalence, uncertainty, and operational consequence. A tiny but critical slice may warrant urgent safeguards despite imprecise estimates. Conversely, a statistically detectable but negligible difference may not justify a disruptive change. If findings lead to model changes, preserve independent assessment for the revised system.
For sensitive or high-impact applications, metric parity alone does not establish fairness or suitability. This lesson uses nonsensitive camera conditions, but real decisions require domain-specific constraints and human accountability beyond a generic slice chart.
CPU lab: calculate support, intervals, and a mixture scenario
The script uses SciPy's binomtest proportion_ci with method='wilson', checked with SciPy 1.17.0. It requires no model, external data, or paid service. Each slice's count and accuracy are printed with a95% interval, and assertions verify aggregate accuracy 0.83 and the conditional new-mixture value 0.63.
The intervals are illustrative under an independent Bernoulli model. The script does not test whether the three conditions have equal performance or adjust for exploratory slice selection. Those would be separate inferential questions requiring an appropriate design.
from scipy.stats import binomtest
slices=[('normal',72,80),('low_light',9,15),('new_camera',2,5)]
for name,correct,total in slices:
interval=binomtest(correct,total).proportion_ci(confidence_level=.95,method='wilson')
print(name,'support/accuracy/Wilson interval:',total,round(correct/total,4),
(round(interval.low,4),round(interval.high,4)))
overall=sum(c for _,c,n in slices)/sum(n for _,c,n in slices)
shifted=sum(weight*c/n for weight,(_,c,n) in zip([.3,.4,.3],slices))
print('Observed overall:',overall)
print('Hypothetical new mixture at fixed slice rates:',round(shifted,4))
assert abs(overall-.83)<1e-12 and abs(shifted-.63)<1e-12
Four practice questions with worked answers
Practice 1
Why does the normal-lighting slice dominate the observed aggregate?
Answer: It contains 80 of 100 cases, so its 90% accuracy receives weight 0.8 in the partition-weighted average. The new-camera rate receives weight 0.05 despite being much lower. Aggregate accuracy therefore describes this particular condition mixture, not uniform performance across conditions.
Practice 2
In a five-case slice, what is the effect of one additional correct decision with the same denominator?
Answer: Accuracy rises by 1/5=0.2, or twenty percentage points. This sensitivity illustrates why small-support rates are unstable. Report counts and uncertainty, and gather more relevant evidence rather than interpreting a single percentage as a precise operating characteristic.
Practice 3
Two slices overlap on ten observations. Can their correct counts and totals be added to compute an overall score?
Answer: Not without accounting for the overlap, because those ten observations would be counted twice. Use mutually exclusive intersections, inclusion-exclusion where appropriate, or compute the overall score directly from unique observation identifiers. Label slice definitions clearly so readers know whether they form a partition.
Practice 4
A perturbation test makes a shipping label illegible but keeps its original target. What assumption needs review?
Answer: The unchanged-label task may no longer be valid or solvable from the transformed input. Define whether the intended outcome is recognition, abstention, or another action under illegibility. Robustness claims require transformations whose semantic and operational meaning is justified, not merely an unchanged target column.
Five multiple-choice questions
Question 1
What must accompany a slice percentage?
- A. Only the model name
- B. Its support and definition
- C. Only the global average
Correct: B. Counts and membership rules determine interpretation, uncertainty, and possible overlap.
Question 2
When is the displayed weighted-mixture calculation valid?
- A. Only when all slice accuracies are equal
- B. For any overlapping slice list
- C. When slices partition cases and their rates are assumed fixed
Correct: C. The calculation requires exhaustive nonoverlapping conditions and an explicit rate-stability assumption.
Question 3
Does a wide interval prove there is no problem?
- A. No, it indicates limited precision rather than safety
- B. Yes, because the difference is uncertain
- C. Only for the smallest slice
Correct: A. Consequence and plausible risk can warrant investigation even when the estimate is imprecise.
Question 4
What must an unchanged-label perturbation preserve?
- A. The relevant task semantics
- B. The model’s current prediction
- C. The exact pixel values
Correct: A. Meaning-preserving changes test a different property from edits that alter the true task or remove essential information.
Question 5
What should happen to an extreme slice found after many searches?
- A. Treat it as automatically confirmed
- B. Label it exploratory and seek appropriate confirmation
- C. Delete the other slice results
Correct: B. Selection among many noisy estimates affects how strongly the finding can be interpreted.
Common pitfalls
Do not hide small denominators, overlapping slices, or failed conditions behind an aggregate. Do not claim all distribution shifts are covered by a few perturbations. Avoid treating uncertainty as proof of safety or exploratory subgroup differences as established causes.
Summary
Slice evaluation reveals where performance varies and how population mixtures affect aggregate results. Pair rates with support, appropriate uncertainty, and operational meaning. Use valid perturbations and independent confirmation to turn observations into defensible robustness improvements.