Text classification assigns documents to classes fixed in advance: a learning method takes a training set $\mathbb{D}$ of labeled documents and produces a classifier $\gamma: \mathbb{X} \to \mathbb{C}$, judged on test documents it has not seen. Tasks are binary, multiclass (one-of), multilabel (any-of) or hierarchical. Reuters-21578, this unit's collection (ModApte split, 7,769 training and 3,019 test stories), is multilabel and is handled with one binary classifier per category. Documents can be classified by people, by hand-written rules or by a learned classifier, and learning is not automatically better: on the category grain, a three-word rule (grain, wheat or corn) reaches test F1 0.844, against 0.710 for Naive Bayes over all 18,135 terms. What learning buys is scale, across 90 categories and a changing vocabulary.
An analyst wants to follow news about multicore computer chips. Running the same search every morning is tedious, so the search is saved and run automatically on each day's new articles. Matching documents are delivered, and the rest are discarded. That saved search is a standing query, and it divides each day's news into two classes: relevant to multicore chips, and not (Manning, Raghavan, and Schütze, 2008, ch. 13). Written as keywords, such as multicore AND (chip OR processor), it will miss articles that say multi-core or dual-core CPU. It can be refined by hand, but each refinement adds a rule that someone must maintain. The alternative is for the system to learn the distinction from examples of articles that do and do not belong.
That is text classification: assigning documents to categories fixed in advance. The ranking models of probabilistic retrieval answered a query that someone typed once. A classifier answers the same question of every document that arrives.
The task has several names. Classification with a standing query is also called routing or filtering. When the classes are general subject areas, or topics, the task is called text classification, text categorization, topic classification or topic spotting. The methods are the same for all of them, so this unit, like the course text, treats them as one problem (Manning, Raghavan, and Schütze, 2008, ch. 13).
Here is the route. First we state the problem and fill in each symbol with Reuters-21578, the news collection that runs through this unit. Then we survey the shapes the task takes. Next we compare three ways to classify, including a three-word rule that beats a learned classifier on the category grain. Finally we look at the representation and at where classification is used.
The problem, stated formally
What a classifier learns is a function from documents to classes. Manning, Raghavan, and Schütze (2008, §13.1) define the problem formally. Given:
- a document space $\mathbb{X}$, the set of all possible documents, each represented in some way, for instance as a bag of terms;
- a fixed set of classes $\mathbb{C} = \{c_1, \dots, c_J\}$, also called categories or labels;
- a training set $\mathbb{D}$ of labeled documents $\langle d, c \rangle$, each document paired with its correct class,
a learning method produces a classifier $\gamma: \mathbb{X} \to \mathbb{C}$ that maps documents to classes.
The experiments in this topic fill in each symbol concretely:
- Documents. Reuters-21578 news stories, each represented as the bag of its stemmed terms after stop words are removed. A median training story has 58 tokens and 38 distinct terms.
- Classes. The collection's topic categories, such as earn, grain, and ship.
- Training and test sets. The 7,769 stories of the standard training split, and the 3,019 stories of the test split.
- Learning method. Naive Bayes or one of the vector space classifiers. The classifier it produces for grain is a function that reads a story and answers yes or no.
A common misreading is that the goal is to label the training stories correctly. That is trivial: a lookup table of the 7,769 stories does it perfectly and knows nothing about story 7,770. The aim is to label new documents correctly. A classifier that memorizes its training set and fails on new documents is said to overfit. Whether it generalizes is measured on a test set of labeled documents kept apart from training. Evaluating classifiers measures how much training data flatters a classifier.
Variants
The task comes in several shapes, and the Reuters task is the third of them, multilabel:
- Binary classification assigns each document to one of two classes: spam or not, relevant to the standing query or not.
- Multiclass (one-of) classification assigns each document to exactly one of several classes, such as the language of a document, or the star rating of a review.
- Multilabel (any-of) classification assigns each document to any number of classes, including none. A news story about a wheat export deal can be about grain, wheat, and trade at once. Multilabel problems are usually decomposed into one binary classifier per class, each deciding independently whether its class applies.
- Hierarchical classification assigns documents to classes arranged in a taxonomy, such as the subject headings of a library or a product catalog, where the structure can be used to share evidence between related classes.
The classes in the standard ModApte split of the Reuters-21578 collection, used for the experiments in this topic, are multilabel. A story can belong to several of 90 topic categories, so classifiers for it are built one class at a time. Of the 10,788 stories in the split, 9,160 carry exactly one category, 1,173 carry two, and the rest between three and fifteen. The experiments use the ten largest categories, whose sizes differ by a factor of sixteen:
The labels also overlap. Of the 283 stories labeled wheat, 280 are also labeled grain, and of the 237 labeled corn, 235 are. Of the 582 grain stories, 146 are about neither wheat nor corn.
Example (a category nested in another). A classifier for wheat has a harder job than one for grain, although nearly every wheat story is a grain story. A wheat classifier must learn not whether a story is about grain but the narrower distinction of whether it is about wheat in particular. Its positive examples share grain vocabulary with negative ones, such as grain stories about rice. In Naive Bayes classification, F1 is 0.583 on wheat and 0.495 on corn, against 0.710 on grain. (Evaluating classifiers defines F1, the harmonic mean of precision and recall.)
Three ways to classify
How should the function $\gamma$ be built? There are three broad answers.
Manual classification. People read documents and assign classes. Editors classified documents this way in library catalogs, in the Yahoo! web directory, and in the legal and medical indexes described in search in professional domains. Manual classification is accurate when done by experts, but it is slow and expensive, and it cannot keep up with large or fast-moving collections.
Hand-written rules. An expert writes rules, often Boolean expressions over terms, that assign classes. Well-crafted rules can be very accurate, and for narrow, stable categories they remain in use. But they are expensive to write and maintain. Each rule needs someone who understands both the domain and the rule language, and rules must be revised as vocabulary changes.
Supervised learning. People label a training set, and an algorithm learns the classifier. Labeling requires domain knowledge but no technical skill. The effort shifts from writing rules to labeling examples, which is cheaper and can be repeated whenever the categories or vocabulary change. Learned classifiers are now the standard approach. Sebastiani (2002) described how they became dominant in research from the early 1990s. They reached effectiveness comparable to that of human experts with far less expert labor.
A rule against a learner
Is learning always the better choice? It is not, and hand-written rules should not be underestimated. For the Reuters category grain, consider the rule "assign grain if the story contains grain, wheat, or corn", applied to stemmed terms. It was chosen without looking at the test stories. It scores as follows (computed by tools/experiments/reuters/reuters_more.py):
| Precision | Recall | F1 | |
|---|---|---|---|
| Rule, on the 7,769 training stories | 0.837 | 0.891 | 0.864 |
| Rule, on the 3,019 test stories | 0.895 | 0.799 | 0.844 |
| Naive Bayes, all 18,135 terms, on the test stories | 0.566 | 0.953 | 0.710 |
Three words beat a classifier trained on the whole vocabulary. The rule is precise because these words rarely occur outside grain stories.
Example (what the rule misses). The rule misses 30 of the 149 grain stories in the test set, and most of those name a particular grain instead of the three words in the rule: rice (14 of them), maize (9), or barley (5). A learned classifier can pick up such words from its training stories, which helps explain its recall of 0.953.
The learned classifier finds almost every grain story but also assigns many others. That is a bias in where its decision threshold sits. Evaluating classifiers shows that moving the threshold, chosen on held-out training data, raises its F1 to 0.807.
The comparison is fair to the rule only for a category that a few words define. No short list of words defines acq (acquisitions) or money-fx, so equally good rules for them are likely to be much harder to write. Writing them for all 90 categories, and maintaining them as the news changes, is the cost that learning avoids.
In practice the approaches are often combined. Rules handle cases that must never be wrong, learned classifiers handle the rest, and people review uncertain cases. Their labels then become new training data.
The bag-of-words representation
Most text classifiers in this course represent a document the way the vector space model does: as a bag of terms, possibly weighted. Every distinct term becomes a feature. A collection may have tens of thousands of features, most absent from any given document. The Reuters training stories have 18,135 distinct terms, and the median story uses 38 of them. That high dimensionality has two consequences:
- Classifiers must handle features that occur rarely in training. That is the role of smoothing in Naive Bayes.
- It is often useful to reduce the number of features, the subject of feature selection.
Where classification is used
Why does the problem deserve a unit of its own? Classification is among the most widely deployed applications of retrieval methods:
- Spam filtering, the most familiar binary classifier, which began moving to statistical methods in the late 1990s: Sahami, Dumais, Heckerman, and Horvitz (1998) filtered junk e-mail with a Naive Bayes classifier (see search applications);
- Routing and topic categorization of news, support tickets, and documents into the queues or folders where they belong;
- Sentiment analysis, deciding whether a review or post is positive or negative, or how many stars it gives, as in my Goodreads project;
- Preprocessing for indexing. Several early steps of text processing are classification problems: identifying a document's language, detecting its character encoding, and deciding where words begin and end in languages written without spaces (Manning, Raghavan, and Schütze, 2008, ch. 13);
- Vertical selection in web search, deciding whether a query should trigger news, image, or shopping results (see how a web search engine answers a query);
- Content moderation and safety, flagging documents that violate policies;
- Technology-assisted review in legal discovery, classifying documents as responsive or not.
Classification is one of the tasks of Natural Language Processing, which shows two traps for sentiment in particular: removing the stop word not reverses a review's meaning, and word vectors place good and bad close together (cosine 0.64), so a classifier must learn the difference from labeled data.
Each of these replaces a judgment that would otherwise require a person to read every document. That is the economic reason classification is so widely used. It is also why its errors, the topic of evaluating classifiers, matter.
Worked problems
The course text sets no exercises on its introduction and §13.1, so these problems are posed on this article's Reuters counts. Each count is printed by tools/experiments/reuters/reuters_more.py, and the arithmetic is rechecked at the end of that script. Precision, recall and F1 are those of precision and recall: with TP documents correctly assigned to the class, FP wrongly assigned and FN missed, $P = \mathrm{TP}/(\mathrm{TP} + \mathrm{FP})$, $R = \mathrm{TP}/(\mathrm{TP} + \mathrm{FN})$ and $F_1 = 2PR/(P + R)$.
Problem 1. On the 3,019 test stories, the rule "assign grain if the story contains grain, wheat, or corn" fires on 133 stories, of which 119 are among the 149 grain stories. On the 7,769 training stories it fires on 461, of which 386 are among the 433 grain stories. Compute its precision, recall and F1 on each set.
Solution. On the test stories TP = 119, FP = 133 − 119 = 14 and FN = 149 − 119 = 30. So
$$P = \frac{119}{133} = 0.895, \qquad R = \frac{119}{149} = 0.799, \qquad F_1 = \frac{2 \times 0.8947 \times 0.7987}{0.8947 + 0.7987} = 0.844.$$
On the training stories TP = 386, FP = 461 − 386 = 75 and FN = 433 − 386 = 47, so $P = 386/461 = 0.837$, $R = 386/433 = 0.891$ and $F_1 = 0.864$. These are the two rows of the table above. The rule was written without looking at either set, so neither row is a training score in the usual sense. The two rows differ because the stories differ, and on the test stories the rule happens to be more precise and less complete.
Problem 2. Of the 10,788 stories in the split, 9,160 carry exactly one category and 1,173 carry two. How many carry three or more, and what fraction is that? If each of the 90 categories gets its own binary classifier, how many yes-or-no decisions are made on the 3,019 test stories?
Solution. Every story carries at least one category, so $10{,}788 - 9{,}160 - 1{,}173 = 455$ stories carry three or more, which is $455/10{,}788 = 4.2$ percent. Stories with exactly one category are $9{,}160/10{,}788 = 84.9$ percent, so a one-of classifier could at best be right about the complete label set of 84.9 percent of the stories. That is why the collection is treated as multilabel. With one binary classifier per category, each test story receives 90 decisions, $90 \times 3{,}019 = 271{,}710$ in all.
Problem 3. Of the 283 stories labeled wheat, 280 are also labeled grain, and 582 stories are labeled grain. Consider the rule "assign wheat to every story labeled grain", which uses the true grain labels. Find its precision, recall and F1 for wheat, and the same for corn (237 stories, 235 of them labeled grain).
Solution. For wheat, the rule assigns 582 stories, of which 280 are correct, and it misses 3:
$$P = \frac{280}{582} = 0.481, \qquad R = \frac{280}{283} = 0.989, \qquad F_1 = \frac{2 \times 280}{582 + 283} = \frac{560}{865} = 0.647.$$
The last form uses $F_1 = 2\,\mathrm{TP}/(2\,\mathrm{TP} + \mathrm{FP} + \mathrm{FN})$, and $2\,\mathrm{TP} + \mathrm{FP} + \mathrm{FN}$ is the number assigned plus the number in the class. For corn, $P = 235/582 = 0.404$, $R = 235/237 = 0.992$ and $F_1 = 470/819 = 0.574$. So even perfect knowledge of which stories are about grain leaves a wheat classifier at F1 0.647. The remaining work is the narrower distinction inside the grain stories, which is the point of the example on nested categories above.
Problem 4. A lookup table stores the 7,769 training stories with their labels and answers "not grain" for any story it has not stored. Find its accuracy and F1 for grain on the training stories and on the 3,019 test stories, 149 of which are about grain. Assume no test story is identical to a training story.
Solution. On the training stories every answer is read from the table, so accuracy and F1 are both 1 (assuming no two training stories with the same text carry different labels). On the test stories it answers "not grain" 3,019 times. It is right about the $3{,}019 - 149 = 2{,}870$ stories that are not about grain, so its accuracy is $2{,}870/3{,}019 = 0.951$. It has no true positives, so its recall is 0 and its F1 is 0. The table has memorized its training set and learned nothing that carries to a new story, which is overfitting in its purest form. The accuracy of 0.951 comes entirely from the rarity of the class, which is why this unit reports F1.
What we have learned
A text classifier is a function from documents to classes fixed in advance, learned from labeled examples and judged on documents it has not seen. Reuters-21578 supplies this unit's data: multilabel categories that differ in size and overlap. Three words beat Naive Bayes on grain, so learning is not automatically better than a rule. What learning buys is scale.
The simplest probabilistic learning method applies Bayes' rule to the words of a document. Naive Bayes classification builds it step by step.
References
- Manning, C. D., Raghavan, P., and Schütze, H. (2008). Introduction to Information Retrieval, chapter 13 introduction and section 13.1. Cambridge University Press. record · 102 pages
- Sahami, M., Dumais, S., Heckerman, D., and Horvitz, E. (1998). A Bayesian approach to filtering junk e-mail. In Learning for Text Categorization: Papers from the 1998 Workshop, AAAI Technical Report WS-98-05, 55–62. record · 2 pages
- Sebastiani, F. (2002). Machine learning in automated text categorization. ACM Computing Surveys, 34(1), 1–47. record · 3 pages
Reviewed: verified, 16 Nov 2026 · citations checked, 16 Nov 2026 · editorial review, 23 Nov 2026 · 9 revisions