-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecisionTree.py
267 lines (197 loc) · 6.46 KB
/
decisionTree.py
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
import sys
import os
import csv
import math
def train_and_test(train_input, test_input, depth, train_out, test_out, metrics):
data = handle_data(train_input)
train_label = data[0]
train_feats = data[1]
train_tags = data[2]
tree = decisionTreeTrain(train_label, train_feats, train_tags,
0, int(depth))
# printTree(tree,0)
train_erro = test(tree, train_input, train_out)
test_erro = test(tree, test_input, test_out)
str = "error(train): {}\nerror(test): {}".format(train_erro, test_erro)
f = open(metrics, 'w')
f.write(str)
f.close()
##
## Helper functions
# handle_data: input an .csv file, return its labels, features and tags
def handle_data(train_input):
labels = []
feats = []
features = {}
with open(train_input, 'r') as csvfile:
reader = csv.reader(csvfile)
print(reader)
for row in reader:
line = len(row)
if (len(feats) == 0):
for i in range(line - 1):
feats.append([row[i]])
else:
for i in range(line - 1):
feats[i].append(row[i])
labels.append(row[-1])
for i in range(line - 1):
temp = feats[i].pop(0)
features[temp] = feats[i]
csvfile.close()
tags = list(set(labels))
return [labels, features, tags]
# decisionTreeTrain: implementation of ID3
# input labels, features, tags, max_depths, return decision tree
def decisionTreeTrain(labels, features, tags, cur_depth, max_depth):
tag0_num, tag1_num = count_num(labels, tags)
if (tag0_num > tag1_num):
guess = tags[0]
guess_num = tag0_num
else:
guess = tags[1]
guess_num = tag1_num
# base case: no need to split further
if (guess_num == len(labels)):
return Tree(guess)
# base case: cannot split further
elif (len(features) == 0):
return Tree(guess)
# do not split over max_depth
elif (cur_depth > max_depth):
return Tree(guess)
else:
n_labels = []
y_labels = []
score = -1
feature = []
for i in features:
# the accuracy we would get if we only queried on i
cur_score, cur_n_labels, cur_y_labels = info_gain(features[i], labels)
if (cur_score >= score):
score = cur_score
n_labels = cur_n_labels
y_labels = cur_y_labels
feature = i
cur_depth += 1
n_features, y_features = split_features(feature, features)
left = decisionTreeTrain(n_labels, n_features, tags, cur_depth, max_depth)
right = decisionTreeTrain(y_labels, y_features, tags, cur_depth, max_depth)
return Tree(tags[0], left, right, feature)
# test the result of the tree, return the error rate
def test(tree, test_input, output):
feats = []
data = []
count = 0
total = 0
with open(test_input, 'r') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
line = len(row)
if (total == 0):
for i in range(line - 1):
feats.append(row[i])
else:
d = {}
for i in range(line - 1):
d[feats[i]] = row[i]
label = decisionTreeTest(tree, d)
data.append(label + '\n')
if (label != row[-1]):
count += 1
total += 1
str = "".join(data)
f = open(output, 'w')
f.write(str)
csvfile.close()
f.close()
return float(count) / (total - 1)
# decisionTreeTest takes in the decision tree and a dict of features
# return the predicted label
def decisionTreeTest(tree, d):
if (tree.isLeaf()):
return tree.tag
else:
if (d[tree.feature] == 'n'):
return decisionTreeTest(tree.left, d)
else:
return decisionTreeTest(tree.right, d)
# printTree: print out the decision tree
def printTree(tree, depth):
if (tree.isLeaf()):
print(' ' * depth, tree.tag)
else:
print(' ' * depth, tree.tag, ': ', tree.feature)
printTree(tree.left, depth + 1)
printTree(tree.right, depth + 1)
# calculate information gain for the specific feature
# Gain(T, X) = Entropy(T) - Entropy(T, X)
# Entropy(T, X) = Sum(c)(P(c)E(c))
def info_gain(feature, labels):
n_labels = []
y_labels = []
tags = list(set(labels))
for i in range(len(feature)):
if (feature[i] == 'n'):
n_labels.append(labels[i])
else:
y_labels.append(labels[i])
p0 = float(len(n_labels)) / len(labels)
p1 = float(len(y_labels)) / len(labels)
return get_entropy(labels, tags) - (p0 * get_entropy(n_labels, tags) +
p1 * get_entropy(y_labels, tags)), n_labels, y_labels
# get entropy of labels
#
def get_entropy(labels, tags):
tag0_num, tag1_num = count_num(labels, tags)
if (tag0_num == 0 or tag1_num == 0):
return 0
p0 = float(tag0_num) / len(labels)
p1 = float(tag1_num) / len(labels)
return -1 * (p0 * math.log(p0, 2) + p1 * math.log(p1, 2))
def split_features(feature, features):
n_features = {}
y_features = {}
len_list = len(features[feature])
for i in features:
if (i == feature):
continue
n_features[i] = []
y_features[i] = []
for j in range(len_list):
if (features[feature][j] == "n"):
n_features[i].append(features[i][j])
else:
y_features[i].append(features[i][j])
return n_features, y_features
def count_num(labels, tags):
tag0_num = 0
tag1_num = 0
for i in labels:
if (i == tags[0]):
tag0_num += 1
else:
tag1_num += 1
return tag0_num, tag1_num
# Tree Class
class Tree(object):
def __init__(self, tag, left=None, right=None, feature=None):
self.tag = tag
self.left = left
self.right = right
self.feature = feature
def isLeaf(self):
if (self.left == None and self.right == None):
return True
else:
return False
##
# Main function
if __name__ == '__main__':
train_input = sys.argv[1]
test_input = sys.argv[2]
depth = sys.argv[3]
train_out = sys.argv[4]
test_out = sys.argv[5]
metrics = sys.argv[6]
train_and_test(train_input, test_input, depth, train_out, test_out, metrics)