Writing the results of Weka classifier to file in Java

孤街浪徒 提交于 2019-12-24 18:34:52

问题


I am generating decision trees in Weka in Java code as follows:

        J48 j48DecisionTree = new J48();   
        Instances data = null;
        data = new Instances(new BufferedReader(new FileReader(dt.getArffFile())));              
        data.setClassIndex(data.numAttributes() - 1);
        j48DecisionTree.buildClassifier(data);

Can I save the results of the Weka results buffer to a text file in the program, such that the following can be saved at run-time to a text file:

=== Stratified cross-validation === === Summary ===

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

Weighted Avg. 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 is an instance of a class of mine to represent decision tree details.

As I'm running a large number of classifiers, this would help somewhat.


回答1:


The Weka classifiers have an extensive #toString() method, which gives you a human readable representation, in this case the tree. You can also use #toSource(String) to get a Java code equivalent for the decision tree.

If you want to store the model for re-using it later, have a look at weka.core.SerializationHelper.




回答2:


Yes, this can be done. But you need to create an instance of Evaluation in Weka and call the appropriate methods from the instance:

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

Will give a summary.

But then methods such as:

eval.pctCorrect();

Can be called. See Weka Javadoc for further info.



来源:https://stackoverflow.com/questions/12699059/writing-the-results-of-weka-classifier-to-file-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!