Evaluating an AI Step: Precision, Recall and Fresh Examples
Measure an AI step fairly: count precision, recall and accuracy on 24 saved results and see why only fresh examples tell the truth.
In this lesson you will
- Build a small labelled test set
- Read accuracy, precision and recall
- Compare tuning-set and fresh-example scores
- Treat small samples with care
About 25 minutes
“It seems to work” is not a measurement
Ravi has been using his email sorter for a month, and it feels fine. But feelings are a poor guide, because mistakes are quiet. A wrongly labelled email does not announce itself, and the ones you notice are the ones that annoyed somebody. If you want to improve an AI step, or compare two versions of it, you need numbers from a fair test. This lesson shows the standard measures and the one habit that makes them honest: testing on examples the step has not seen.
Build a small test set
A test set is a collection of real examples with the right answer written next to each, decided by a person. For the sorter, that means a few dozen emails with removed private details, each labelled sales, billing, support or other by Ravi himself. Choose examples that look like your real mail, including the awkward ones, and keep the set separate from anything you use while building. Add new tricky cases each time the step makes a mistake in real use.
Three measures worth knowing
- Accuracy. The share of all examples labelled correctly. Easy to understand, but it hides which labels go wrong.
- Precision. Of the emails the step called sales, what share really were sales? High precision means few false alarms.
- Recall. Of the emails that really were sales, what share did the step find? High recall means few missed cases.
The two pull against each other. A step that labels every email ‘sales’ would catch every real sales email, so its recall would be perfect, but its precision would be poor. Which one matters more depends on the cost of each kind of mistake, and on the label. Missing a complaint may be worse than wrongly flagging one.
Measure the sorter
The program below holds 24 saved results, each a pair: the label a person gave and the label the step gave. It counts the matches and mismatches for every label, then prints precision and recall for each label and the accuracy. The last lines split the list into two halves. The pretend story is that the step was tuned, with its rules adjusted by looking at the results, on one half only.
from collections import Counter
# SAVED results (made up): (label a person gave, label the step gave)
pairs = [
("sales", "sales"), ("billing", "billing"), ("support", "support"), ("other", "other"),
("sales", "sales"), ("billing", "billing"), ("support", "sales"), ("sales", "sales"),
("support", "support"), ("billing", "support"), ("other", "other"), ("sales", "other"),
("support", "support"), ("sales", "sales"), ("billing", "billing"), ("other", "sales"),
("billing", "billing"), ("sales", "sales"), ("support", "support"), ("other", "other"),
("billing", "billing"), ("sales", "sales"), ("support", "support"), ("sales", "sales"),
]
labels = ["sales", "billing", "support", "other"]
count = Counter(pairs)
print("examples:", len(pairs))
print("correct:", sum(v for (a, b), v in count.items() if a == b))
for lab in labels:
tp = count[(lab, lab)]
fp = sum(count[(a, lab)] for a in labels if a != lab)
fn = sum(count[(lab, b)] for b in labels if b != lab)
print(f"{lab}: precision {tp / (tp + fp):.1%}, recall {tp / (tp + fn):.1%}")
acc = lambda ps: sum(a == b for a, b in ps) / len(ps)
build, fresh = pairs[12:], pairs[:12] # the step was tuned on the first set only
print(f"accuracy on the tuning set: {acc(build):.1%}")
print(f"accuracy on fresh examples: {acc(fresh):.1%}")
print(f"accuracy on all: {acc(pairs):.1%}")
examples: 24
correct: 20
sales: precision 77.8%, recall 87.5%
billing: precision 100.0%, recall 83.3%
support: precision 83.3%, recall 83.3%
other: precision 75.0%, recall 75.0%
accuracy on the tuning set: 91.7%
accuracy on fresh examples: 75.0%
accuracy on all: 83.3%
Of 24 examples, 20 were correct. Sales had a precision of 77.8 per cent and a recall of 87.5 per cent, so some non-sales emails were being called sales, since 22.2 per cent of the emails it called sales were wrong, while it found most real sales emails. Billing was perfectly precise at 100.0 per cent but its recall was 83.3 per cent, which means that every email it called billing really was billing, but one real billing email was missed. Support came in at 83.3 per cent on both measures, and the small ‘other’ group scored 75.0 and 75.0 per cent. Each label tells Ravi something different, which accuracy alone would not.
The final three lines carry the key lesson. On the half of the examples used for tuning, accuracy was 91.7 per cent. On fresh examples it was 75.0 per cent. The overall figure, 83.3 per cent, sits between them and hides the gap. A step always looks better on the examples it was adjusted for, so the honest score is the one on examples it has never seen. If you tune a step again and again against the same list, the list stops being a fair test. Keep a fresh set locked away for the final check.
Small samples need care
With 24 examples, one mistake moves accuracy by more than four points, and the number for a label with only four examples is not stable at all. Treat these figures as rough guides until you have a few hundred examples, and report them with a plain note about how small the sample was. Never promise a customer, or yourself, that the step is some percentage right in general based on a tiny list.
A routine you can keep
- Collect labelled examples, including awkward ones.
- Measure accuracy, precision and recall per label.
- Look at the mistakes themselves, not only the totals.
- Change one thing, such as the prompt or the confidence line, and measure again on fresh examples.
- Repeat monthly, or whenever the kind of mail changes.
Judging a step on the examples you used to improve it. The score will look better than the real world will treat you.
The 24 results are made up for teaching and do not come from any real AI step. The measures are standard ones, but the right numbers for your own work depend on your data and the cost of mistakes. No promises about accuracy are made.
Practice for this lesson
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
What is a test set?
- A list of customers
- A kind of workflow
- Labelled examples with answers a person chose
- A set of passwords
The right answers come from a person, not the step.
What does precision measure?
- Of the items the step called X, how many really were X
- How fast the step is
- How many items exist
- How long replies are
High precision means few false alarms.
What does recall measure?
- How many labels exist
- How fast it runs
- Of the real X items, how many the step found
- How many emails arrive
High recall means few missed cases.
Why is the score on fresh examples more honest?
- The step was not tuned on them
- They are always easier
- They are fewer
- They are older
A step looks better on the data it was adjusted for.
Why be careful with only 24 examples?
- Small sets are always perfect
- One mistake moves the score a lot
- Labels do not matter
- Precision cannot be measured
Small samples give unstable numbers.
Summary
- A test set has answers chosen by a person.
- Precision counts false alarms, recall counts misses.
- The honest score is on fresh examples.
- Small samples give rough numbers only.
Questions
How many examples do I need?
More is better. A few dozen is a start for learning the habit, and a few hundred gives steadier numbers. Include awkward cases.
Which measure matters most?
It depends on which mistake costs more. Missing a complaint may matter more than wrongly flagging one, so look at each label.
Are these results from a real AI step?
No. They are made-up saved results used to teach the measures.
