如何在WEKA中打印交叉验证后的预测类别
使用分类器完成 10 倍交叉验证后,如何打印出每个实例的预测类以及这些实例的分布?
J48 j48 = new J48();
Evaluation eval = new Evaluation(newData);
eval.crossValidateModel(j48, newData, 10, new Random(1));
当我尝试类似于下面的内容时,它说分类器未构建。
for (int i=0; i<data.numInstances(); i++){
System.out.println(j48.distributionForInstance(newData.instance(i)));
}
我想要做的功能与 WEKA GUI 中的功能相同,其中训练分类器后,我可以单击“可视化分类器错误”>“保存”,然后我将在中找到预测的类但现在我需要它在我自己的 Java 代码中工作,
我尝试了如下所示:
J48 j48 = new J48();
Evaluation eval = new Evaluation(newData);
StringBuffer forPredictionsPrinting = new StringBuffer();
weka.core.Range attsToOutput = null;
Boolean outputDistribution = new Boolean(true);
eval.crossValidateModel(j48, newData, 10, new Random(1), forPredictionsPrinting, attsToOutput, outputDistribution);
但它提示了我错误:
Exception in thread "main" java.lang.ClassCastException: java.lang.StringBuffer cannot be cast to weka.classifiers.evaluation.output.prediction.AbstractOutput
Once a 10-fold cross-validation is done with a classifier, how can I print out the prediced class of every instance and the distribution of these instances?
J48 j48 = new J48();
Evaluation eval = new Evaluation(newData);
eval.crossValidateModel(j48, newData, 10, new Random(1));
When I tried something similar to below, it said that the classifier is not built.
for (int i=0; i<data.numInstances(); i++){
System.out.println(j48.distributionForInstance(newData.instance(i)));
}
What I'm trying to do is the same function as in the WEKA GUI wherein once a classifier is trained, I can click on Visualize classifier error" > Save
, and I will find the predicted class in the file. But now I need it in to work in my own Java code.
I have tried something like below:
J48 j48 = new J48();
Evaluation eval = new Evaluation(newData);
StringBuffer forPredictionsPrinting = new StringBuffer();
weka.core.Range attsToOutput = null;
Boolean outputDistribution = new Boolean(true);
eval.crossValidateModel(j48, newData, 10, new Random(1), forPredictionsPrinting, attsToOutput, outputDistribution);
Yet it prompts me the error:
Exception in thread "main" java.lang.ClassCastException: java.lang.StringBuffer cannot be cast to weka.classifiers.evaluation.output.prediction.AbstractOutput
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
crossValidateModel()
方法可以采用forPredictionsPrinting
varargs
参数,该参数是weka.classifiers.evaluation.output.prediction.AbstractOutput< /代码> 实例。
其中重要的部分是一个 StringBuffer 来保存所有预测的字符串表示形式。以下代码是未经测试的
JRuby
代码,但您应该能够根据您的需要对其进行转换。The
crossValidateModel()
method can take aforPredictionsPrinting
varargs
parameter that is aweka.classifiers.evaluation.output.prediction.AbstractOutput
instance.The important part of that is a
StringBuffer
to hold a string representation of all the predictions. The following code is in untestedJRuby
, but you should be able to convert it for your needs.前几天我被困住了。我想使用矩阵而不是从 arff 文件加载来评估 matlab 中的 Weka 分类器。我使用 http://www.mathworks.com/matlabcentral/fileexchange/21204 -matlab-weka-interface 和以下源代码。我希望这对其他人有帮助。
阿斯德鲁巴尔·洛佩斯·乔
I was stuck some days ago. I wanted to to evaluate a Weka classifier in matlab using a matrix instead of loading from an arff file. I use http://www.mathworks.com/matlabcentral/fileexchange/21204-matlab-weka-interface and the following source code. I hope this help someone else.
Asdrúbal López-Chau