Search code examples
javawekanaivebayes

Why doesn't weka NaiveBayes class implements classifyInstance method?


I'm having trouble with classifying an instance with a trained naive bayes model in Weka. I'm using the java framework for that. I've already trained the model and was able to generate the distribution for an instance. My doubt here is, since the NaiveBayes class doesn't implement the classifyInstance method, it just herds it from the AbstractClassifier abstract class, is it implicit that I should implement it with my own rules?


Solution

  • My doubt here is, since the NaiveBayes class doesn't implement the classifyInstance method, it just herds it from the AbstractClassifier abstract class, is it implicit that I should implement it with my own rules?

    No, you shouldn't.

    The AbstractClassifier documentation states:

    Abstract classifier. All schemes for numeric or nominal prediction in Weka extend this class. Note that a classifier MUST either implement distributionForInstance() or classifyInstance().

    Looking at the source code of AbstractClassifier shows us that classifyInstance calls distributionForInstance and vice versa. So if a class inherits from AbstractClassifier and does not override at least one of these two methods, this would lead to an infinite recursion and a stack overflow.

    NaiveBayes does implement distributionForInstance and the classifyInstance method inherited from AbstractClassifier will use this it. It returns the class index with the highest probability if the class attribute is nominal.