-
Notifications
You must be signed in to change notification settings - Fork 51
/
DecisionTree.java
105 lines (90 loc) · 3.23 KB
/
DecisionTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.gg.ml;
import java.io.File;
import java.io.IOException;
import weka.classifiers.trees.Id3;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.trees.J48;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
/**
* @author Gowtham Girithar Srirangasamy
*
*/
public class DecisionTreeDemo {
/** file names are defined*/
public static final String TRAINING_DATA_SET_FILENAME="decision-train.arff";
public static final String TESTING_DATA_SET_FILENAME="decision-test.arff";
/**
* This method is to load the data set.
* @param fileName
* @return
* @throws IOException
*/
public static Instances getDataSet(String fileName) throws IOException {
/**
* we can set the file i.e., loader.setFile("finename") to load the data
*/
int classIdx = 1;
/** the arffloader to load the arff file */
ArffLoader loader = new ArffLoader();
/** load the traing data */
loader.setSource(DecisionTreeDemo.class.getResourceAsStream("/" + fileName));
/**
* we can also set the file like loader3.setFile(new
* File("test-confused.arff"));
*/
//loader.setFile(new File(fileName));
Instances dataSet = loader.getDataSet();
/** set the index based on the data given in the arff files */
dataSet.setClassIndex(classIdx);
return dataSet;
}
/**
* This method is used to process the input and return the statistics.
*
* @throws Exception
*/
public static void process() throws Exception {
Instances trainingDataSet = getDataSet(TRAINING_DATA_SET_FILENAME);
Instances testingDataSet = getDataSet(TESTING_DATA_SET_FILENAME);
System.out.println("************************** J48 *************************");
/** Classifier here is Linear Regression */
Classifier classifier = new J48();
//J48,Id3
/** */
classifier.buildClassifier(trainingDataSet);
/**
* train the alogorithm with the training data and evaluate the
* algorithm with testing data
*/
Evaluation eval = new Evaluation(trainingDataSet);
eval.evaluateModel(classifier, testingDataSet);
/** Print the algorithm summary */
System.out.println("** Decision Tress Evaluation with Datasets **");
System.out.println(eval.toSummaryString());
System.out.print(" the expression for the input data as per alogorithm is ");
System.out.println(classifier);
System.out.println(eval.toMatrixString());
System.out.println(eval.toClassDetailsString());
System.out.println("************************** ID3 *************************");
/** Classifier here is Linear Regression */
Classifier id3Classifier = new Id3();
//J48,Id3
/** */
id3Classifier.buildClassifier(trainingDataSet);
/**
* train the alogorithm with the training data and evaluate the
* algorithm with testing data
*/
Evaluation evalId3 = new Evaluation(trainingDataSet);
evalId3.evaluateModel(id3Classifier, testingDataSet);
/** Print the algorithm summary */
System.out.println("** Decision Tress Evaluation with Datasets **");
System.out.println(evalId3.toSummaryString());
System.out.print(" the expression for the input data as per alogorithm is ");
System.out.println(id3Classifier);
System.out.println(evalId3.toMatrixString());
System.out.println(evalId3.toClassDetailsString());
}
}