Prabodha Dissanayake
Prabodha Dissanayake

Reputation: 181

How do I load a Weka model in Java?

I have saved the result of a Weka classification by right-clicking on the model and selecting "save model". Now, I want to load it and work with it in my Java application. How can I do that? Models could be naive Bayes, decision trees, or regression. I need to use these three models.

Any suggestion or solution would be appreciated.

Upvotes: 6

Views: 13935

Answers (2)

arutaku
arutaku

Reputation: 6087

If you saved a model to a file in WEKA, you can use it reading the generated java object. Here is an example with Random Forest classifier (previously saved to a file in WEKA):

RandomForest rf = (RandomForest) (new ObjectInputStream(PATH_TO_MODEL_FILE)).readObject();

Do not forget imports:

import weka.core.Instance;
import weka.core.Instances;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.classifiers.trees.RandomForest;

Upvotes: 7

iTech
iTech

Reputation: 18440

Here is an example assuming you have a RandomTree model saved to a model.weka file (change to whatever classifier and file you have)

RandomTree treeClassifier = (RandomTree) SerializationHelper.read(new FileInputStream("model.weka")));

Upvotes: 11

Related Questions