gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:12:21 25 4
gpt4 key购买 nike

用分类器完成 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 classifier error"> Save,然后我会找到预测的文件中的类。但现在我需要它来处理我自己的 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 - 如何在 WEKA 中打印出交叉验证后的预测类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7314684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com