below is a code snippet for loading a decision tree and classifying some new instances using it.
//this file is a decision tree model file, created using the weka software
String model_file = "./tree.model";
// input new instances which we want to label
String descriptors_file = "./Not_Labeled.arff";
//instances labeled using the decision tree. this file will
//hold the classified category for each instance
String output_file = "./labeled.txt";
//loads the classifier
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream(model_file));
Classifier cls = (Classifier) ois.readObject();
ois.close();
//loads the input instances
Instances unlabeled = new Instances(
new BufferedReader(
new FileReader(descriptors_file)));
// set class attribute
unlabeled.setClassIndex(unlabeled.numAttributes() - 1);
// create copy
Instances labeled = new Instances(unlabeled);
// label instances
for (int i = 0; i < unlabeled.numInstances(); i++) {
clsLabel = cls.classifyInstance(unlabeled.instance(i));
labeled.instance(i).setClassValue(clsLabel);
String className=labeled.classAttribute().value((int)clsLabel);
}
BufferedWriter writer = new BufferedWriter(
new FileWriter(output_file));
for (int i = 0; i < unlabeled.numInstances(); i++) {
clsLabel = labeled.instance(i).classValue();
writer.write(labeled.classAttribute().value((int)clsLabel) + "\n");
}
writer.flush();
writer.close();