Sub topic 6
Slicing Results by Text Properties
Assume you know Python, pandas, NumPy, and basic evaluation metrics such as accuracy, precision, recall, and F1. A slice is a subset of evaluation examples selected by a property, such as text length, language, product area, or the presence of a negation word.
Why slice results?
An aggregate score combines all examples into one number. That number can hide systematic failures. A classifier might perform well on short reviews but fail on long reviews. A named entity recognizer might work for common names but miss organization names.
Slice analysis compares model behavior across meaningful groups.
Choose properties before looking at the results when possible. Otherwise, you may search for a slice that confirms a preferred explanation. Useful properties include:
- Number of tokens or characters
- Presence of negation, slang, emojis, or URLs
- Document source or product category
- Language or dialect
- Number of entities
- Rare or out-of-vocabulary words
- Time period or data collection source
Every slice needs a clear definition. “Long text” is not reproducible unless you specify its token-count range.
Computing metrics by slice
The following example groups sentiment examples into length buckets. The correct column records whether each prediction matches its label.
import pandas as pd
df = pd.DataFrame({
"text": [
"good",
"not good at all really",
"the delivery was late but the product itself works well and looks nice",
"bad",
"I expected more from this brand given the price I paid for it",
"fine",
],
"label": ["pos", "neg", "pos", "neg", "neg", "pos"],
"pred": ["pos", "pos", "neg", "neg", "pos", "pos"],
})
df["n_tokens"] = df["text"].str.split().str.len()
df["length_bucket"] = pd.cut(
df["n_tokens"],
bins=[0, 3, 8, 1000],
labels=["short", "medium", "long"],
)
df["correct"] = df["label"] == df["pred"]
summary = (
df.groupby("length_bucket", observed=True)["correct"]
.agg(accuracy="mean", count="count")
)
print(summary)For a classification task, accuracy is the mean of correct. For imbalanced classes, also calculate per-class precision, recall, or F1 inside each slice. A slice can have high accuracy while failing almost every example from a minority class.
| Slice | Useful question | Warning |
|---|---|---|
| Short text | Does the model need more context? | Very short examples may be ambiguous |
| Long text | Does performance decline with complexity? | Long documents may contain several topics |
| Text with negation | Does the model handle compositional meaning? | Negation rules differ by domain |
| Data source | Does one source use different language? | Source may correlate with the label |
Interpreting slice gaps
Compare each slice with the overall score and with other slices. A gap is a signal for investigation, not proof of a defect. Check the support, which is the number of examples in the slice. A score based on ten examples is less reliable than one based on ten thousand.
Also inspect how slices overlap. Long texts may contain more entities, and one data source may contain mostly long texts. A length gap might actually be a source gap. You can analyze a two-dimensional slice, such as source × length, but small cells quickly become unreliable.
A useful workflow is:
- Define the property and its slicing rule.
- Compute metrics and counts on the untouched test set.
- Inspect examples from weak and strong slices.
- Check whether the slices overlap with other factors.
- Estimate uncertainty before making a product decision.
- Add the slice to the regular evaluation report if it represents a real user group.
The next step is to determine whether an observed slice gap is larger than ordinary sampling noise.
Exercise
A slice has 92% accuracy on 25 examples, while the full test set has 88% accuracy on 10,000 examples. Should you conclude that the model is better on this slice?
Show answer
No. The slice is small, so its estimate may be noisy. Report its count, inspect its examples, and calculate an uncertainty interval before drawing a conclusion.