4

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 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.