Search code examples
pythontextclassificationnltk

python nltk naive bayes probabilities


Is there a way to get at the individual probabilities using nltk.NaiveBayesClassifier.classify? I want to see the probabilities of classification to try and make a confidence scale. Obviously with a binary classifier the decision is going to be one or the other, but is there some way to see the inner workings of how the decision was made? Or, do I just have to write my own classifier?

Thanks


Solution

  • How about nltk.NaiveBayesClassifier.prob_classify?

    http://nltk.org/api/nltk.classify.html#nltk.classify.naivebayes.NaiveBayesClassifier.prob_classify

    classify calls this function:

    def classify(self, featureset):
        return self.prob_classify(featureset).max()
    

    Edit: something like this should work (not tested):

    dist = classifier.prob_classify(features)
    for label in dist.samples():
        print("%s: %f" % (label, dist.prob(label)))