-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdt_core.py
More file actions
412 lines (326 loc) · 13.6 KB
/
dt_core.py
File metadata and controls
412 lines (326 loc) · 13.6 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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# version 1.0
import math
from typing import List
from anytree import Node, RenderTree
import dt_global
import dt_provided
import numpy as np # numpy==1.19.2
def find_all_labels(examples, pos, value, feature, label_set, direction):
label_set.add(examples[pos][dt_global.label_index])
next_pos = pos + direction
while (next_pos < len(examples) and next_pos >= 0 and examples[next_pos][feature] == value):
label_set.add(examples[next_pos][dt_global.label_index])
next_pos += direction
return
def diff_sets(set1, set2):
for a in set1:
if a not in set2:
return True
for a in set2:
if a not in set1:
return True
return False
def get_splits(examples: List, feature: str): #-> List[float]:
"""
Given some examples and a feature, returns a list of potential split point values for the feature.
:param examples: a set of examples
:type examples: List[List[Any]]
:param feature: a feature
:type feature: str
:return: a list of potential split point values
:rtype: List[float]
"""
feature_index = dt_global.feature_names.index(feature)
possible_split_points = []
forward = 1
backward = -1
if (len(examples) < 2):
return possible_split_points
examples.sort(key=lambda x: x[feature_index])
for i in range(len(examples) - 1):
curr_value = examples[i][feature_index]
next_value = examples[i + 1][feature_index]
if (curr_value != next_value):
curr_set = set()
next_set = set()
find_all_labels(examples, i, curr_value, feature_index, curr_set, backward)
find_all_labels(examples, i + 1, next_value, feature_index, next_set, forward)
if diff_sets(curr_set, next_set):
possible_split_points.append((curr_value + next_value) / 2)
return possible_split_points
def simple_entropy(d, lenth):
ret = 0.0
for i in d.keys():
proportion = d[i] / lenth
if (proportion != 0):
ret += proportion * math.log2(proportion)
return -ret
def calculate_entropy(start, end, examples, class_dict):
for i in range(start, end):
class_dict[examples[i][dt_global.label_index]] += 1
lenth = end - start
ret = 0.0
for i in class_dict.keys():
proportion = class_dict[i] / lenth
if (proportion != 0.0):
ret += proportion * math.log2(proportion)
return -ret
def choose_best_split(examples:List, feature: str, ret:List, class_dict) -> (str, float, float):
# print(feature)
feature_index = dt_global.feature_names.index(feature)
lenth = len(examples)
list_of_splits = get_splits(examples, feature)
# print(examples)
los = len(list_of_splits)
entropy_before = calculate_entropy(0, lenth, examples, class_dict)
# print("class_dict: ", class_dict, "lenth: ", lenth)
current_split = 0
# count the numbers here good
left_dict = {}
for k in class_dict.keys():
left_dict[k] = 0
for i in range(lenth - 1):
if current_split == los:
break
left_dict[examples[i][dt_global.label_index]] += 1
if (dt_provided.less_than_or_equal_to(examples[i][feature_index], list_of_splits[current_split]) and \
dt_provided.less_than_or_equal_to(list_of_splits[current_split], examples[i + 1][feature_index])):
# print("i: ", i, "lenth:", lenth)
# print("left_dict: ", left_dict, "lenth: ", i + 1)
left_entropy = simple_entropy(left_dict, i + 1)
right_dict = {}
for k in class_dict.keys():
right_dict[k] = class_dict[k] - left_dict[k]
# print("i: ", i, "lenth:", lenth)
# print("right_dict: ", right_dict, "lenth: ", lenth - i - 1)
entropy_after = (i + 1) / lenth * left_entropy + \
(lenth - i - 1) / lenth * simple_entropy(right_dict, lenth - i - 1)
info_gain = entropy_before - entropy_after
if (dt_provided.less_than(ret[2], info_gain)):
ret[0] = feature
ret[1] = list_of_splits[current_split]
ret[2] = info_gain
current_split += 1
def choose_feature_split(examples: List, features: List[str]) -> (str, float, float):
"""
Given some examples and some features,
returns a feature and a split point value with the max expected information gain.
If there are no valid split points for the remaining features, return None, -1, and -inf.
Tie breaking rules:
(1) With multiple split points, choose the one with the smallest value.
(2) With multiple features with the same info gain, choose the first feature in the list.
:param examples: a set of examples
:type examples: List[List[Any]]
:param features: a set of features
:type features: List[str]
:return: the best feature, the best split value, the max expected information gain
:rtype: str, float, float
"""
ret_vals = [None, -1, -math.inf]
class_set = set(np.array(examples)[:, -1])
class_dict = {}
for i in range(len(features)):
for c in class_set:
class_dict[c] = 0.0
choose_best_split(examples, features[i], ret_vals, class_dict)
return ret_vals[0], ret_vals[1], ret_vals[2]
def split_examples(examples: List, feature: str, split: float) -> (List, List):
"""
Given some examples, a feature, and a split point,
splits examples into two lists and return the two lists of examples.
The first list of examples have their feature value <= split point.
The second list of examples have their feature value > split point.
:param examples: a set of examples
:type examples: List[List[Any]]
:param feature: a feature
:type feature: str
:param split: the split point
:type split: float
:return: two lists of examples split by the feature split
:rtype: List[List[Any]], List[List[Any]]
"""
feature_index = dt_global.feature_names.index(feature)
first_list = []
second_list = []
for i in range(len(examples)):
if dt_provided.less_than_or_equal_to(examples[i][feature_index], split):
first_list.append(examples[i])
else:
second_list.append(examples[i])
return first_list, second_list
def dict_init(examples, dict):
for i in range(len(examples)):
if examples[i][dt_global.label_index] in dict.keys():
dict[examples[i][dt_global.label_index]] += 1
else:
dict[examples[i][dt_global.label_index]] = 1
def select_majority(dict):
major_class = -1
major_class_count = 0
for v in dict.keys():
if dt_provided.less_than(major_class_count, dict[v]) or \
(dt_provided.less_than_or_equal_to(major_class_count, dict[v]) and \
dt_provided.less_than(v, major_class)):
major_class = v
major_class_count = dict[v]
return major_class
def split_node(cur_node: Node, examples: List, features: List[str], max_depth=math.inf):
"""
Given a tree with cur_node as the root, some examples, some features, and the max depth,
grows a tree to classify the examples using the features by using binary splits.
If cur_node is at max_depth, makes cur_node a leaf node with majority decision and return.
This function is recursive.
:param cur_node: current node
:type cur_node: Node
:param examples: a set of examples
:type examples: List[List[Any]]
:param features: a set of features
:type features: List[str]
:param max_depth: the maximum depth of the tree
:type max_depth: int
"""
dict = {}
dict_init(examples, dict)
cur_node.majority = select_majority(dict)
if (cur_node.depth >= max_depth):
cur_node.decision = cur_node.majority
return
best_feature, best_split_value, max_info_gain = choose_feature_split(examples, features)
if (best_feature == None):
cur_node.decision = cur_node.majority
return
cur_node.feature = best_feature
cur_node.split = best_split_value
cur_node.max_info_gain = max_info_gain
left_child = Node(name=cur_node.name + "0", parent=cur_node, depth=cur_node.depth + 1)
right_child = Node(name=cur_node.name + "1", parent=cur_node, depth=cur_node.depth + 1)
# print(RenderTree(cur_node))
left_examples, right_examples = split_examples(examples, best_feature, best_split_value)
split_node(left_child, left_examples, features, max_depth)
split_node(right_child, right_examples, features, max_depth)
def learn_dt(examples: List, features: List[str], max_depth=math.inf) -> Node:
"""
Given some examples, some features, and the max depth,
creates the root of a decision tree, and
calls split_node to grow the tree to classify the examples using the features, and
returns the root node.
This function is a wrapper for split_node.
Tie breaking rule:
If there is a tie for majority voting, always return the label with the smallest value.
:param examples: a set of examples
:type examples: List[List[Any]]
:param features: a set of features
:type features: List[str]
:param max_depth: the max depth of the tree
:type max_depth: int, default math.inf
:return: the root of the tree
:rtype: Node
"""
root = Node(name="root", depth=0)
split_node(root, examples, features, max_depth)
# print(RenderTree(root))
return root
def example_is_less(cur_node, example):
test_val = example[dt_global.feature_names.index(cur_node.feature)]
return dt_provided.less_than_or_equal_to(test_val, cur_node.split)
def predict(cur_node: Node, example, max_depth=math.inf) -> int:
"""
Given a tree with cur_node as its root, an example, and optionally a max depth,
returns a prediction for the example based on the tree.
If max_depth is provided and we haven't reached a leaf node at the max depth,
return the majority decision at this node.
This function is recursive.
Tie breaking rule:
If there is a tie for majority voting, always return the label with the smallest value.
:param cur_node: cur_node of a decision tree
:type cur_node: Node
:param example: one example
:type example: List[Any]
:param max_depth: the max depth
:type max_depth: int, default math.inf
:return: the decision for the given example
:rtype: int
"""
if cur_node.is_leaf:
return cur_node.decision
elif max_depth == cur_node.depth:
return cur_node.majority
elif example_is_less(cur_node, example):
return predict(cur_node.children[0], example, max_depth)
return predict(cur_node.children[1], example, max_depth)
def get_prediction_accuracy(cur_node: Node, examples: List, max_depth=math.inf) -> float:
"""
Given a tree with cur_node as the root, some examples,
and optionally the max depth,
returns the accuracy by predicting the examples using the tree.
The tree may be pruned by max_depth.
:param cur_node: cur_node of the decision tree
:type cur_node: Node
:param examples: the set of examples.
:type examples: List[List[Any]]
:param max_depth: the max depth
:type max_depth: int, default math.inf
:return: the prediction accuracy for the examples based on the cur_node
:rtype: float
"""
noe = len(examples)
if noe == 0:
return 0.0;
nor = 0;
for i in range(noe):
if predict(cur_node, examples[i], max_depth) == examples[i][dt_global.label_index]:
nor += 1
return nor / noe
def post_prune_helper(cur_node, min_info_gain, loc):
if cur_node.is_leaf:
return
if cur_node.children[0].is_leaf and cur_node.children[1].is_leaf and \
dt_provided.less_than(cur_node.max_info_gain, min_info_gain):
cur_node.decision = cur_node.majority
cur_node.children = []
post_prune_helper(cur_node.parent, min_info_gain, loc)
loc[0] = False
return
else:
post_prune_helper(cur_node.children[0], min_info_gain, loc)
post_prune_helper(cur_node.children[1], min_info_gain, loc)
def post_prune(cur_node: Node, min_info_gain: float):
"""
Given a tree with cur_node as the root, and the minimum information gain,
post prunes the tree using the minimum information gain criterion.
This function is recursive.
Let leaf parents denote all the nodes that only have leaf nodes as its descendants.
Go through all the leaf parents.
If the information gain at a leaf parent is smaller than the pre-defined value,
convert the leaf parent into a leaf node.
Repeat until the information gain at every leaf parent is greater than
or equal to the pre-defined value of the minimum information gain.
:param cur_node: the current node
:type cur_node: Node
:param min_info_gain: the minimum information gain
:type min_info_gain: float
"""
if cur_node.is_leaf:
return
if cur_node.children[0].is_leaf and cur_node.children[1].is_leaf and \
dt_provided.less_than(cur_node.max_info_gain, min_info_gain):
cur_node.decision = cur_node.majority
cur_node.children = []
if cur_node.is_root == False:
post_prune(cur_node.parent, min_info_gain)
return
else:
post_prune(cur_node.children[0], min_info_gain)
if len(cur_node.children) != 0:
post_prune(cur_node.children[1], min_info_gain)
def pre_prune(cur_node, depth):
if cur_node.is_leaf:
return
elif cur_node.depth >= depth:
cur_node.decision = cur_node.majority
cur_node.children = []
return
else:
pre_prune(cur_node.children[0], depth)
pre_prune(cur_node.children[1], depth)
return