编程语言
首页 > 编程语言> > java – 如何在WEKA中交叉验证后打印预测类

java – 如何在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中相同的功能,其中一旦训练了分类器,我就可以点击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