2014-09-29 143 views
0

我想列出所有的预测。weka决策树java

我有这样的代码:

//Get File 
     BufferedReader reader = new BufferedReader(new FileReader(PATH + "TempArffFile.arff")); 

     //Get the data 
     Instances data = new Instances(reader); 
     reader.close(); 

     //Setting class attribute 
     data.setClassIndex(data.numAttributes() - 1); 

     //Make tree 
     J48 tree = new J48(); 
     String[] options = new String[1]; 
     options[0] = "-U"; 
     tree.setOptions(options); 
     tree.buildClassifier(data); 

     //Print tree 
     System.out.println(tree); 

它工作正常,我可以看到树印,但不知道如何使用,从这里工作。 我想为每个根做一个清单,我该怎么做?

回答

2

如果你想所有的测试预测的列表,你可以使用下面的代码(示例代码提供here):

import weka.core.Instances; 
import weka.classifiers.Evaluation; 
import weka.classifiers.trees.J48; 
... 
Instances train = ... // from somewhere 
Instances test = ... // from somewhere 
// train classifier 
Classifier cls = new J48(); 
cls.buildClassifier(train); 
// evaluate classifier and print some statistics 
Evaluation eval = new Evaluation(train); 
eval.evaluateModel(cls, test); 
System.out.println(eval.toSummaryString("\nResults\n======\n", false)); 

你也可以使用J48.classifyInstance()预测单个实例,如果你喜欢走那条路。

+0

那不是我想要的。 我有这个主题:title <= 1:bad(4.0) title> 1 | positionMatch <= 1 | | countryCode <= 1-> good | | countryCode> 1-> bad | positionMatch> 1->好。 现在我有一些项目,并希望检查它是否好。 title = 2,position = 0,country code = 0 =>所以很好。我怎么能在java中检查? – igor 2014-09-30 08:58:03

+0

你的意思是说你想要树中的所有规则?我很抱歉,但问题似乎要求预测。 – 2014-09-30 11:59:43

+0

我的错误我会问一个新的。你的代码确实适用于预测 – igor 2014-09-30 14:19:44