如何在WEKA中交叉验证后打印预测类
使用分类器完成10倍交叉验证后,如何打印出每个实例的预测类以及这些实例的分布?
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)));
}
我要做的是和WEKA GUI中的功能相同其中一旦训练了分类器,我就可以点击可视化分类器错误>保存
,然后我会在文件中找到预测的类。但是现在我需要它使用我自己的Java代码。
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);
然而它却提示我错误:
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
实例。
The crossValidateModel()
method can take a forPredictionsPrinting
varargs
parameter that is a weka.classifiers.evaluation.output.prediction.AbstractOutput
instance.
其中重要的部分是 StringBuffer
来保存所有预测的字符串表示。以下代码在未经测试的 JRuby
中,但您应该能够根据需要进行转换。
The important part of that is a StringBuffer
to hold a string representation of all the predictions. The following code is in untested JRuby
, but you should be able to convert it for your needs.
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