Feature selection keeps, for each class, the $k$ terms with the highest utility score computed from the term's two-by-two table with the class. Mutual information, $I = \sum_{i,j} p_{ij} \log_2 \frac{p_{ij}}{p_{i.}\,p_{.j}}$, lies between 0, reached only under independence, and the class entropy $H(C) \le 1$ bit; χ² reduces to $X^2 = N D^2/(N_{1.} N_{0.} N_{.1} N_{.0})$ with $D = N_{11} N_{00} - N_{10} N_{01}$. Both reward association in either direction, and χ² favors rarer terms. For grain on Reuters both rank wheat first (0.124 bits, $X^2 = 3{,}677.9$). With multinomial Naive Bayes on the ten largest Reuters categories, none of mutual information, χ² and frequency beat all 18,135 terms (0.835 micro-F1, 0.721 macro-F1). Mutual information dips to 0.680 micro-F1 at 50 features because it picks negatively associated terms; restricted to positive ones it gives 0.820 and 0.759 (read off the test set). Selection must use training labels only.
The grain classifier of Naive Bayes classification uses every one of the 18,135 distinct terms in the Reuters training stories. It does not need most of them. Wheat, tonne, corn, and USDA say a great deal about grain, while Tuesday and spokesman say nothing. This article shows how to find the terms that matter, and measures whether the classifier does as well with them alone.
Feature selection keeps a subset of the terms and discards the rest before the classifier is trained. It is a step of training, driven by the class labels.
Here is the route. First we set out why to select features, and the two-by-two table that every measure starts from. Then we derive the two standard measures, mutual information and χ², from their definitions to formulas in four counts, work both by hand on the term wheat, and compare them with plain frequency. Next we test what selection does to accuracy on Reuters. It does less than the textbook suggests, and a small experiment pins down why. Finally we see why selection must use training data only. The comparison of the two event models in the Bernoulli model and why Naive Bayes works is assumed.
Why select features
Feature selection serves two purposes (Manning, Raghavan, and Schütze, 2008, §13.5):
- Efficiency. Fewer features mean smaller models and faster training and classification. For methods whose cost grows with the number of features, the saving can be large.
- Removing noise features. A term that by chance occurs more often in one class's training documents can mislead the classifier on new documents, a form of overfitting. Removing such noise features can improve accuracy.
The usual procedure is simple. For each class, compute a utility score for every term, rank the terms by it, and keep the top $k$ (Manning, Raghavan, and Schütze, 2008, Figure 13.6). In a multilabel setting, each binary classifier gets its own set of features. That is how every experiment in this article is run.
A system with many classifiers may prefer one feature set shared by all of them, so that each document is represented once. The course text describes three ways to build it (Manning, Raghavan, and Schütze, 2008, §13.5.4):
- compute one statistic per term over all the classes at once, for example χ² for the table with one row per class and two columns, term present and term absent;
- compute the two-class score of the term for each class and combine the scores into one figure of merit, for example their average. Yang and Pedersen (1997) used both an average weighted by the class priors and the maximum over classes;
- select the top $k/n$ terms for each of the $n$ classifiers and pool the $n$ lists.
The text adds that accuracy often decreases with a shared set of $k$ features compared with $n$ separate sets of $k$, and that the common representation may be worth the loss. A one-of problem such as the star ratings of the Goodreads project, when it is solved by a single classifier that chooses among all the classes, needs a shared set, since every class's score must be computed over the same terms.
The measures differ in what "utility" means. Where does a utility score come from? All the measures in this article start from the same four numbers for each term and class, so it is worth setting them out first.
The two-by-two table
For a term $t$ and a class $c$, every training document falls into one of four cells, according to whether it contains the term and whether it belongs to the class:
| In class $c$ | Not in class $c$ | Total | |
|---|---|---|---|
| Contains $t$ | $N_{11}$ | $N_{10}$ | $N_{1.}$ |
| Lacks $t$ | $N_{01}$ | $N_{00}$ | $N_{0.}$ |
| Total | $N_{.1}$ | $N_{.0}$ | $N$ |
The first subscript records the term and the second the class. $N_{1.} = N_{11} + N_{10}$ is the number of documents containing the term, $N_{.1} = N_{11} + N_{01}$ the number in the class, $N_{0.} = N_{01} + N_{00}$ the number lacking the term, and $N_{.0} = N_{10} + N_{00}$ the number outside the class. For the term wheat and the Reuters category grain, counted over the 7,769 training stories:
| In grain | Not in grain | Total | |
|---|---|---|---|
| Contains wheat | 227 | 16 | 243 |
| Lacks wheat | 206 | 7,320 | 7,526 |
| Total | 433 | 7,336 | 7,769 |
Wheat appears in 243 stories, 227 of them about grain. A term that told nothing about the class would be spread across the columns in proportion to the column totals. Since 433/7,769 = 5.6 percent of stories are about grain, about 5.6 percent of the stories containing it would be, or 13.5 of 243. The measures below quantify how far the observed table departs from that.
Mutual information
How can "how far from independence" become one number? Mutual information (MI) answers with information theory. It measures how much knowing whether a term occurs in a document reduces uncertainty about whether the document belongs to the class. Let $U$ be a random variable that is 1 if the document contains term $t$ and 0 otherwise, and $C$ one that is 1 if the document is in class $c$ and 0 otherwise. Then
$$I(U; C) = \sum_{e_t \in \{1, 0\}} \sum_{e_c \in \{1, 0\}} P(U = e_t, C = e_c) \log_2 \frac{P(U = e_t, C = e_c)}{P(U = e_t)\, P(C = e_c)}.$$
The ratio inside the logarithm compares how often a cell occurs with how often it would occur if term and class were independent. The measure is zero when the term's occurrence is independent of the class, and it is never negative; both facts are proved below.
From the definition to four counts
The definition is in probabilities, and the training set supplies counts. Each probability is estimated by its relative frequency, which is its maximum likelihood estimate: the joint probability of a cell by the cell's count over $N$, and each marginal probability by the row or column total over $N$. For the cell with term value $e_t$ and class value $e_c$,
$$P(U = e_t, C = e_c) = \frac{N_{e_t e_c}}{N}, \qquad P(U = e_t) = \frac{N_{e_t .}}{N}, \qquad P(C = e_c) = \frac{N_{. e_c}}{N},$$
so the ratio inside the logarithm is
$$\frac{P(U = e_t, C = e_c)}{P(U = e_t)\,P(C = e_c)} = \frac{N_{e_t e_c}/N}{(N_{e_t .}/N)(N_{. e_c}/N)} = \frac{N\,N_{e_t e_c}}{N_{e_t .}\,N_{. e_c}}.$$
The two factors of $1/N$ in the denominator leave one factor of $N$ in the numerator. Each cell is paired with its own row total and column total: $N_{11}$ with $N_{1.}$ and $N_{.1}$, $N_{01}$ with $N_{0.}$ and $N_{.1}$, $N_{10}$ with $N_{1.}$ and $N_{.0}$, and $N_{00}$ with $N_{0.}$ and $N_{.0}$. Writing out the four terms of the definition gives the computing formula
$$I = \frac{N_{11}}{N} \log_2 \frac{N N_{11}}{N_{1.} N_{.1}} + \frac{N_{01}}{N} \log_2 \frac{N N_{01}}{N_{0.} N_{.1}} + \frac{N_{10}}{N} \log_2 \frac{N N_{10}}{N_{1.} N_{.0}} + \frac{N_{00}}{N} \log_2 \frac{N N_{00}}{N_{0.} N_{.0}}.$$
These are the two forms the course text gives (§13.5.1), and the second is the first with relative frequencies in place of probabilities. In terms of the four cell counts alone, $N_{1.} = N_{11} + N_{10}$, $N_{0.} = N_{01} + N_{00}$, $N_{.1} = N_{11} + N_{01}$, $N_{.0} = N_{10} + N_{00}$ and $N$ is the sum of all four.
A cell with a count of zero contributes zero, since $x \log x \to 0$ as $x \to 0$. The other three cells must still be included. Skipping the whole term when one cell is zero is a common implementation mistake.
Its smallest and largest values
Two bounds give the measure a scale. Write $p_{ij}$ for the four joint probabilities, $p_{i.}$ for the two probabilities of the term variable and $p_{.j}$ for the two of the class variable, so that $I = \sum_{i,j} p_{ij} \log_2 \frac{p_{ij}}{p_{i.}\,p_{.j}}$, summed over the cells with $p_{ij} > 0$.
Mutual information is never negative, and it is zero only under independence. The proof uses the inequality $\ln x \le x - 1$ for $x > 0$, with equality only at $x = 1$, that Naive Bayes classification used for maximum likelihood. Applying it to $x = p_{i.}\,p_{.j}/p_{ij}$ in each cell,
$$-I = \sum_{i,j} p_{ij} \log_2 \frac{p_{i.}\,p_{.j}}{p_{ij}} \le \frac{1}{\ln 2} \sum_{i,j} p_{ij} \Big( \frac{p_{i.}\,p_{.j}}{p_{ij}} - 1 \Big) = \frac{1}{\ln 2} \Big( \sum_{i,j:\, p_{ij} > 0} p_{i.}\,p_{.j} - 1 \Big) \le 0.$$
The last step holds because the products $p_{i.}\,p_{.j}$ over all four cells sum to $(p_{1.} + p_{0.})(p_{.1} + p_{.0}) = 1$. So $I \ge 0$. Equality in the first step requires $p_{ij} = p_{i.}\,p_{.j}$ in every cell with $p_{ij} > 0$. Equality in the second requires the products over those cells to sum to 1, so $p_{i.}\,p_{.j} = 0$ in any cell with $p_{ij} = 0$. Together they say $p_{ij} = p_{i.}\,p_{.j}$ in every cell, which is independence. Conversely, under independence every ratio inside the logarithm is 1 and $I = 0$. Individual cells can contribute negative amounts, as the worked example below shows, but the total cannot be negative.
Mutual information is at most the entropy of the class. The entropy of the class variable is $H(C) = -\sum_j p_{.j} \log_2 p_{.j}$, the uncertainty in bits about whether a document is in the class before anything is known about it. Split the logarithm, $\log_2 \frac{p_{ij}}{p_{i.}\,p_{.j}} = \log_2 \frac{p_{ij}}{p_{i.}} - \log_2 p_{.j}$, and sum the second part over $i$ first:
$$I = \sum_{i,j} p_{ij} \log_2 \frac{p_{ij}}{p_{i.}} - \sum_{j} p_{.j} \log_2 p_{.j} = H(C) + \sum_{i,j} p_{ij} \log_2 \frac{p_{ij}}{p_{i.}}.$$
Each ratio $p_{ij}/p_{i.}$ is a conditional probability, at most 1, so every term of the remaining sum is zero or negative and $I \le H(C)$. Equality requires every nonzero cell to have $p_{ij} = p_{i.}$, that is, each row of the table to have only one nonzero cell: knowing whether the term occurs then settles the class. The remaining sum is the negative of the uncertainty about the class that is left once the term is known, so mutual information is the reduction in uncertainty described above. By the symmetry of the definition, $I \le H(U)$ as well. (Mutual Information proves both bounds, and that the quantity is never negative, for variables with any finite number of values.) A two-valued variable has entropy at most 1 bit, by the same inequality $\ln x \le x - 1$:
$$H(C) - 1 = \sum_{j:\, p_{.j} > 0} p_{.j} \log_2 \frac{1}{2\,p_{.j}} \le \frac{1}{\ln 2} \sum_{j:\, p_{.j} > 0} p_{.j} \Big( \frac{1}{2\,p_{.j}} - 1 \Big) \le \frac{1}{\ln 2} \Big( \frac{2}{2} - 1 \Big) = 0,$$
with equality when $p_{.1} = p_{.0} = \tfrac{1}{2}$. So $I \le 1$ bit.
For grain, 433 of the 7,769 training stories are in the class, so $H(C) = -\tfrac{433}{7769} \log_2 \tfrac{433}{7769} - \tfrac{7336}{7769} \log_2 \tfrac{7336}{7769} = 0.310$ bits. No term can score more than that for grain.
Worked by hand: wheat and grain
Example (the cell that carries the score). Of the four cells of the wheat table, the one that contributes most to its mutual information is the cell of stories that contain wheat and are about grain. It holds 227 stories where independence predicts 13.5, nearly 17 times as many. The cell of wheat stories outside grain is far below its prediction too, but it holds only 16 stories, so its weight in the sum is small. Each cell of the wheat table contributes one term of the sum (computed by tools/experiments/reuters/fs_worked.py; ratios to four decimals, contributions to five):
| Cell | $N_{e_t e_c}/N$ | Ratio $N N_{e_t e_c} / (N_{e_t .} N_{. e_c})$ | $\log_2$ of ratio | Contribution |
|---|---|---|---|---|
| contains, in grain | 227/7,769 = 0.0292 | 7,769 × 227 / (243 × 433) = 16.7609 | +4.0670 | +0.11883 |
| contains, not in grain | 16/7,769 = 0.0021 | 0.0697 | −3.8421 | −0.00791 |
| lacks, in grain | 206/7,769 = 0.0265 | 0.4911 | −1.0259 | −0.02720 |
| lacks, not in grain | 7,320/7,769 = 0.9422 | 1.0300 | +0.0427 | +0.04023 |
| Total | 0.12395 bits |
The first cell dominates: stories containing wheat are 16.8 times as likely to be about grain as independence predicts. Two contributions are negative, because those cells occur less often than independence predicts. The total is positive, as the first bound requires. With 0.124 bits, wheat ranks first among all 18,135 terms for grain. That is 40 percent of the 0.310 bits a perfect predictor of grain would score.
The textbook's example
The textbook's own example shows how much the scale depends on the class. Manning, Raghavan, and Schütze (2008, §13.5.1) take the class poultry and the term export in the Reuters-RCV1 collection:
| In poultry | Not in poultry | Total | |
|---|---|---|---|
| Contains export | $N_{11} = 49$ | $N_{10} = 27{,}652$ | 27,701 |
| Lacks export | $N_{01} = 141$ | $N_{00} = 774{,}106$ | 774,247 |
| Total | 190 | 801,758 | 801,948 |
Cell by cell, as for wheat (probabilities to seven decimals, contributions to eight):
| Cell | $N_{e_t e_c}/N$ | Ratio $N N_{e_t e_c} / (N_{e_t .} N_{. e_c})$ | $\log_2$ of ratio | Contribution |
|---|---|---|---|---|
| contains, in poultry | 49/801,948 = 0.0000611 | 801,948 × 49 / (27,701 × 190) = 7.4661 | +2.9004 | +0.00017722 |
| contains, not in poultry | 0.0344810 | 0.9985 | −0.0022 | −0.00007629 |
| lacks, in poultry | 0.0001758 | 0.7687 | −0.3796 | −0.00006674 |
| lacks, not in poultry | 0.9652820 | 1.0001 | +0.0001 | +0.00007635 |
| Total | 0.00011054 bits |
So $I \approx 0.0001105$ bits, the textbook's value. Stories containing export are 7.5 times as likely to be about poultry as independence predicts, a strong association, yet the score is about a thousandth of wheat's. The second bound explains why. Only 190 of the 801,948 documents are about poultry, so $H(C) = 0.00320$ bits, and no term can score more than that for poultry. Export reaches 3.5 percent of the bound. When features are selected class by class, only the ranking of MI values among the terms of one class matters, so the difference in scale between classes does no harm. It does matter when per-class scores are averaged into one figure of merit for a shared feature set: the classes with the larger bound can then dominate the average.
Negative association counts too
A natural assumption is that a high score means a term occurs often in the class. It need not. Mutual information rewards any departure from independence, in either direction. The fourth-ranked term for grain shows what that means:
| In grain | Not in grain | Total | |
|---|---|---|---|
Contains lt |
2 | 4,316 | 4,318 |
Lacks lt |
431 | 3,020 | 3,451 |
lt is not a word. It is a fragment of the HTML entity <, which the Reuters text uses to encode the opening angle bracket before company names. It appears in more than half the training stories, and only 2 of them are about grain. Its MI of 0.06573 bits comes from the two cells that record negative association:
- grain stories lack it 2.24 times as often as independence predicts, which contributes +0.06458;
- other stories contain it slightly more often than predicted, +0.04559.
The other two cells subtract 0.04443. The presence of lt is evidence against grain. Its absence is mild evidence for it, since almost every grain story lacks it. For selection this distinction matters, as the experiment below shows.
Mutual information in this sense, an expectation over all four cells, is what other authors call information gain. The name "mutual information" is sometimes used instead for the pointwise quantity $\log \frac{P(t, c)}{P(t)\,P(c)}$, which looks at only one cell. In Yang and Pedersen's (1997) comparison, information gain was among the best selection measures. Pointwise mutual information was among the worst, because it favors rare terms. Comparisons across papers should check which quantity is meant.
The χ² statistic
Mutual information comes from information theory. Statistics offers a second way to measure departure from independence. (The two are relatives: Cross-Entropy and Kullback–Leibler Divergence proves that a divergence between two distributions that are close is about half a chi-square statistic, and the fifth worked problem of Mutual Information compares the two scores for wheat and from.) The χ² test of independence asks whether the term's occurrence and the class's occurrence are independent (The Chi-Square Test of Independence gives its theory for tables of any size). It compares the observed count in each cell with the count expected if they were:
$$X^2 = \sum_{e_t \in \{0, 1\}} \sum_{e_c \in \{0, 1\}} \frac{(N_{e_t e_c} - E_{e_t e_c})^2}{E_{e_t e_c}},$$
where the expected count $E_{e_t e_c}$ is $N$ times the product of the two marginal probabilities, which simplifies to the row total times the column total divided by $N$. For wheat and grain:
| Cell | Observed | Expected | $(O - E)^2 / E$ |
|---|---|---|---|
| contains, in grain | 227 | 243 × 433 / 7,769 = 13.54 | 3,364.3 |
| contains, not in grain | 16 | 243 × 7,336 / 7,769 = 229.46 | 198.6 |
| lacks, in grain | 206 | 7,526 × 433 / 7,769 = 419.46 | 108.6 |
| lacks, not in grain | 7,320 | 7,526 × 7,336 / 7,769 = 7,106.54 | 6.4 |
| Total | $X^2 = 3{,}677.9$ |
As with mutual information, the cell for documents that contain the term and are in the class carries most of the weight.
The direct formula, derived
For a two-by-two table the four cells collapse to a direct formula,
$$X^2 = \frac{N (N_{11} N_{00} - N_{10} N_{01})^2}{(N_{11} + N_{01})(N_{11} + N_{10})(N_{10} + N_{00})(N_{01} + N_{00})}.$$
The course text gives both forms (§13.5.2), and this is the derivation of the second from the first. Here is the idea. The observed and expected tables have the same row and column totals, so the four differences between them are one number with alternating signs. Factoring its square out of the sum leaves a sum of reciprocals that can be added up exactly.
The formal version. Write $D = N_{11} N_{00} - N_{10} N_{01}$. Assume all four totals $N_{1.}, N_{0.}, N_{.1}, N_{.0}$ are positive, so that every expected count is positive; otherwise $X^2$ is not defined.
Step 1: the first difference. Put $N_{11} - E_{11}$ over the denominator $N$ and expand, using $N = N_{11} + N_{10} + N_{01} + N_{00}$:
$$N_{11} - E_{11} = N_{11} - \frac{N_{1.}\,N_{.1}}{N} = \frac{N_{11}(N_{11} + N_{10} + N_{01} + N_{00}) - (N_{11} + N_{10})(N_{11} + N_{01})}{N}.$$
The numerator is $N_{11}^2 + N_{11}N_{10} + N_{11}N_{01} + N_{11}N_{00} - N_{11}^2 - N_{11}N_{01} - N_{10}N_{11} - N_{10}N_{01} = N_{11}N_{00} - N_{10}N_{01}$. So $N_{11} - E_{11} = D/N$.
Step 2: the other three differences. The expected counts in the first row sum to the row total: $E_{11} + E_{10} = N_{1.}(N_{.1} + N_{.0})/N = N_{1.}$, which is also $N_{11} + N_{10}$. Hence $N_{10} - E_{10} = -(N_{11} - E_{11}) = -D/N$. The first column gives $N_{01} - E_{01} = -(N_{11} - E_{11}) = -D/N$, from $E_{11} + E_{01} = N_{.1}(N_{1.} + N_{0.})/N = N_{.1} = N_{11} + N_{01}$. The second row then gives $N_{00} - E_{00} = -(N_{01} - E_{01}) = +D/N$, from $E_{01} + E_{00} = N_{0.} = N_{01} + N_{00}$. All four differences have the same magnitude $\lvert D \rvert / N$, so all four squared differences equal $D^2/N^2$.
Step 3: the sum of reciprocals. Each expected count is $E_{e_t e_c} = N_{e_t .} N_{. e_c}/N$, so
$$X^2 = \frac{D^2}{N^2} \sum_{e_t, e_c} \frac{1}{E_{e_t e_c}} = \frac{D^2}{N^2} \cdot N \Big( \frac{1}{N_{1.}} + \frac{1}{N_{0.}} \Big) \Big( \frac{1}{N_{.1}} + \frac{1}{N_{.0}} \Big) = \frac{D^2}{N} \cdot \frac{N}{N_{1.} N_{0.}} \cdot \frac{N}{N_{.1} N_{.0}} = \frac{N D^2}{N_{1.}\,N_{0.}\,N_{.1}\,N_{.0}},$$
using $1/N_{1.} + 1/N_{0.} = (N_{0.} + N_{1.})/(N_{1.}N_{0.}) = N/(N_{1.}N_{0.})$ and the same for the columns. Writing the four totals as sums of cells gives the formula above.
For wheat, $D = 227 \times 7{,}320 - 16 \times 206 = 1{,}658{,}344$ and $D/N = 213.46$. That is the common difference in the table: $227 - 13.54 = 213.46$ and $229.46 - 16 = 213.46$. The direct formula gives the same 3,677.9. Step 2 also shows why the table has one degree of freedom: with the totals fixed, one difference determines the other three. And $D$ is zero exactly when the table matches independence, and it is squared, so χ², like mutual information, rewards association in either direction.
The textbook's example, continued
For the textbook's export and poultry, cell by cell:
| Cell | Observed | Expected | $(O - E)^2 / E$ |
|---|---|---|---|
| contains, in poultry | 49 | 27,701 × 190 / 801,948 = 6.6 | 274.40 |
| contains, not in poultry | 27,652 | 27,701 × 801,758 / 801,948 = 27,694.4 | 0.07 |
| lacks, in poultry | 141 | 774,247 × 190 / 801,948 = 183.4 | 9.82 |
| lacks, not in poultry | 774,106 | 774,247 × 801,758 / 801,948 = 774,063.6 | 0.00 |
| Total | $X^2 = 284.3$ |
Every observed count differs from its expected count by $D/N = 42.44$, where 6.6 documents are expected to contain export and be about poultry and 49 are observed. The direct formula gives the same value, $X^2 \approx 284$, as the textbook reports. That is far above 10.83, the value that a χ² variable with one degree of freedom exceeds with probability 0.001, so independence is rejected at the 0.001 level. Export is strongly associated with poultry, as one might expect for a category about the poultry trade.
A test used as a score
χ² is a statistical test used here as a ranking score. There are statistical objections to that use, such as the many tests run at once and the missing continuity correction for a two-by-two table. They matter little, since only the ranking is used, not the significance level (Manning, Raghavan, and Schütze, 2008, §13.5.2). When the significance level is used, they matter: The False Discovery Rate and the Benjamini–Hochberg Procedure tests every Reuters term against grain with a correction for the many tests, and q-Values and the Share of True Nulls shows that χ² p-values can be far too small in the extreme tail, where those corrections work, so both use Fisher's exact test. A real weakness is that a test of significance favors rare terms. A term that occurs in only a few documents, all of them in the class, is highly significant but says little about most documents. So χ² selects more such rare terms than mutual information does (§13.5.5).
Frequency
Both measures take some arithmetic. Would simply counting do? Only in part. The simplest measure keeps the terms that occur in the most documents of the class, the $N_{11}$ cell alone. It is cheap, and it removes rare terms that are likely to be noise. But it also keeps common words that occur everywhere, which have no power to distinguish the class.
Frequency can be counted in two ways (Manning, Raghavan, and Schütze, 2008, §13.5.3). Document frequency is the number of the class's documents that contain the term, which is $N_{11}$. Collection frequency is the number of the term's tokens in the class's documents, the count $T_{ct}$ of Naive Bayes classification. The text recommends document frequency for the Bernoulli model, whose parameters are estimated from document counts, and collection frequency for the multinomial model, whose parameters are estimated from token counts. The frequency curve in this article's experiment uses document frequency with the multinomial model; collection frequency was not tried.
From shows the problem. It occurs in 210 of the 433 grain stories, but also in 2,782 others. The 210 is not far above the 166.8 grain stories expected by chance. Its MI is 0.00175 bits and its χ² 19.3, ranking 340th and 605th.
What each method selects
In practice the three lists share their core and differ at the edges. For the Reuters category grain, the ten terms each method chose from the training stories, after stemming:
| Method | Top ten terms |
|---|---|
| Mutual information | agricultur, corn, crop, depart, export, grain, lt, tonn, usda, wheat |
| χ² | agricultur, barlei, corn, crop, farmer, grain, maiz, tonn, usda, wheat |
| Frequency | agricultur, corn, export, from, grain, ha, mln, tonn, wheat, year |
χ² picks the most specific vocabulary, such as barley, maize, and farmer. Frequency picks generic words that happen to be common in grain stories, such as from, has (stemmed to ha), mln, and year. Mutual information's list includes lt, for the reason shown above: its absence is informative, and mutual information ranks it fourth. χ² gives it 564.3 and ranks it 13th, just outside its top ten. The example is a reminder that feature selection inherits every artifact of tokenization.
The textbook's remark that χ² prefers rarer terms can be seen by plotting every term's score against the number of training stories that contain it:
Under χ², the 50 selected terms have a median document frequency of 63; under mutual information, 178.5. The two lists share 37 of their 50 terms. The preference is relative, not extreme: none of χ²'s top 100 terms for grain occurs in fewer than six training stories.
The effect on accuracy
The lists look sensible, but with these three measures as they stand none of them makes a better classifier on Reuters than the full vocabulary does. Multinomial Naive Bayes was trained on the ten largest Reuters categories with the top $k$ features per category under each measure, and evaluated on the standard test stories.
Example (fifty features against all of them). With 50 features per category chosen by mutual information, the multinomial classifier scores 0.680 micro-F1, far below its all-term 0.835. The textbook reports that a few well-chosen features can beat the full vocabulary, so the opposite might have been expected. The curves show the whole picture:
| Features per category | MI micro / macro F1 | χ² micro / macro F1 | Frequency micro / macro F1 |
|---|---|---|---|
| 10 | 0.781 / 0.686 | 0.787 / 0.638 | 0.742 / 0.652 |
| 50 | 0.680 / 0.557 | 0.798 / 0.716 | 0.771 / 0.659 |
| 100 | 0.676 / 0.548 | 0.746 / 0.651 | 0.776 / 0.666 |
| 500 | 0.710 / 0.576 | 0.711 / 0.576 | 0.792 / 0.668 |
| 1,000 | 0.735 / 0.602 | 0.723 / 0.587 | 0.778 / 0.649 |
| 5,000 | 0.779 / 0.650 | 0.781 / 0.654 | 0.807 / 0.686 |
| All 18,135 | 0.835 / 0.721 |
With these three measures, no feature set beat using all the features. The textbook reports a different result on a larger Reuters collection, RCV1. There, multinomial Naive Bayes with 10 to 100 features selected by mutual information outperformed the same classifier with all features by about 0.1 in F1 (Manning, Raghavan, and Schütze, 2008, §13.5.1). That did not happen here, and the curves are not even monotonic. Mutual information does worst between 50 and 100 features, where χ² does best.
Why mutual information fails here: an experiment
What goes wrong with mutual information on Reuters is, on the evidence of the experiment below, the thing lt illustrates. Mutual information rewards negative association as well as positive, so it selects terms whose absence indicates the class. A multinomial classifier can only use the terms present in a document: a term the document lacks adds nothing to its score, as Naive Bayes classification shows. A negatively associated term in the feature set therefore contributes only when it is present, which is when it counts against the class, and it never contributes the positive evidence that made it rank highly. Such features waste much of a small budget. Among the top 50 terms by mutual information, 29 are negatively associated for earn, 15 for acq, and between 6 and 12 for each of the other eight categories.
The explanation predicts that restricting mutual information to positively associated terms, those with $N_{11} N_{00} > N_{10} N_{01}$, should remove the dip. It does:
| Features per category | MI, positive terms only, micro / macro F1 | MI, all terms, micro / macro F1 |
|---|---|---|
| 10 | 0.744 / 0.635 | 0.781 / 0.686 |
| 50 | 0.820 / 0.759 | 0.680 / 0.557 |
| 100 | 0.822 / 0.747 | 0.676 / 0.548 |
| 500 | 0.798 / 0.707 | 0.710 / 0.576 |
| 1,000 | 0.788 / 0.695 | 0.735 / 0.602 |
| 5,000 | 0.799 / 0.705 | 0.779 / 0.650 |
| 10,000 | 0.834 / 0.724 | 0.802 / 0.681 |
At 50 features per category, the restricted measure scores 0.820 micro-F1 and 0.759 macro-F1, against 0.680 and 0.557 for unrestricted mutual information. Its macro-F1 is above the full vocabulary's 0.721. So on Reuters, once the measure suits the classifier, selection does improve the small categories, as in the textbook's experiment. Micro-F1 stays slightly below the full vocabulary's 0.835.
One caution applies. These numbers were read off the test set at a handful of values of $k$, so the best of them is an optimistic estimate. The point is the shape of the curves: the restricted measure beats unrestricted mutual information at every size from 20 to 10,000 features. The Bernoulli model uses absent terms as well as present ones. It has no such problem with mutual information, and its accuracy is highest with 10 to 20 features.
What selection buys
The results still show the main practical value of selection. With only 50 features per category, 0.3 percent of the vocabulary, χ² selection came within 0.006 of the full model's macro-F1 (0.716 against 0.721). It was 0.037 below it on micro-F1, and the result was sensitive to the exact number of features. Yang and Pedersen (1997) reported the same finding with other classifiers: information gain and χ² could remove the great majority of terms, up to 98 percent of them with a nearest-neighbor classifier on Reuters, with no loss of accuracy.
Selection buys efficiency cheaply. Whether it also improves accuracy depends on the classifier, the collection, and the measure. Forman (2003), in a larger comparison, found a measure called bi-normal separation best overall, especially for highly skewed classes. Information gain was a strong choice when only a few features are kept. The Bernoulli model, whose scores multiply over every vocabulary term, benefits from selection far more than the multinomial model does.
Selecting on training data only
One rule applies to every measure here. Feature selection uses the class labels, so it is part of training. Suppose features are selected using all the labeled data, and the data is then split for training and testing or for cross-validation. The test labels have then influenced which features the classifier sees, and test results overstate how the classifier will do on new data. Selection must be repeated inside each training split, using only that split's labels (see evaluating classifiers). All the tables in this article were computed this way: the counts come from the 7,769 training stories only.
Worked problems
Problems 1 to 4 are Exercises 13.5, 13.11, 13.13 and 13.16 of the course text (Manning, Raghavan, and Schütze, 2008), restated in our own words with their data. Problem 5 is posed on this article's Reuters counts. Exercises 13.14 and 13.15 of the text, the equivalence of the two χ² formulas and the equal magnitude of the four differences, are proved in the body above. The script tools/experiments/reuters/fs_worked.py recomputes every number in the problems except Problem 5's ranks, which come from reuters_more.py.
Problem 1. For the class coffee in the first 100,000 documents of Reuters-RCV1, four terms have these counts:
| Term | $N_{00}$ | $N_{01}$ | $N_{10}$ | $N_{11}$ |
|---|---|---|---|---|
| brazil | 98,012 | 102 | 1,835 | 51 |
| council | 96,322 | 133 | 3,525 | 20 |
| producers | 98,524 | 119 | 1,118 | 34 |
| roasted | 99,824 | 143 | 23 | 10 |
Select two of the four terms by (i) χ², (ii) mutual information, (iii) frequency.
Solution. First a correction to the data. Three of the rows sum to 100,000, and every row has $N_{01} + N_{11} = 153$ coffee documents, but the producers row sums to 99,795, which is 205 documents short. The row is computed below as printed, with its own $N = 99{,}795$, and again with the shortfall added to its largest cell ($N_{00} = 98{,}729$), which is an assumption. The selections come out the same both ways.
Each score comes from the computing formulas of this article:
| Term | $E_{11}$ | χ² | Mutual information (bits) | $N_{11}$ |
|---|---|---|---|---|
| brazil | 2.89 | 818.9 | 0.001554 | 51 |
| council | 5.42 | 40.7 | 0.000177 | 20 |
| producers, as printed | 1.77 | 596.1 | 0.001048 | 34 |
| producers, corrected | 1.76 | 597.4 | 0.001047 | 34 |
| roasted | 0.05 | 1,964.3 | 0.000648 | 10 |
For brazil, for example, $N_{1.} = 1{,}835 + 51 = 1{,}886$ and $N_{.1} = 153$, so $E_{11} = 1{,}886 \times 153/100{,}000 = 2.89$, and the direct formula gives
$$X^2 = \frac{100{,}000 \times (51 \times 98{,}012 - 1{,}835 \times 102)^2}{153 \times 1{,}886 \times 99{,}847 \times 98{,}114} = 818.9.$$
(i) By χ² the two terms are roasted and brazil. (ii) By mutual information they are brazil and producers. (iii) By document frequency in the class, $N_{11}$, they are brazil and producers. The data do not give token counts, so collection frequency cannot be computed.
The disagreement over roasted is the difference between the measures in miniature. Roasted occurs in only 33 documents, and 10 of them are about coffee where 0.05 would be expected, so the departure from independence is extremely significant and χ² ranks it first. But it is present in only 10 of the 153 coffee documents, so knowing whether it occurs says little about most documents, and mutual information ranks it third.
Problem 2. Find the values of mutual information and of χ² when the term and the class are completely independent, and when they are completely dependent.
Solution. Independent. Every observed count equals its expected count, so every ratio inside a logarithm is 1 and $I = 0$, and every difference $N_{e_t e_c} - E_{e_t e_c}$ is zero, so $X^2 = 0$. Equivalently $D = N_{11}N_{00} - N_{10}N_{01} = 0$.
Completely dependent. Suppose the term occurs in exactly the documents of the class, so $N_{10} = N_{01} = 0$. Then knowing the term settles the class, and by the second bound $I = H(C)$, the entropy of the class. For χ², the totals are $N_{1.} = N_{.1} = N_{11}$ and $N_{0.} = N_{.0} = N_{00}$, so the direct formula gives
$$X^2 = \frac{N\,(N_{11} N_{00})^2}{N_{11} \cdot N_{11} \cdot N_{00} \cdot N_{00}} = N.$$
The other complete dependence, a term that occurs in exactly the documents outside the class ($N_{11} = N_{00} = 0$), gives the same two values, since both measures are unchanged when the two rows are exchanged. For grain, a term occurring in exactly the 433 grain stories would score $I = 0.310$ bits and $X^2 = 7{,}769$. Wheat, the best real term, reaches 0.124 bits and 3,677.9, which are 40 and 47 percent of those maxima.
Problem 3. Information gain is defined as the entropy of the class distribution in the whole training set, minus the weighted average of the entropies of the class distribution in the documents that contain the term and in those that do not, each weighted by its share of the documents. Show that information gain equals mutual information.
Solution. In the notation of the bounds above, the documents with term value $i$ are a share $p_{i.}$ of the training set, and within them the class distribution is $p_{ij}/p_{i.}$ for $j = 1, 0$. The entropy of that distribution is $-\sum_j \frac{p_{ij}}{p_{i.}} \log_2 \frac{p_{ij}}{p_{i.}}$, so the weighted average of the two entropies is
$$\sum_i p_{i.} \Big( -\sum_j \frac{p_{ij}}{p_{i.}} \log_2 \frac{p_{ij}}{p_{i.}} \Big) = -\sum_{i,j} p_{ij} \log_2 \frac{p_{ij}}{p_{i.}}.$$
Information gain is therefore $H(C) + \sum_{i,j} p_{ij} \log_2 \frac{p_{ij}}{p_{i.}}$, which is exactly the expression for $I$ obtained in the proof of the second bound. For wheat: $H(C) = 0.31027$ bits. Among the 243 stories containing wheat, 227 are about grain, an entropy of 0.35022 bits, and among the 7,526 without it, 206 are, an entropy of 0.18104 bits. The weighted average is $(243 \times 0.35022 + 7{,}526 \times 0.18104)/7{,}769 = 0.18633$, and $0.31027 - 0.18633 = 0.12394$ bits. With unrounded entropies the difference is 0.12395 bits, the mutual information of the worked table.
Problem 4. Mutual information and χ² do not distinguish terms that are positively associated with the class from terms that are negatively associated. Give a way to rule out the negative indicators.
Solution. Keep a term only if it occurs in a larger share of the class's documents than of the other documents, $N_{11}/N_{.1} > N_{10}/N_{.0}$, and rank the terms that pass by the measure as before. The condition has an equivalent form in the quantity $D$ of the χ² derivation. Multiplying out, $N_{11} N_{.0} > N_{10} N_{.1}$ reads $N_{11}(N_{10} + N_{00}) > N_{10}(N_{11} + N_{01})$, and cancelling $N_{11}N_{10}$ from both sides leaves $N_{11} N_{00} > N_{10} N_{01}$, that is, $D > 0$. By step 1 of that derivation this is the same as $N_{11} > E_{11}$: the term and the class occur together more often than independence predicts. For wheat, $D = 1{,}658{,}344 > 0$ and the term is kept. For lt, $D = 2 \times 3{,}020 - 4{,}316 \times 431 = -1{,}854{,}156 < 0$ and the term is dropped. This is the restriction used in the experiment above, where it removed the dip in the mutual information curve.
Problem 5. The term from occurs in 210 of the 433 grain training stories and in 2,782 of the 7,336 others. Compute its mutual information and χ² with grain cell by cell, and say whether its association with grain is statistically significant at the 0.001 level.
Solution. The table is $N_{11} = 210$, $N_{10} = 2{,}782$, $N_{01} = 433 - 210 = 223$ and $N_{00} = 7{,}336 - 2{,}782 = 4{,}554$, with $N_{1.} = 2{,}992$ and $N_{0.} = 4{,}777$.
| Cell | Ratio | $\log_2$ of ratio | MI contribution | Expected | $(O - E)^2/E$ |
|---|---|---|---|---|---|
| contains, in grain | 1.2593 | +0.3326 | +0.00899 | 166.8 | 11.21 |
| contains, not in grain | 0.9847 | −0.0223 | −0.00797 | 2,825.2 | 0.66 |
| lacks, in grain | 0.8376 | −0.2557 | −0.00734 | 266.2 | 7.02 |
| lacks, not in grain | 1.0096 | +0.0138 | +0.00807 | 4,510.8 | 0.41 |
| Total | 0.00175 bits | 19.3 |
Each observed count differs from its expected count by $D/N = 43.24$. The mutual information is 0.00175 bits, under 1 percent of the 0.310-bit maximum, and the term ranks 340th. The χ² value of 19.3 is above 10.83, so the association is significant at the 0.001 level: from really is somewhat more common in grain stories, 210 against an expected 166.8. It still ranks 605th by χ². Significance says that an association exists, not that it is strong enough to be useful, which is the caution of the section on χ² as a score.
What we have learned
Every selection measure starts from a term's two-by-two table with a class. Mutual information and χ² both score departures from independence in either direction, and both rank wheat first for grain. Frequency keeps common words such as from. χ² leans toward rarer terms, and mutual information also rewards terms like lt whose absence signals the class. On Reuters no measure beat the full vocabulary for the multinomial model, until mutual information was restricted to positively associated terms.
Every number so far has been a micro- or macro-averaged F1 on a held-out test set. Evaluating classifiers defines the two averages and shows why they disagree: one follows the large categories and the other weighs every category equally.
References
- Forman, G. (2003). An extensive empirical study of feature selection metrics for text classification. Journal of Machine Learning Research, 3, 1289–1305. record
- Manning, C. D., Raghavan, P., and Schütze, H. (2008). Introduction to Information Retrieval, section 13.5. Cambridge University Press. record · 102 pages
- Yang, Y., and Pedersen, J. O. (1997). A comparative study on feature selection in text categorization. In Proceedings of ICML 1997, 412–420. record
Reviewed: verified, 16 Nov 2026 · citations checked, 16 Nov 2026 · editorial review, 23 Nov 2026 · 14 revisions