-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDT.java
More file actions
209 lines (191 loc) · 4.89 KB
/
Copy pathDT.java
File metadata and controls
209 lines (191 loc) · 4.89 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// Yunyun Chen
// Platform: MacOS
// Language/Environment: java
//
public class DT extends Classifier implements OptionHandler{
static int x = 10;//x-fold cross validation
static boolean hasTrainingSet = false;
static boolean hasTestSet = false;
static DataSet train;
public static int classIndex;
static String[] options;
Double z = 0.6925; //by default
Node root = new Node();
public static void main(String args[]) throws Exception
{
DT dt = new DT(args);
TrainTestSets TTS = new TrainTestSets(options);
Evaluator evaluator = new Evaluator(dt,TTS);
if(hasTestSet)
{
evaluator.holdOut();
}
else{
evaluator.kFold(x);
}
//System.out.println(TTS.toString());
}
DT(String[] args)
{
setOptions(args);
}
public void train(DataSet dataset) throws Exception
{
train=dataset;
classIndex = dataset.attributes.classIndex;
root.setCount(train);
build(root,train);
Prune(root); //prune from root
}
void outputAttribute(Node node)
{
//just for testing this program
System.out.println(node.attributeIndex);
for(int i =0;i<node.childnode.size();i++)
outputAttribute(node.childnode.get(i));
}
void build(Node node, DataSet train) throws Exception
{
if(node.isHomogeneous||train.examples.size()<=1)
{
return;
}
int bestAttribute = train.getBestAttribute();
if(bestAttribute == -1) return;//in the get BestAttribute function, if the biggest Gain Ratio
//equals 0, return -1.
//System.out.println("Best Attribute: "+bestAttribute+" @DT::build");
node.attributeIndex = bestAttribute;
//System.out.println(node.attributeIndex);
DataSet[] datasets = train.partitiononAttribute(bestAttribute);
for(int i=0;i<train.attributes.get(bestAttribute).getSize();i++)
{
Node c = new Node();
c.setCount(datasets[i]);
if(datasets[i].examples.size()==0)
c.setClassLable(node.getClassLable());// if there are no corresponding example for this value
// use the majority class label of the parent
node.addChild(c);
build(c,datasets[i]);
}
}
void Prune(Node root)
{
TraversePrune(root);
}
Boolean TraversePrune(Node node)// if is leaf, return true
{
Boolean allLeafChild = true;
if(node.childnode.size()==0)
{
return true;
}
for(int i=0;i<node.childnode.size();i++)
{
if(!TraversePrune(node.childnode.get(i)))// if one of the child is not leaf
{
allLeafChild = false;
//return false;
}
}
if(!allLeafChild) return false;
//all child didn't return false, means all leaf node
//whether to prune
if(needPrune(node))
{
PruneNode(node);
//System.out.println("yes");
return true; // become a new leaf node
}
//didn't prune
else
return false;
}
void PruneNode(Node node)
{
//System.out.println("prune node "+node.attributeIndex);
node.getClassLable();
//remove all childnode fails, so just use a new arraylist to replace
node.resetChild();
//System.out.println(node.childnode.size());
}
Boolean needPrune(Node node)
{
Double npParent=0.0,npChild=0.0;
//System.out.println("need prune "+node.attributeIndex);
npParent = node.solveforP(z)*node.getTotalNumber();
//System.out.println(node.childnode.size());
for(int i=0;i<node.childnode.size();i++)
{
Node child = node.childnode.get(i);
npChild += child.solveforP(z)*child.getTotalNumber();
// System.out.println(child.solveforP(z));
}
//System.out.println(npParent+" "+ npChild);
if(npParent <= npChild) return true;
else
return false;
}
int classify(Example e)
{
Node node = root;
//System.out.println("root: "+root.attributeIndex);
int valueIndex;
while(node.childnode.size()>0)
{
valueIndex = e.get(node.attributeIndex).intValue();
//System.out.println(valueIndex+" "+node.childnode.size());
//System.out.println("attribute index "+node.attributeIndex);
node = node.childnode.get(valueIndex);
}
return node.getClassLable();
}
Performance classify(DataSet dataset)
{
int[] result = new int[dataset.examples.size()];
for(int i=0;i<dataset.examples.size();i++)
{
result[i] = classify(dataset.examples.get(i));
}
Performance p= new Performance(dataset, classIndex, result);
// System.out.println(p.getAccuracy());
return p;
}
static void setx(String a)
{
x = Integer.valueOf(a);
}
public void setOptions(String args[])
{
int count = 0;
String temp[] = new String[args.length];
for(int i = 0 ; i< args.length-1; i+=2)
{
if(args[i].compareTo("-t")==0)
{
temp[i] = args[i];
temp[i+1] = args[i+1];
count = i+1;
hasTrainingSet = true;
}
if(args[i].compareTo("-T")==0)
{
temp[i] = args[i];
temp[i+1] = args[i+1];
count = i+1;
hasTestSet = true;
}
if(args[i].compareTo("-x")==0)
{
setx(args[i+1]);
}
}
options = new String[count+1];
for(int i=0;i<count+1;i++)
{
options[i]=temp[i];
// System.out.println(options[i]);
}
}
}