将 weka 模型加载到 Java 代码中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14209510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loding a weka model to a java code
提问by Prabodha Dissanayake
I have saved the result of weka classification by right clicking on the model and selecting "save model". Now, I want to load it and work with in my "JAVA" application. How can I do that? Models could be Naive bias, Decision tree and regression. I need to use these three models.
我通过右键单击模型并选择“保存模型”来保存weka分类的结果。现在,我想加载它并在我的“JAVA”应用程序中使用它。我怎样才能做到这一点?模型可以是朴素偏差、决策树和回归。我需要使用这三个模型。
Any suggestion or solution would be appreciated.
任何建议或解决方案将不胜感激。
Thanks.
谢谢。
回答by iTech
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 模型保存到model.weka
文件中(更改为您拥有的任何分类器和文件)
RandomTree treeClassifier = (RandomTree) SerializationHelper.read(new FileInputStream("model.weka")));
回答by arutaku
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):
如果您在 WEKA 中将模型保存到文件中,则可以使用它来读取生成的 java 对象。以下是随机森林分类器的示例(之前保存在 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;