Part III has spent four chapters making a model better at predicting the next word. Counting, then a network, then a recurrence, then a gate.
At no point did we ask how you would know whether any of it worked. Perplexity was the only answer offered, and Section that section was careful to say that perplexity measures surprise rather than usefulness.
This chapter fixes that, and it does so on the simplest task in the field.
Language modelling asks what comes next. Classification asks a smaller question and answers it far more reliably.
Given a document, which of a fixed set of labels applies?
Spam or not spam. Positive, negative or neutral. News, fiction, government or learned. The label set is decided in advance, and the model picks one.
This is the task that pays for itself. It runs in every mail system, every support desk and every content moderation pipeline in production today.
It is also where the empirical method of this field is easiest to see, which is why the chapter spends more time on evaluation than on models.
Everything needed for a strong classifier is in Part II.
Represent each document as a vector of term weights, as Chapter 4 did. Feed those vectors to a linear classifier such as logistic regression or a linear support vector machine. Train on labelled documents.
That pipeline is thirty years old and it is still the right first move. On many tasks it is within a few points of a fine-tuned transformer, at a thousandth of the cost.
Two details decide most of its quality.
The features. Unigrams are the default. Adding bigrams captures short phrases such as not good, which unigrams cannot express at all.
The weighting. Raw counts let long documents shout. The tf-idf of Chapter 4 fixes both length and the dominance of common words.
A neural classifier replaces the hand-built vector with a learned one. The recurrent networks of Chapter 12 can read a document and hand their final state to a softmax. The evaluation, however, is identical, and the evaluation is what this chapter is about.
The obvious score is accuracy, the fraction of test documents labelled correctly. It is the wrong default, and one example shows why.
Take a hundred test documents distributed unevenly.
| class | documents |
|---|---|
| news | 90 |
| fiction | 5 |
| government | 3 |
| learned | 2 |
Now build a model that is one line long. Always answer news.
| class | precision | recall | |
|---|---|---|---|
| news | 0.9000 | 1.0000 | 0.9474 |
| fiction | 0.0000 | 0.0000 | 0.0000 |
| government | 0.0000 | 0.0000 | 0.0000 |
| learned | 0.0000 | 0.0000 | 0.0000 |
| accuracy | 0.9000 | ||
| macro- | 0.2368 | ||
Ninety per cent accuracy, from a model that cannot tell any two documents apart.
Accuracy is blind to which classes are wrong. What we want is a score where every class counts equally, however rare it is.
Treat each class as a question on its own, that class against all the others.
Three counts follow. is documents predicted and truly . is predicted but truly something else. is truly but predicted something else.
Precision asks how much of what you claimed was right. Recall asks how much of what existed you found.
They pull against each other. Predict for everything and recall hits while precision collapses. Predict only when certain and precision rises while recall falls.
is their harmonic mean, and the harmonic mean is the point. It is high only when both are high. An arithmetic mean would let a precision of hide a recall of , and the harmonic mean will not.
To get one number for the whole task, average the per-class scores.
Every class contributes one term, whatever its size. A classifier that ignores one class entirely scores for it, and macro- drops by a full .
Neglecting the rare class therefore costs you. That is exactly what Equation (14.2) is for, and why Project 5’s leaderboard uses it.
Micro-averaging pools every decision first and computes one from the totals. Frequent classes then dominate.
There is a fact about micro-averaging worth stating, because it saves confusion later. On a single-label task micro- equals accuracy exactly.
The reason is simple. When each document gets exactly one label, every error is simultaneously one false positive for the predicted class and one false negative for the true one. So the pooled precision and the pooled recall are both the fraction correct, and their harmonic mean is that same number.
So micro- tells you nothing accuracy did not. Report macro-, or report the per-class table.
One number cannot tell you what to fix. A confusion matrix can.
Rows are the true class, columns the prediction. The diagonal is correct and every off-diagonal cell is an error.
| true predicted | news | fiction | govt. | learned | total |
|---|---|---|---|---|---|
| news | 9 | 0 | 2 | 1 | 12 |
| fiction | 0 | 11 | 0 | 1 | 12 |
| government | 3 | 0 | 7 | 2 | 12 |
| learned | 1 | 1 | 1 | 9 | 12 |
Score it with Equation (14.1), one row per class.
| class | ||||||
|---|---|---|---|---|---|---|
| news | 9 | 4 | 3 | 0.6923 | 0.7500 | 0.7200 |
| fiction | 11 | 1 | 1 | 0.9167 | 0.9167 | 0.9167 |
| government | 7 | 3 | 5 | 0.7000 | 0.5833 | 0.6364 |
| learned | 9 | 4 | 3 | 0.6923 | 0.7500 | 0.7200 |
| accuracy | 0.7500 | |||||
| macro- | 0.7483 | |||||
The classes are balanced here at twelve each, so accuracy is not misleading. It is still less useful. It says and stops.
The per-class table says which genre is failing and how. Look at government. Recall is , because five of its twelve documents went elsewhere. Precision is , because three documents from other genres were labelled government.
Both directions are wrong at once, which is a different problem from being wrong in one direction only.
Project 5 asks for both of these, and both are computed rather than guessed.
The hardest class is the one with the lowest per-class . Here that is government at , against , and .
The most confused pair is the unordered pair with the most cross errors. Add the two off-diagonal cells that join them.
| pair | total | ||
|---|---|---|---|
| news / government | 2 | 3 | 5 |
| government / learned | 2 | 1 | 3 |
| news / learned | 1 | 1 | 2 |
| fiction / learned | 1 | 1 | 2 |
| news / fiction | 0 | 0 | 0 |
| fiction / government | 0 | 0 | 0 |
So the answer is news and government, with five errors between them.
Both diagnoses are properties of the matrix, not opinions. Two people reading the same matrix must reach the same pair, which is what makes them gradeable.
One more thing the matrix shows that a score cannot. The errors are directional. Government loses three documents to news and takes two back, and those are different mistakes with different causes.
A score is the end of an experiment, not the work. The work is a loop, and the lectures name its discipline.
Train on train. Tune on a held-out slice of train. Touch the test set once, at the end. Every decision made while watching test scores is a decision that has quietly memorised the test set.
An ablation changes exactly one thing and records the validation score. A log might read like this.
| configuration | validation macro- |
|---|---|
| unigrams, raw counts | 0.612 |
| unigrams, tf-idf | 0.681 |
| unigrams and bigrams, tf-idf | 0.724 |
| plus minimum document frequency 2 | 0.748 |
| plus lowercasing | 0.741 |
The last row went down, and reporting it is the point. An ablation is a record of the runs you did, not of the runs that worked.
The winning configuration in your ablation must be the model you actually submit. If it is not, your experiment log and your system have diverged, and one of them is lying.
Which class pairs blur? What vocabulary do the misclassified documents share? A confusion matrix tells you where to look, and only the documents tell you why.
That loop, baseline to iteration to ablation to confusion matrix, is the empirical method of this whole field.
It is the same loop for a linear classifier over tf-idf features and for a fine-tuned transformer. Only the middle step changes.
Try it yourself.
code/worked_examples/classification.pyproduces every table in this chapter.--trapbuilds the ninety-per-cent-accuracy model that has learned nothing,--matrixscores the genre matrix class by class,--readderives the hardest class and the most confused pair, and--averagesshows micro- landing exactly on accuracy.
Part III began with counting words to predict the next one. It ends with a classifier and a confusion matrix.
Three things carry forward unchanged.
The objective is still cross-entropy against a one-hot target, whether the target is the next word or a genre label.
The evaluation discipline is now fixed. Held-out data, a stated metric, one factor at a time, and a matrix rather than a number.
The representation is still a vector, and it is still built from context.
What Part III could not fix is the vector itself. Every word has had exactly one, and Chapter 9 already showed why that fails for bank.
Part IV takes that last constraint away.
cover naive Bayes, logistic regression and evaluation with more worked cases. treat classification and evaluation from the information retrieval side, where precision and recall were first made standard. survey the averaging choices and what each of them hides.
Build the trap. Construct a four-class test set of documents where a majority-class classifier scores above accuracy and below macro-. State the class distribution you used, and explain which property of Equation (14.2) makes the second number small.
Score the matrix by hand. Using the genre matrix in this chapter, compute , and for government and confirm , and . Then say in one sentence what the gap between its precision and its recall tells you.
Why the harmonic mean. A class has and . Compute the arithmetic mean and the harmonic mean. Which one would you rather report to someone deciding whether to deploy the system, and why?
Micro equals accuracy. Prove that for single-label classification the pooled and pooled are equal, and therefore that micro- equals accuracy. Where does the proof break if a document may carry two labels?
Find the pair. For the genre matrix, list all six unordered class pairs with their cross-error totals and confirm the winner. Then construct a matrix in which the hardest class is not a member of the most confused pair, and explain how that happens.
Run an ablation. On a corpus of your choice, build a tf-idf and linear-classifier baseline and record validation macro-. Then change one factor at a time: bigrams, minimum document frequency, sublinear term frequency. Report every run including the ones that hurt, and state which row you would submit.
Read the documents. Take the ten misclassified documents from your most confused pair. What vocabulary do they share? Propose one feature change that would help, predict its effect on macro- before running it, then run it and report the gap between your prediction and the result.