【问题标题】:Writing the results of Weka classifier to file in Java将 Weka 分类器的结果写入 Java 文件
【发布时间】:2012-10-03 21:21:13
【问题描述】:

我正在用 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 结果缓冲区的结果保存到程序中的文本文件中,以便在运行时将以下内容保存到文本文件中:

=== 分层交叉验证 === ===总结===

Correctly Classified Instances         229               40.1754 %
Incorrectly Classified Instances       341               59.8246 %
Kappa statistic                          0.2022
Mean absolute error                      0.1916
Root mean squared error                  0.3138
Relative absolute error                 80.8346 %
Root relative squared error             91.1615 %
Coverage of cases (0.95 level)          96.3158 %
 Mean rel. region size (0.95 level)      70.9774 %
Total Number of Instances              570     

=== Detailed Accuracy By Class ===

           TP Rate   FP Rate   Precision   Recall  F-Measure   ROC Area  Class
             0.44      0.012      0.786     0.44      0.564      0.76     Business and finance and economics
             0         0          0         0         0          0.616    Fashion and celebrity lifestyle
             0.125     0.01       0.667     0.125     0.211      0.663    Film
             0         0.002      0         0         0          0.617    Music
             0.931     0.78       0.318     0.931     0.474      0.633    News and current affairs
             0.11      0.006      0.786     0.11      0.193      0.653    Science and nature and technology
             0.74      0.012      0.86      0.74      0.796      0.85     Sport

加权平均。 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

dt 是我的一个类的一个实例,用来表示决策树的细节。

由于我正在运行大量分类器,这会有所帮助。

【问题讨论】:

    标签: java text-files weka


    【解决方案1】:

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

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

    【讨论】:

    • 我可以使用#toString 输出树。这是我所追求的分类结果。我已经编辑了问题以显示我的意思。
    【解决方案2】:

    是的,这可以做到。但是你需要在 Weka 中创建一个评估实例,并从实例中调用相应的方法:

    Evaluation eval = new Evaluation(data);
    eval.evaluateModel(j48DecisionTree, data);
    System.out.println(eval.toSummaryString("\nResults\n======\n", true));
    

    会给出一个总结。

    然后方法如:

    eval.pctCorrect();
    

    可以调用。请参阅Weka Javadoc 了解更多信息。

    【讨论】:

      猜你喜欢
      • 2015-01-03
      • 1970-01-01
      • 2013-04-29
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      • 2015-06-15
      • 2014-11-11
      • 2018-07-24
      相关资源
      最近更新 更多