Evaluating Classifiers
A trained classifier is only useful once we can measure how good it is. We build the confusion matrix, see why accuracy misleads on unbalanced data, and define precision, recall, and the F-measure that balances them.
╌╌╌╌
This builds on Naive Bayes and Sentiment Classification, which trained a classifier that labels documents. Having a classifier is not the same as knowing it works. This lesson answers two questions that apply to any classifier in the module: how good is it, and — when a new model beats the old one — is the improvement real or an accident of the test set?
The confusion matrix
A trained classifier is worthless until we can say how good it is. Start with a
binary detection task — spam vs. not-spam, or tweets about our pie
vs.
everything else — and the human-assigned truth we compare against, the gold
labels.
Cross-tabulate what the system said against the gold truth and you get a confusion matrix. Each cell counts one kind of outcome: true positives (system says positive, gold agrees), false positives (system says positive, gold disagrees), false negatives (system misses a real positive), true negatives (both say negative).
The obvious metric, accuracy — the fraction of all items labeled correctly — is
misleading on unbalanced data. Take a million tweets, only of them about our
pie. A classifier that labels everything not about pie
scores accuracy while finding not one of the comments we care about. When the
classes are unbalanced — as spam, or about pie,
always are — accuracy rewards
ignoring the rare class.
Precision, recall, F1
Two metrics repair this by focusing on the positive class. Precision is the fraction of the items the system called positive that really are positive — how much to trust a positive verdict:
Recall is the fraction of the items that really are positive that the system found — how much it misses:
The nothing is pie
classifier now scores a recall of : precision and
recall both hinge on true positives, so neither can be gamed by ignoring the rare
class. They trade off — a system can raise recall by labeling everything positive
(at very low precision), or raise precision by labeling only its surest cases (at
very low recall) — so we want a single number combining both. That is the F-measure, the
weighted harmonic mean of precision and recall:
The parameter tilts the balance: favors recall, favors precision. With the two weigh equally, giving the standard :
The harmonic mean (not the plain average) is deliberate: it sits closer to the smaller of the two values, so a system cannot post a high by excelling at one metric while failing the other. Both precision and recall must be respectable to score well.
For example, say a spam filter flags messages, of which are truly spam (so are false alarms), and it misses real spam messages it should have caught. Then precision is and recall is . The plain average would be , but the harmonic mean pulls toward the weaker number: . Push recall down to while holding precision at , and the plain average is still a respectable , but collapses to — the harmonic mean does not let strong precision compensate for weak recall.
More than two classes: macro vs. micro averaging
Sentiment often has three classes (positive, negative, neutral), and tasks like part-of-speech tagging have dozens. Naive Bayes is already multi-class — nothing in the arg-max depends on — but precision and recall need extending. Compute them per class (treat that class as positive, all others as negative), then combine the per-class numbers in one of two ways.
Macroaveraging computes precision (and recall) separately for each class, then averages those numbers, weighting every class equally. Microaveraging pools all classes' decisions into a single confusion matrix and computes one precision from the totals. The two answer different questions. A microaverage is dominated by the frequent class — if spam vastly outnumbers the others, spam's performance swamps the pooled count. A macroaverage gives a small, rare class the same vote as a big one, so it better reflects performance when every class matters equally.
Cross-validation
We train on a training set, tune on a development test set (devset), and report final numbers on a held-out test set the model has never seen — tuning on the test set would overfit it and inflate the score. But carving out fixed dev and test sets removes data from training, and a small fixed test set may not be representative.
Cross-validation uses all the data for both. Partition it into disjoint folds; hold out one fold as the test set, train on the other , record the score; repeat with each fold as the held-out set in turn; average the scores. With this is 10-fold cross-validation — ten models, each trained on of the data and tested on the remaining .
- 1partition into disjoint folds
- 2for to do
- 3
- 4
- 5train a classifier on
- 6evaluate the classifier on
- 7return
Cross-validation uses the whole corpus for testing, which means we can never look at the data to understand it or design features without peeking at the test set. The common compromise is to fix a small test set once, then run cross-validation inside the remaining training data.
Statistical significance testing
Suppose you build a new classifier and it scores higher than the old one on the test set — logistic regression's beats naive Bayes' by . Is the new system really better, or did it just get lucky on this particular test set?1Statistical hypothesis testing answers this, and no comparison of two systems is complete without it.
Write for the score system earns on test set under some metric (accuracy, , BLEU — anything). The quantity we care about is the performance difference between and on that test set:
This is the effect size: a large says looks far ahead of , a small one that it barely edges it out. We would like , but observing a positive on one test set is not enough. might be accidentally ahead on this and behind on the next. We want to know whether 's lead would persist on some other test set drawn from the same source.
The null hypothesis and the p-value
Hypothesis testing formalizes this with two competing claims:
The null hypothesis supposes is not actually better than — that the observed lead is an accident of this test set. The goal is to rule out with enough confidence to accept , that genuinely wins.
To do so, imagine a random variable ranging over all possible test sets and ask: if were true, how often would we see a difference as large as the we actually observed? That probability is the p-value:
Read it carefully — it is the probability of seeing our result, or a more extreme one, under the assumption that is no better than . A huge observed (say scores and scores ) would be astonishing if held, so its p-value is tiny. A small is unsurprising even under , so its p-value is large. When the p-value falls below a threshold — commonly or — we reject the null hypothesis and call the result statistically significant.
In NLP we rarely compute the p-value with parametric tests like the -test, because those assume the test statistic is normally distributed and that assumption does not hold for our metrics. Instead we use non-parametric tests based on sampling: artificially manufacture many versions of the test set and read the distribution of off them directly. The two common choices are approximate randomization and the bootstrap test. Both are usually run in a paired form, comparing and on the same items so each of 's per-item results lines up with 's.
The paired bootstrap test
The bootstrap test applies to any metric and rests on one idea:
bootstrapping — repeatedly drawing samples with replacement from the observed
test set to fabricate many virtual
test sets.2 The only assumption is
that the original test set is representative of the population. Each virtual test
set is a plausible alternative sample, and across thousands of them we can measure
how often 's lead is an accident.
Take a tiny example: a test set of documents, on which scores accuracy and scores , so . Label each document by which systems got it right. Now draw a bootstrap sample by selecting a document from at random times with replacement — the same document may appear several times, others not at all — and record its own . Repeat times (perhaps ) to build a whole distribution of deltas.
Now we count how surprising the observed is. Under we would expect estimated over many true test sets to average zero. But the bootstrap sets are not drawn from a zero-mean distribution — they are resampled from , which is itself biased in 's favor by exactly . So the bootstrap distribution is centered on , not . To measure how often beats expectations by or more, count how often a resampled exceeds the biased center by another :
where is when its condition holds and otherwise. The key correction is the factor of two: because the bootstrap sets inherit 's bias, we test against , not . The result is a one-sided empirical p-value.
The whole procedure is a short loop: compute the real , then for each of bootstrap samples compute and tally how often it clears .
- 1input: test set of size , number of bootstrap samples
- 2computeobserved lead of over on
- 3
- 4for to do
- 5for to do
- 6select a member of at random and add it toresample with replacement
- 7computelead of over on this virtual set
- 8if then
- 9
- 10fraction of samples where beat expectations
- 11return
To finish the example: with bootstrap test sets and a threshold of , suppose only of them have . Then
so the observed is sufficiently surprising under : we reject the null hypothesis and conclude is genuinely better than . Had hundreds or thousands of bootstrap sets matched 's lead, the p-value would sit above the threshold and we could not rule out luck. Report the effect size alongside the p-value — significance says the difference is real, the effect size says whether it is large enough to matter.
Footnotes
- Jurafsky & Martin, §4.9 — Statistical Significance Testing: comparing two classifiers with the effect size , the null hypothesis , the p-value , significance thresholds of / , and non-parametric (approximate-randomization and bootstrap) rather than parametric tests. ↩
- Jurafsky & Martin, §4.9.1 — The Paired Bootstrap Test (Efron and Tibshirani 1993; Berg-Kirkpatrick et al. 2012): resampling virtual test sets with replacement, the sampling distribution of , and the p-value with the factor of two correcting for the bias of the observed test set. ↩
╌╌ END ╌╌