forked from m4jidRafiei/Decision-Tree-Python-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecisionTree.py
More file actions
251 lines (228 loc) · 9.66 KB
/
DecisionTree.py
File metadata and controls
251 lines (228 loc) · 9.66 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
import math
from collections import deque
from graphviz import Digraph
class Node(object):
def __init__(self):
self.value = None
self.next = None
self.childs = None
# Simple class of Decision Tree
# Aimed for who want to learn Decision Tree, so it is not optimized
class DecisionTree(object):
def __init__(self, sample, attributes, labels, criterion):
self.sample = sample
self.attributes = attributes
self.labels = labels
self.labelCodes = None
self.labelCodesCount = None
self.initLabelCodes()
self.criterion = criterion
# print(self.labelCodes)
self.gini = None
self.entropy = None
self.root = None
if(self.criterion == "gini"):
self.gini = self.getGini([x for x in range(len(self.labels))])
else:
self.entropy = self.getEntropy([x for x in range(len(self.labels))])
def initLabelCodes(self):
self.labelCodes = []
self.labelCodesCount = []
for l in self.labels:
if l not in self.labelCodes:
self.labelCodes.append(l)
self.labelCodesCount.append(0)
self.labelCodesCount[self.labelCodes.index(l)] += 1
def getLabelCodeId(self, sampleId):
return self.labelCodes.index(self.labels[sampleId])
def getAttributeValues(self, sampleIds, attributeId):
vals = []
for sid in sampleIds:
val = self.sample[sid][attributeId]
if val not in vals:
vals.append(val)
# print(vals)
return vals
def getEntropy(self, sampleIds):
entropy = 0
labelCount = [0] * len(self.labelCodes)
for sid in sampleIds:
labelCount[self.getLabelCodeId(sid)] += 1
# print("-ge", labelCount)
for lv in labelCount:
# print(lv)
if lv != 0:
entropy += -lv/len(sampleIds) * math.log(lv/len(sampleIds), 2)
else:
entropy += 0
return entropy
def getGini(self, sampleIds):
gini = 0
labelCount = [0] * len(self.labelCodes)
for sid in sampleIds:
labelCount[self.getLabelCodeId(sid)] += 1
# print("-ge", labelCount)
for lv in labelCount:
# print(lv)
if lv != 0:
gini += (lv/len(sampleIds)) ** 2
else:
gini += 0
return 1 - gini
def getDominantLabel(self, sampleIds):
labelCodesCount = [0] * len(self.labelCodes)
for sid in sampleIds:
labelCodesCount[self.labelCodes.index(self.labels[sid])] += 1
return self.labelCodes[labelCodesCount.index(max(labelCodesCount))]
def getInformationGain(self, sampleIds, attributeId):
gain = self.getEntropy(sampleIds)
attributeVals = []
attributeValsCount = []
attributeValsIds = []
for sid in sampleIds:
val = self.sample[sid][attributeId]
if val not in attributeVals:
attributeVals.append(val)
attributeValsCount.append(0)
attributeValsIds.append([])
vid = attributeVals.index(val)
attributeValsCount[vid] += 1
attributeValsIds[vid].append(sid)
# print("-gig", self.attributes[attributeId])
for vc, vids in zip(attributeValsCount, attributeValsIds):
# print("-gig", vids)
gain -= (vc/len(sampleIds)) * self.getEntropy(vids)
return gain
def getInformationGainGini(self, sampleIds, attributeId):
gain = self.getGini(sampleIds)
attributeVals = []
attributeValsCount = []
attributeValsIds = []
for sid in sampleIds:
val = self.sample[sid][attributeId]
if val not in attributeVals:
attributeVals.append(val)
attributeValsCount.append(0)
attributeValsIds.append([])
vid = attributeVals.index(val)
attributeValsCount[vid] += 1
attributeValsIds[vid].append(sid)
# print("-gig", self.attributes[attributeId])
for vc, vids in zip(attributeValsCount, attributeValsIds):
# print("-gig", vids)
gain -= (vc/len(sampleIds)) * self.getGini(vids)
return gain
def getAttributeMaxInformationGain(self, sampleIds, attributeIds):
attributesEntropy = [0] * len(attributeIds)
for i, attId in zip(range(len(attributeIds)), attributeIds):
attributesEntropy[i] = self.getInformationGain(sampleIds, attId)
maxId = attributeIds[attributesEntropy.index(max(attributesEntropy))]
try:
maxvalue = attributesEntropy[maxId]
except:
maxvalue = 0
return self.attributes[maxId], maxId, maxvalue
def getAttributeMaxInformationGainGini(self, sampleIds, attributeIds):
attributesEntropy = [0] * len(attributeIds)
for i, attId in zip(range(len(attributeIds)), attributeIds):
attributesEntropy[i] = self.getInformationGainGini(sampleIds, attId)
maxId = attributeIds[attributesEntropy.index(max(attributesEntropy))]
try:
maxvalue = attributesEntropy[maxId]
except:
maxvalue = 0
return self.attributes[maxId], maxId, maxvalue
def isSingleLabeled(self, sampleIds):
label = self.labels[sampleIds[0]]
for sid in sampleIds:
if self.labels[sid] != label:
return False
return True
def getLabel(self, sampleId):
return self.labels[sampleId]
def id3(self,gain_threshold, minimum_samples):
sampleIds = [x for x in range(len(self.sample))]
attributeIds = [x for x in range(len(self.attributes))]
self.root = self.id3Recv(sampleIds, attributeIds, self.root, gain_threshold, minimum_samples)
def id3Recv(self, sampleIds, attributeIds, root, gain_threshold, minimum_samples):
root = Node() # Initialize current root
if self.isSingleLabeled(sampleIds):
root.value = self.labels[sampleIds[0]]
return root
# print(attributeIds)
if len(attributeIds) == 0:
root.value = self.getDominantLabel(sampleIds)
return root
if(self.criterion == "gini"):
bestAttrName, bestAttrId, bestValue = self.getAttributeMaxInformationGainGini(sampleIds, attributeIds)
else:
bestAttrName, bestAttrId, bestValue = self.getAttributeMaxInformationGain(sampleIds, attributeIds)
# print(bestAttrName)
#if(bestValue > 0):
#print("Best gain -> " + bestAttrName + "::" + str(bestValue) + "\n" )
root.value = bestAttrName
root.childs = [] # Create list of children
if(bestValue < gain_threshold):
Dominantlabel = self.getDominantLabel(sampleIds)
root.value = Dominantlabel
return root
if(len(sampleIds) < minimum_samples):
Dominantlabel = self.getDominantLabel(sampleIds)
root.value = Dominantlabel
return root
for value in self.getAttributeValues(sampleIds, bestAttrId):
# print(value)
child = Node()
child.value = value
root.childs.append(child) # Append new child node to current root
childSampleIds = []
for sid in sampleIds:
if self.sample[sid][bestAttrId] == value:
childSampleIds.append(sid)
if len(childSampleIds) == 0:
child.next = self.getDominantLabel(sampleIds)
else:
# print(bestAttrName, bestAttrId)
# print(attributeIds)
if len(attributeIds) > 0 and bestAttrId in attributeIds:
toRemove = attributeIds.index(bestAttrId)
attributeIds.pop(toRemove)
child.next = self.id3Recv(childSampleIds, attributeIds, child.next, gain_threshold, minimum_samples)
return root
def print_visualTree(self):
dot = Digraph(comment='Decision Tree')
if self.root:
roots = deque()
roots.append(self.root)
counter = 0
while len(roots) > 0:
root = roots.popleft()
# print(root.value)
dot.node(root.value, root.value)
if root.childs:
for child in root.childs:
counter += 1
# print('({})'.format(child.value))
dot.node(child.value, child.value)
dot.edge(root.value,child.value)
if(child.next.childs):
dot.node(child.next.value, child.next.value)
dot.edge(child.value,child.next.value)
roots.append(child.next)
else:
nodeName = ""
try:
nodeName = child.next.value+str(counter)
except:
nodeName = ""+str(counter)
dot.node(nodeName, child.next.value)
dot.edge(child.value,nodeName)
elif root.next:
dot.node(root.next, root.next)
dot.edge(root.value,root.next)
# print(root.next)
# print(dot.source)
try:
dot.render('output/visualTree.gv', view=True)
except:
print("Please close the resulted pdf file")