ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 如何在WEKA中交叉验证后打印预测类

2019-06-30 14:00:33  阅读:252  来源: 互联网

标签:java machine-learning weka decision-tree validation


使用分类器完成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中相同的功能,其中一旦训练了分类器,我就可以点击Visualize分类器错误“>保存,我将在文件中找到预测的类.但现在我需要它在我自己的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

解决方法:

crossValidateModel()方法可以使用forPredictionsPrinting varargs参数,该参数是weka.classifiers.evaluation.output.prediction.AbstractOutput实例.

其中重要的部分是StringBuffer来保存所有预测的字符串表示.以下代码在未经测试的JRuby中,但您应该能够根据需要进行转换.

j48 = j48.new
eval = Evalution.new(newData)
predictions = java.lange.StringBuffer.new
eval.crossValidateModel(j48, newData, 10, Random.new(1), predictions, Range.new('1'), true)
# variable predictions now hold a string of all the individual predictions

标签:java,machine-learning,weka,decision-tree,validation
来源: https://codeday.me/bug/20190630/1337358.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有