Sub topic 4
Collecting Errors into an Inspectable Table
This chapter assumes you know basic Python, pandas, NumPy, accuracy, precision, recall, and F1. It also assumes you have trained and evaluated an NLP classifier at least once. The goal is to turn model outputs into a table that you can inspect, filter, sort, and share.
Why keep an error table?
An aggregate score tells you how often a model is correct. It does not show which examples fail or whether the failures share a pattern. An error table preserves one row per example, including both the expected label and the model's output.
A useful table usually contains:
| Column | Purpose |
|---|---|
| `example_id` | Stable identifier for tracing the example back to its source |
| `text` | The input shown to the model |
| `label` | The trusted or gold label |
| `pred` | The model's predicted label |
| `confidence` | The model's probability or score for its prediction |
| `split` | Whether the row came from validation or test data |
| metadata | Properties such as language, length, source, or product type |
Keep the test set separate from training data. Do not use error analysis to quietly change test labels or tune the model against the test set. Use a validation set while developing fixes, then use the test set for a final check.
Build the table
Start with aligned arrays or columns. Every position must refer to the same example. A common mistake is to shuffle texts without applying the same ordering to labels and predictions.
import pandas as pd
df = pd.DataFrame({
"example_id": [101, 102, 103, 104, 105],
"text": [
"great phone",
"not bad at all",
"battery died in a day",
"fine I guess",
"love the camera",
],
"label": ["pos", "pos", "neg", "neu", "pos"],
"pred": ["pos", "neg", "neg", "pos", "pos"],
"confidence": [0.97, 0.81, 0.90, 0.64, 0.99],
"split": ["test"] * 5,
})
df["correct"] = df["label"].eq(df["pred"])
errors = df.loc[~df["correct"]].copy()
print(errors.sort_values("confidence", ascending=False).to_string(index=False))The correct column makes filtering explicit. The errors table contains only disagreements, but keep the full table as well. You need it to compare error rates, inspect correct examples, and calculate metrics later.
Inspect errors in a useful order
Sort errors by confidence from highest to lowest. A confident mistake may indicate a labeling problem, a shortcut learned by the model, or an important failure mode. Low-confidence mistakes can instead show examples near the model's decision boundary.
Do not treat confidence as correctness. A score of 0.99 means the model selected its answer with high confidence. It does not mean that answer has a 99 percent chance of being correct unless the model is calibrated.
Add fields that help you ask specific questions:
- Token count or character count.
- Language or source system.
- Predicted probabilities for every class.
- Annotation notes.
- An error category assigned during manual review.
- Model version and evaluation-run identifier.
A stable example_id is essential. It lets you find the original record, compare predictions from two model versions, and avoid counting a duplicated example twice.
Create a repeatable review queue
Manual inspection becomes more reliable when the review order is recorded rather than chosen informally. Save the table as CSV or Parquet with the model version and dataset snapshot. Then reviewers can label a sample using the same categories, such as ambiguous text, wrong class, missing context, or preprocessing failure.
Review a random sample of errors as well as the most confident errors. Sorting alone overrepresents one kind of failure. Record the number reviewed so later conclusions include the review sample size.
Exercise
Why should an error table retain the full evaluation output instead of storing only incorrect rows?
Show answer
The full table supports comparisons with correct examples, metric calculations, slice analysis, and reproducible review.