-
Notifications
You must be signed in to change notification settings - Fork 0
/
decision_tree.cpp
392 lines (322 loc) · 9.06 KB
/
decision_tree.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cmath>
#include <map>
using namespace std;
class Table {
public:
vector<string> attrName;
vector<vector<string> > data;
vector<vector<string> > attrValueList;
void extractAttrValue() {
attrValueList.resize(attrName.size());
for(int j=0; j<attrName.size(); j++) {
map<string, int> value;
for(int i=0; i<data.size(); i++) {
value[data[i][j]]=1;
}
for(auto iter=value.begin(); iter != value.end(); iter++) {
attrValueList[j].push_back(iter->first);
}
}
}
};
class Node {
public:
int criteriaAttrIndex;
string attrValue;
int treeIndex;
bool isLeaf;
string label;
vector<int > children;
Node() {
isLeaf = false;
}
};
class DecisionTree {
public:
Table initialTable;
vector<Node> tree;
DecisionTree(Table table) {
initialTable = table;
initialTable.extractAttrValue();
Node root;
root.treeIndex=0;
tree.push_back(root);
run(initialTable, 0);
printTree(0, "");
cout<< "<-- finish generating decision tree -->" << endl << endl;
}
string guess(vector<string> row) {
string label = "";
int leafNode = dfs(row, 0);
if(leafNode == -1) {
return "dfs failed";
}
label = tree[leafNode].label;
return label;
}
int dfs(vector<string>& row, int here) {
if(tree[here].isLeaf) {
return here;
}
int criteriaAttrIndex = tree[here].criteriaAttrIndex;
for(int i=0;i<tree[here].children.size(); i++) {
int next = tree[here].children[i];
if (row[criteriaAttrIndex] == tree[next].attrValue) {
return dfs(row, next);
}
}
return -1;
}
void run(Table table, int nodeIndex) {
if(isLeafNode(table) == true) {
tree[nodeIndex].isLeaf = true;
tree[nodeIndex].label = table.data.back().back();
return;
}
int selectedAttrIndex = getSelectedAttribute(table);
map<string, vector<int> > attrValueMap;
for(int i=0;i<table.data.size();i++) {
attrValueMap[table.data[i][selectedAttrIndex]].push_back(i);
}
tree[nodeIndex].criteriaAttrIndex = selectedAttrIndex;
pair<string, int> majority = getMajorityLabel(table);
if((double)majority.second/table.data.size() > 0.8) {
tree[nodeIndex].isLeaf = true;
tree[nodeIndex].label = majority.first;
return;
}
for(int i=0;i< initialTable.attrValueList[selectedAttrIndex].size(); i++) {
string attrValue = initialTable.attrValueList[selectedAttrIndex][i];
Table nextTable;
vector<int> candi = attrValueMap[attrValue];
for(int i=0;i<candi.size(); i++) {
nextTable.data.push_back(table.data[candi[i]]);
}
Node nextNode;
nextNode.attrValue = attrValue;
nextNode.treeIndex = (int)tree.size();
tree[nodeIndex].children.push_back(nextNode.treeIndex);
tree.push_back(nextNode);
// for empty table
if(nextTable.data.size()==0) {
nextNode.isLeaf = true;
nextNode.label = getMajorityLabel(table).first;
tree[nextNode.treeIndex] = nextNode;
} else {
run(nextTable, nextNode.treeIndex);
}
}
}
double getEstimatedError(double f, int N) {
double z = 0.69;
if(N==0) {
cout << ":: getEstimatedError :: N is zero" << endl;
exit(0);
}
return (f+z*z/(2*N)+z*sqrt(f/N-f*f/N+z*z/(4*N*N)))/(1+z*z/N);
}
pair<string, int> getMajorityLabel(Table table) {
string majorLabel = "";
int majorCount = 0;
map<string, int> labelCount;
for(int i=0;i< table.data.size(); i++) {
labelCount[table.data[i].back()]++;
if(labelCount[table.data[i].back()] > majorCount) {
majorCount = labelCount[table.data[i].back()];
majorLabel = table.data[i].back();
}
}
return {majorLabel, majorCount};
}
bool isLeafNode(Table table) {
for(int i=1;i < table.data.size();i++) {
if(table.data[0].back() != table.data[i].back()) {
return false;
}
}
return true;
}
int getSelectedAttribute(Table table) {
int maxAttrIndex = -1;
double maxAttrValue = 0.0;
// except label
for(int i=0; i< initialTable.attrName.size()-1; i++) {
if(maxAttrValue < getGainRatio(table, i)) {
maxAttrValue = getGainRatio(table, i);
maxAttrIndex = i;
}
}
return maxAttrIndex;
}
double getGainRatio(Table table, int attrIndex) {
return getGain(table, attrIndex)/getSplitInfoAttrD(table, attrIndex);
}
double getInfoD(Table table) {
double ret = 0.0;
int itemCount = (int)table.data.size();
map<string, int> labelCount;
for(int i=0;i<table.data.size();i++) {
labelCount[table.data[i].back()]++;
}
for(auto iter=labelCount.begin(); iter != labelCount.end(); iter++) {
double p = (double)iter->second/itemCount;
ret += -1.0 * p * log(p)/log(2);
}
return ret;
}
double getInfoAttrD(Table table, int attrIndex) {
double ret = 0.0;
int itemCount = (int)table.data.size();
map<string, vector<int> > attrValueMap;
for(int i=0;i<table.data.size();i++) {
attrValueMap[table.data[i][attrIndex]].push_back(i);
}
for(auto iter=attrValueMap.begin(); iter != attrValueMap.end(); iter++) {
Table nextTable;
for(int i=0;i<iter->second.size(); i++) {
nextTable.data.push_back(table.data[iter->second[i]]);
}
int nextItemCount = (int)nextTable.data.size();
ret += (double)nextItemCount/itemCount * getInfoD(nextTable);
}
return ret;
}
double getGain(Table table, int attrIndex) {
return getInfoD(table)-getInfoAttrD(table, attrIndex);
}
double getSplitInfoAttrD(Table table, int attrIndex) {
double ret = 0.0;
int itemCount = (int)table.data.size();
map<string, vector<int> > attrValueMap;
for(int i=0;i<table.data.size();i++) {
attrValueMap[table.data[i][attrIndex]].push_back(i);
}
for(auto iter=attrValueMap.begin(); iter != attrValueMap.end(); iter++) {
Table nextTable;
for(int i=0;i<iter->second.size(); i++) {
nextTable.data.push_back(table.data[iter->second[i]]);
}
int nextItemCount = (int)nextTable.data.size();
double d = (double)nextItemCount/itemCount;
ret += -1.0 * d * log(d) / log(2);
}
return ret;
}
/*
* Enumerates through all the nodes of the tree and prints all the branches
*/
void printTree(int nodeIndex, string branch) {
if (tree[nodeIndex].isLeaf == true)
cout << branch << "Label: " << tree[nodeIndex].label << "\n";
for(int i = 0; i < tree[nodeIndex].children.size(); i++) {
int childIndex = tree[nodeIndex].children[i];
string attributeName = initialTable.attrName[tree[nodeIndex].criteriaAttrIndex];
string attributeValue = tree[childIndex].attrValue;
printTree(childIndex, branch + attributeName + " = " + attributeValue + ", ");
}
}
};
class InputReader {
private:
ifstream fin;
Table table;
public:
InputReader(string filename) {
fin.open(filename);
if(!fin) {
cout << filename << " file could not be opened\n";
exit(0);
}
parse();
}
void parse() {
string str;
bool isAttrName = true;
while(!getline(fin, str).eof()){
vector<string> row;
int pre = 0;
for(int i=0;i<str.size();i++){
if(str[i] == '\t') {
string col = str.substr(pre, i-pre);
row.push_back(col);
pre = i+1;
}
}
string col = str.substr(pre, str.size()-pre-1);
row.push_back(col);
if(isAttrName) {
table.attrName = row;
isAttrName = false;
} else {
table.data.push_back(row);
}
}
}
Table getTable() {
return table;
}
};
class OutputPrinter {
private:
ofstream fout;
public:
OutputPrinter(string filename) {
fout.open(filename);
if(!fout) {
cout << filename << " file could not be opened\n";
exit(0);
}
}
string joinByTab(vector<string> row) {
string ret = "";
for(int i=0; i< row.size(); i++) {
ret += row[i];
if(i != row.size() -1) {
ret += '\t';
}
}
return ret;
}
void addLine(string str) {
fout << str << endl;
}
};
int main(int argc, const char * argv[]) {
if(argc!=4) {
cout << "Please follow this format. dt.exe [train.txt] [test.txt] [result.txt]";
return 0;
}
string trainFileName = argv[1];
InputReader trainInputReader(trainFileName);
DecisionTree decisionTree(trainInputReader.getTable());
string testFileName = argv[2];
InputReader testInputReader(testFileName);
Table test = testInputReader.getTable();
string resultFileName = argv[3];
OutputPrinter outputPrinter(resultFileName);
outputPrinter.addLine(outputPrinter.joinByTab(test.attrName));
for(int i=0;i < test.data.size(); i++) {
vector<string> result = test.data[i];
result.push_back(decisionTree.guess(test.data[i]));
outputPrinter.addLine(outputPrinter.joinByTab(result));
}
/* for answer check */
/*
InputReader answerInputReader("dt_answer1.txt");
Table answer = answerInputReader.getTable();
int totalCount = (int)answer.data.size();
int hitCount = 0;
for(int i=0;i < test.data.size(); i++) {
if(answer.data[i].back() == decisionTree.guess(test.data[i])) {
hitCount++;
}
}
cout << "Accuracy: " << (double)hitCount/totalCount*100 << "%";
cout << "(" << hitCount << "/" << totalCount << ")" << endl;
*/
return 0;
}