Skip to content

Chi-Squared Feature Selection for a Classifier

Chi-squared feature selection scores each nonnegative count feature by how far its per-class totals sit from what class sizes alone would predict. The scikit-learn function chi2 is described as "Compute chi-squared stats between each non-negative feature and class," and its documentation says the scores can be used to select the features with the highest values, with X containing "only non-negative integer feature values such as booleans or frequencies (e.g., term counts in document classification)." It adds: "If some of your features are continuous, you need to bin them." It also notes that because "the chi-square test measures dependence between stochastic variables, so using this function 'weeds out' the features that are the most likely to be independent of class and therefore irrelevant for classification."

What the function computes, from my reading of its source code and not from the documentation page: for each class and feature it adds up the feature's values over that class's rows, which gives the observed count. The expected count is the class's share of the rows times the feature's total across all rows. The score is the sum over classes of (observed − expected)² / expected, and the p-value comes from a chi-squared distribution with one fewer degree of freedom than the number of classes.

Try it by hand. Take 100 invented emails: 40 spam and 60 ham. The word "free" appears in 30 spam emails and 6 ham emails, 36 in all. If the word ignored the class, the 36 appearances would split in the class proportions, 40% and 60%: an expected 14.4 for spam and 21.6 for ham. The score is (30 − 14.4)² / 14.4 + (6 − 21.6)² / 21.6 = 16.9 + 11.267 = 28.17, exactly 169/6, with two classes so one degree of freedom, and a p-value of about 1.1e-7. The word "meeting" appears in 12 spam emails and 18 ham emails, 30 in all. The expected counts are 12 and 18, so the observed counts match them, the score is 0, and the p-value is 1. Selecting the top-scoring feature keeps "free" and drops "meeting." I computed these in exact fractions and checked them against scikit-learn 1.9.1's chi2 on a matrix built from this table, and the data are invented. One more detail from the same check: the score above counts only the cells where the word is present. A full two-by-two Pearson test that also counts the absent cells gives 44.01 for "free," so the two numbers are not interchangeable.

The trap: reading a high score as proof that the feature causes the class, or applying the score to any feature. The score measures how far the counts depart from the split the class sizes alone predict, nothing more, and so it can be large for a word that is a symptom of spam rather than a reason for it. The input must be nonnegative counts or indicators, as the documentation says, so a signed continuous feature needs a different tool or binning first. A caution of my own, not from the documentation: the usual chi-squared approximation is rough when expected counts are small, and if you score thousands of features, some will look significant by chance, so treat the ranking as a screen and not as a verdict.

Prof Rod's rule: use chi-squared to rank count features by association with the class, check that every feature is a nonnegative count, and keep the ranking on the training data only.

scikit-learn documentation, sklearn.feature_selection.chi2