如何使用 weka 实现决策树?
How can I use weka to implement a decision tree?
我正在使用 java、eclipse 和 weka,我想显示包含每个规则的树和一组数据的预测来测试我的决策树。
我正在尝试使用这段代码做一些事情,但它没有做我需要的事情,即显示所有可能的规则的树。我只能看到树的一部分而不是全部。
而且我还没有能够通过数据测试和数据训练来测试它,我认为这与我在文本和训练文件中使用的格式有关。
所以问题是,我怎样才能展示每一个可能的决策树然后测试它??
这是我目前拥有的:
public class Test {
public static BufferedReader readDataFile(String filename) {
BufferedReader inputReader = null;
try {
inputReader = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException ex) {
System.err.println("File not found: " + filename);
}
return inputReader;
}
public static void main(String[] args) throws Exception {
//Get File
BufferedReader reader = readDataFile("maitre.txt");
//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);
//Predictions with test and training set of data
BufferedReader datafile = readDataFile("maitre.txt");
BufferedReader testfile = readDataFile("maitretest.txt");
Instances train = new Instances(datafile);
data.setClassIndex(data.numAttributes() - 1); // from somewhere
Instances test = new Instances(testfile);
data.setClassIndex(data.numAttributes() - 1); // 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));
}
}
错误:
Exception in thread "main" weka.core.UnassignedClassException: weka.classifiers.trees.j48.C45PruneableClassifierTree: Class attribute not set!
at weka.core.Capabilities.test(Capabilities.java:1284)
at weka.core.Capabilities.test(Capabilities.java:1208)
at weka.core.Capabilities.testWithFail(Capabilities.java:1506)
at weka.classifiers.trees.j48.C45PruneableClassifierTree.buildClassifier(C45PruneableClassifierTree.java:120)
at weka.classifiers.trees.J48.buildClassifier(J48.java:293)
at Test.main(Test.java:60)
maitre.txt 和 maitretest.txt 是这样的:
@relation maitre
@attribute patrons {none, some, full}
@attribute waitEstation {0-10,10-30,30-60,>60}
@attribute reservation {TRUE, FALSE}
@attribute bar {TRUE, FALSE}
@attribute alternative {TRUE, FALSE}
@attribute sit {yes, no}
@data
some,0-10,TRUE,FALSE,TRUE,yes
full,30-60,FALSE,FALSE,TRUE,no
some,0-10,FALSE,TRUE,FALSE,yes
full,10-30,FALSE,FALSE,TRUE,yes
full,>60,TRUE,FALSE,TRUE,no
some,0-10,TRUE,TRUE,FALSE,yes
none,0-10,FALSE,TRUE,FALSE,no
some,0-10,TRUE,FALSE,FALSE,yes
full,>60,FALSE,TRUE,FALSE,no
full,10-30,TRUE,TRUE,TRUE,yes
none,0-10,FALSE,FALSE,FALSE,no
full,30-60,FALSE,TRUE,TRUE,no
是的,当然你的文件格式有问题。将这两个文件保存为 .arff 文件,然后重复该过程。是的,为此您需要使用 arffloader 读取 arff 文件....下面的代码将帮助您...
ArffLoader loader= new ArffLoader();
loader.setSource(new File("C:/Users/..../Desktop/maitre.arff"));
Instances data= loader.getDataSet();
祝你好运:)
此代码可能会有所帮助...
for(int i=0;i<Train.numInstances();i++)
{
double value=cls.classifyInstance(originalTrain.instance(i));
String prediction=Train.classAttribute().value((int)value);
System.out.println(Train.instance(i)+"............Prediction.......... "+prediction);
}
以上代码用于预测值。以下是您的示例 maitretest.arff 文件。希望对您有所帮助:)
@relation maitretest
@attribute patrons {none, some, full}
@attribute waitEstation {0-10,10-30,30-60,>60}
@attribute reservation {TRUE, FALSE}
@attribute bar {TRUE, FALSE}
@attribute alternative {TRUE, FALSE}
@attribute sit {yes, no}
@data
some,0-10,TRUE,FALSE,TRUE,?
full,30-60,FALSE,FALSE,TRUE,?
我正在使用 java、eclipse 和 weka,我想显示包含每个规则的树和一组数据的预测来测试我的决策树。
我正在尝试使用这段代码做一些事情,但它没有做我需要的事情,即显示所有可能的规则的树。我只能看到树的一部分而不是全部。
而且我还没有能够通过数据测试和数据训练来测试它,我认为这与我在文本和训练文件中使用的格式有关。
所以问题是,我怎样才能展示每一个可能的决策树然后测试它??
这是我目前拥有的:
public class Test {
public static BufferedReader readDataFile(String filename) {
BufferedReader inputReader = null;
try {
inputReader = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException ex) {
System.err.println("File not found: " + filename);
}
return inputReader;
}
public static void main(String[] args) throws Exception {
//Get File
BufferedReader reader = readDataFile("maitre.txt");
//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);
//Predictions with test and training set of data
BufferedReader datafile = readDataFile("maitre.txt");
BufferedReader testfile = readDataFile("maitretest.txt");
Instances train = new Instances(datafile);
data.setClassIndex(data.numAttributes() - 1); // from somewhere
Instances test = new Instances(testfile);
data.setClassIndex(data.numAttributes() - 1); // 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));
}
}
错误:
Exception in thread "main" weka.core.UnassignedClassException: weka.classifiers.trees.j48.C45PruneableClassifierTree: Class attribute not set!
at weka.core.Capabilities.test(Capabilities.java:1284)
at weka.core.Capabilities.test(Capabilities.java:1208)
at weka.core.Capabilities.testWithFail(Capabilities.java:1506)
at weka.classifiers.trees.j48.C45PruneableClassifierTree.buildClassifier(C45PruneableClassifierTree.java:120)
at weka.classifiers.trees.J48.buildClassifier(J48.java:293)
at Test.main(Test.java:60)
maitre.txt 和 maitretest.txt 是这样的:
@relation maitre
@attribute patrons {none, some, full}
@attribute waitEstation {0-10,10-30,30-60,>60}
@attribute reservation {TRUE, FALSE}
@attribute bar {TRUE, FALSE}
@attribute alternative {TRUE, FALSE}
@attribute sit {yes, no}
@data
some,0-10,TRUE,FALSE,TRUE,yes
full,30-60,FALSE,FALSE,TRUE,no
some,0-10,FALSE,TRUE,FALSE,yes
full,10-30,FALSE,FALSE,TRUE,yes
full,>60,TRUE,FALSE,TRUE,no
some,0-10,TRUE,TRUE,FALSE,yes
none,0-10,FALSE,TRUE,FALSE,no
some,0-10,TRUE,FALSE,FALSE,yes
full,>60,FALSE,TRUE,FALSE,no
full,10-30,TRUE,TRUE,TRUE,yes
none,0-10,FALSE,FALSE,FALSE,no
full,30-60,FALSE,TRUE,TRUE,no
是的,当然你的文件格式有问题。将这两个文件保存为 .arff 文件,然后重复该过程。是的,为此您需要使用 arffloader 读取 arff 文件....下面的代码将帮助您...
ArffLoader loader= new ArffLoader();
loader.setSource(new File("C:/Users/..../Desktop/maitre.arff"));
Instances data= loader.getDataSet();
祝你好运:)
此代码可能会有所帮助...
for(int i=0;i<Train.numInstances();i++)
{
double value=cls.classifyInstance(originalTrain.instance(i));
String prediction=Train.classAttribute().value((int)value);
System.out.println(Train.instance(i)+"............Prediction.......... "+prediction);
}
以上代码用于预测值。以下是您的示例 maitretest.arff 文件。希望对您有所帮助:)
@relation maitretest
@attribute patrons {none, some, full}
@attribute waitEstation {0-10,10-30,30-60,>60}
@attribute reservation {TRUE, FALSE}
@attribute bar {TRUE, FALSE}
@attribute alternative {TRUE, FALSE}
@attribute sit {yes, no}
@data
some,0-10,TRUE,FALSE,TRUE,?
full,30-60,FALSE,FALSE,TRUE,?