用Java将Weka分类器的结果写入文件

用Java将Weka分类器的结果写入文件,java,text-files,weka,Java,Text Files,Weka,我用Java代码在Weka中生成决策树,如下所示: J48 j48DecisionTree = new J48(); Instances data = null; data = new Instances(new BufferedReader(new FileReader(dt.getArffFile()))); data.setClassIndex(data.numAttributes() - 1

我用Java代码在Weka中生成决策树,如下所示:

        J48 j48DecisionTree = new J48();   
        Instances data = null;
        data = new Instances(new BufferedReader(new FileReader(dt.getArffFile())));              
        data.setClassIndex(data.numAttributes() - 1);
        j48DecisionTree.buildClassifier(data);
我是否可以将Weka结果缓冲区的结果保存到程序中的文本文件中,以便在运行时将以下内容保存到文本文件中:

==分层交叉验证=== ==摘要===

加权平均值0.402 0.224 0.465 0.402 0.316 0.667

=== Confusion Matrix ===

  a   b   c   d   e   f   g   <-- classified as
 22   0   0   0  25   2   1 |   a = Business and finance and economics
  0   0   1   0  59   0   0 |   b = Fashion and celebrity lifestyle
  0   0  10   1  69   0   0 |   c = Film
  0   0   1   0  69   0   0 |   d = Music
  5   0   2   0 149   0   4 |   e = News and current affairs
  1   0   0   0  87  11   1 |   f = Science and nature and technology
  0   0   1   0  11   1  37 |   g = Sport
==混淆矩阵===

Weka分类器有一个扩展的
#toString()
方法,它提供了一个人类可读的表示,在本例中是树。您还可以使用
#toSource(String)
为决策树获取等效的Java代码


如果要存储模型以供以后重新使用,请查看
weka.core.SerializationHelper

是的,可以这样做。但您需要在Weka中创建一个求值实例,并从该实例调用适当的方法:

Evaluation eval = new Evaluation(data);
eval.evaluateModel(j48DecisionTree, data);
System.out.println(eval.toSummaryString("\nResults\n======\n", true));
我将做一个总结

但是,还有一些方法,例如:

eval.pctCorrect();

可以称之为。有关更多信息,请参阅。

我可以使用#toString输出树。这是我想要的分类结果。我对问题进行了编辑,以显示我所指的数据。
eval.pctCorrect();