NaiveBayes most_informative_features ranks perfectly separating features as least informative (#184 still reproduces)
NaiveBayesClassifier.most_informative_features() computes the max and min of P(fname=fval|label) only over the labels whose training data contained fval (it iterates probdist.samples() per label). A feature value seen with just one label is therefore compared with that label alone and gets a ratio of 1.0, so a feature that perfectly separates the classes is ranked as the least informative one. show_most_informative_features() filters labels the same way and then skips these features (len(labels) == 1).
This was reported in #184, which was closed during a cleanup but still reproduces on develop:
>>> from nltk.classify import NaiveBayesClassifier
>>> c = NaiveBayesClassifier.train([({'viagra': True}, 'spam'), ({'hello': True}, 'ok')])
>>> c.show_most_informative_features()
Most Informative Features(only the header is printed).
Proposed fix (small, in nltk/classify/naivebayes.py): collect the values each feature name takes with any label, then compare P(fname=fval|label) over every label; with the default ELEProbDist an unseen value has a non-zero probability, so the ratio is well defined. The existing "drop features whose min probability is 0" rule stays, applied once after the loop. show_most_informative_features() would consider every label that has a distribution for the feature name. With that, the example above prints:
Most Informative Features
hello = None spam : ok = 3.0 : 1.0
hello = True ok : spam = 3.0 : 1.0
viagra = None ok : spam = 3.0 : 1.0
viagra = True spam : ok = 3.0 : 1.0and the three-example case in #184 prints exactly the output listed there as expected. The only change to existing doctest output is one extra line in the Naive Bayes section of classify.doctest (a = None x : y = 3.0 : 1.0, which follows from the ELE estimates: (1+0.5)/6.5 vs 0.5/6.5). The computation of the classifier itself is unchanged; only which labels the ranking/display compare.
I have a branch with this change and two regression tests from #184 (they fail on develop and pass with the change); the pre-commit hooks pass on the changed files. Per #3883 I'm posting it here for discussion first. Does this look like the right fix to you?
AI disclosure (per #3883): I used an AI coding assistant (Claude Code, model Claude Opus 5.5) while finding and writing the fix and tests; I reviewed the changes and ran the checks above. The bug was found by exercising NaiveBayesClassifier on small inputs and traced to the per-label samples() iteration.
Source: nltk/nltk