如何在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 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.neweval = Evalution.new(newData)predictions = java.lange.StringBuffer.neweval.crossValidateModel(j48, newData, 10, Random.new(1), predictions, Range.new('1'), true)# 变量predictions现在包含所有单个预测的字符串

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注