3
votes

I saved the train model through weka like explained in this LINK

Now i want to load this model in python program and try to test the queries with the help of this model. So i have file called "naivebayes.model" as the saved naive bayes multinomial updatable classifier. I tried the below code with the help of python-weka wrapper. But I am not sure if the model is getting loaded or not

from weka.core.converters import Loader, Saver
import weka.core.jvm as jvm
from weka.classifiers import Classifier, Evaluation

#starting JVM
jvm.start()

classifier = Classifier(classname="weka.classifiers.bayes.NaiveBayesMultinomialUpdateable", options=['-l','naivebayes.model'])
print(classifier)
print (dir(classifier))

#stopping JVM
jvm.stop()

Can anyone please tell me the rite way to do this. Help is appreciated.

1

1 Answers

3
votes

The -l option is not an option that is parsed by a classifier, but by Weka's Evaluation class. If you want to load a serialized model, you have to deserialize it manually. You can do this as follows:

import weka.core.serialization as serialization
from weka.classifiers import Classifier
objects = serialization.read_all("naivebayes.model")
classifier = Classifier(jobject=objects[0])
print(classifier)

The above code assumes that the model was serialized with Weka, as it stores two objects in the file, the model and the dataset header. The above code was taken from the python-weka-wrapper documentation.