-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathd_tree.py
More file actions
282 lines (206 loc) · 5.96 KB
/
d_tree.py
File metadata and controls
282 lines (206 loc) · 5.96 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
import math
def d_tree(examples, features, parent_examples, depth=20):
"""
Builds the decision tree, selecting features
based on information gain.
:param examples: training examples
:param features: set of features
:param parent_examples: parent's example set
:param depth: maximum depth
:return: The root node of a decision tree
"""
if not examples:
return DNode(plurality_value(parent_examples), is_leaf=True)
if same_goal(examples):
return DNode(examples[0].goal, is_leaf=True)
if not features:
return DNode(plurality_value(examples), is_leaf=True)
feature, kids = max_gain(examples, features)
root = DNode(feature)
if depth < 1:
depth = 1
for value in kids:
exs = kids[value]
if depth == 1:
subtree = DNode(plurality_value(exs), is_leaf=True)
root.add(value, subtree)
else:
subtree = d_tree(exs, features.difference({feature}), examples, depth - 1)
root.add(value, subtree)
return root
class DNode:
"""
This class represents a single node in
a decision tree.
"""
def __init__(self, value, is_leaf=False):
"""
Initialize the node.
:param value: the node's value
:param is_leaf: is this a leaf node?
"""
self.is_leaf = is_leaf
self.value = value
self.children = {}
self.weight = None
def add(self, label, d_node):
"""
Adds a child to the node.
:param label: the branch label
:param d_node: the new node
"""
self.children[label] = d_node
def print(self):
"""
Prints a representation of the node
and its children.
"""
out = str(self.value) + " -> "
for key in self.children:
out += str(key) + " : " + str(self.children[key].value) + " | "
print(out)
for key in self.children:
self.children[key].print()
def decide(self, instance):
"""
Classify an instance of data.
:param instance: instance of data
:return: classification
"""
node = self
while node:
if node.is_leaf:
return node.value
branch = instance.features[node.value]
if branch in node.children:
node = node.children[branch]
else:
return vote(node)
return None
def vote(node):
"""
Get the majority classification from a
node's children
:param node: the node to be voted on
:return: majority classification
"""
if node.is_leaf:
return node.value
if not node.children:
return None
count = {}
max_count = -1
max_val = None
for branch in node.children:
val = vote(node.children[branch])
if not val:
continue
count[val] = count[val] + 1 if val in count else 1
if count[val] > max_count:
max_count = count[val]
max_val = val
return max_val
def count_goals(examples):
"""
Counts the number of examples for
each classification. Takes weights into
consideration.
:param examples: list of examples
:return: count of every classification
"""
count = {}
for ex in examples:
weight = ex.weight if ex.weight else 1
if ex.goal in count:
count[ex.goal] += weight
else:
count[ex.goal] = weight
return count
def plurality_value(examples):
"""
Gets the majority classification from a
list of examples.
:param examples: list of examples.
:return: majority classification
"""
value = None
max_weight = -1
count = count_goals(examples)
for ex in examples:
if count[ex.goal] > max_weight:
max_weight = count[ex.goal]
value = ex.goal
return value
def same_goal(examples):
"""
Checks if every example in the list
has the same classification.
:param examples: list of examples
:return: True or False
"""
goal = examples[0].goal
for i in range(1, len(examples)):
if examples[i] != goal:
return False
return True
def entropy(examples):
"""
:param examples: list of examples
:return: entropy of the list
"""
count = count_goals(examples)
total = 0
for key in count.keys():
p = count[key]/len(examples)
total += -p * math.log(p, 2)
return total
def max_gain(examples, features):
"""
Finds a split of the example list which
leads to the most information gain.
:param examples: list of examples
:param features: set of features
:return: feature and split with max gain.
"""
entrpy = entropy(examples)
max_val = -1
max_feature = None
children = None
for feature in features:
gains, kids = gain(examples, feature, entrpy)
if gains > max_val:
max_val = gains
max_feature = feature
children = kids
return max_feature, children
def gain(examples, feature, entrpy):
"""
Calculates the information gain after
splitting examples on a feature.
:param examples: list of examples
:param feature: feature to split on
:param entrpy: current entropy
:return: information gain
"""
kids = split(examples, feature)
total = 0
for kid in kids:
exs = kids[kid]
total += (len(exs)/len(examples)) * entropy(exs)
gains = entrpy - total
return gains, kids
def split(examples, feature):
"""
Splits a list of examples on a feature.
:param examples: list of examples
:param feature: feature to split on.
:return: table representing the split.
"""
result = {}
for ex in examples:
value = ex.features[feature]
if value in result:
result[value].append(ex)
else:
result[value] = [ex]
return result