-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDtree_with_pruning.py
285 lines (247 loc) · 11 KB
/
Dtree_with_pruning.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import warnings
import pandas as pd
#import numpy as np
import math
import random
import copy
import sys
warnings.simplefilter("ignore")
if(len(sys.argv) != 5):
sys.exit("Please give the required amount of arguments - <trainPath> <testPath> <validationPath> <PruneFactor>")
else:
trainPath = sys.argv[1]
testPath = sys.argv[2]
validationPath = sys.argv[3]
pruneFactor = float(sys.argv[4])
df = pd.read_csv(trainPath)
dtest = pd.read_csv(testPath)
dvalidation = pd.read_csv(validationPath)
# remove empty rows
df = df.dropna()
dtest = dtest.dropna()
dvalidation = dvalidation.dropna()
nodeCount = 0 # for node id
print("Please wait to complete!")
def entropyCalculator(labels):
total = labels.shape[0]
ones = labels.sum().sum()
zeros = total - ones
if total == ones or total == zeros:
return 0
entropy = -(ones/total)*math.log(ones/total, 2) - (zeros/total)*math.log(zeros/total,2)
# print ( "ones : " + str(ones) + "zeros : " + str(zeros) + "entropy : " + str(entropy))
return entropy
# print(entropyCalculator(df[['Class']]))
def informationGain(featurelabels):
total = featurelabels.shape[0]
ones = featurelabels[featurelabels[featurelabels.columns[0]] == 1].shape[0]
zeros = featurelabels[featurelabels[featurelabels.columns[0]] == 0].shape[0]
parentEntropy = entropyCalculator(featurelabels[['Class']])
entropyChildWithOne = entropyCalculator(featurelabels[featurelabels[featurelabels.columns[0]] == 1][['Class']])
entropyChildWithZero = entropyCalculator(featurelabels[featurelabels[featurelabels.columns[0]] == 0][['Class']])
# print ("left entropy : " + str(entropyChildWithZero))
# print ("right entropy : " + str(entropyChildWithOne))
infoGain = parentEntropy - (ones/total)*entropyChildWithOne - (zeros/total)*entropyChildWithZero
return infoGain
# informationGain(df[['XB', 'Class']])
def findBestAttribute(data):
maxInfoGain = -1.0
for x in data.columns:
if x == 'Class':
continue
currentInfoGain = informationGain(data[[x, 'Class']])
# print(str(currentInfoGain) + " " + x)
if maxInfoGain < currentInfoGain:
maxInfoGain = currentInfoGain
bestAttribute = x
return bestAttribute
# findBestAttribute(df)
class Node():
def __init__(self):
self.left = None
self.right = None
self.attribute = None
self.nodeType = None # L/R/I leaf/Root/Intermidiate
self.value = None # attributes split's value 0 or 1
self.positiveCount = None
self.negativeCount = None
self.label = None
self.nodeId = None
def setNodeValue(self, attribute, nodeType, value = None, positiveCount = None, negativeCount = None):
self.attribute = attribute
self.nodeType = nodeType
self.value = value
self.positiveCount = positiveCount
self.negativeCount = negativeCount
class Tree():
def __init__(self):
self.root = Node()
self.root.setNodeValue('$@$', 'R')
def createDecisionTree(self, data, tree):
global nodeCount
total = data.shape[0]
ones = data['Class'].sum()
zeros = total - ones
if data.shape[1] == 1 or total == ones or total == zeros:
tree.nodeType = 'L'
if zeros >= ones:
tree.label = 0
else:
tree.label = 1
return
else:
bestAttribute = findBestAttribute(data)
tree.left = Node()
tree.right = Node()
tree.left.nodeId = nodeCount
nodeCount=nodeCount+1
tree.right.nodeId = nodeCount
nodeCount=nodeCount+1
tree.left.setNodeValue(bestAttribute, 'I', 0, data[(data[bestAttribute]==0) & (df['Class']==1) ].shape[0], data[(data[bestAttribute]==0) & (df['Class']==0) ].shape[0])
tree.right.setNodeValue(bestAttribute, 'I', 1, data[(data[bestAttribute]==1) & (df['Class']==1) ].shape[0], data[(data[bestAttribute]==1) & (df['Class']==0) ].shape[0])
self.createDecisionTree( data[data[bestAttribute]==0].drop([bestAttribute], axis=1), tree.left)
self.createDecisionTree( data[data[bestAttribute]==1].drop([bestAttribute], axis=1), tree.right)
def printTreeLevels(self, node,level):
if(node.left is None and node.right is not None):
for i in range(0,level):
print("| ",end="")
level = level + 1
print("{} = {} (ID:{}) : {}".format(node.attribute, node.value,(node.nodeId if node.nodeId is not None else ""),(node.label if node.label is not None else "")))
self.printTreeLevels(node.right,level)
elif(node.right is None and node.left is not None):
for i in range(0,level):
print("| ",end="")
level = level + 1
# print("{} = {} : {}".format(node.attribute, node.value,(node.label if node.label is not None else "")))
print("{} = {} (ID:{}) : {}".format(node.attribute, node.value,(node.nodeId if node.nodeId is not None else ""),(node.label if node.label is not None else "")))
self.printTreeLevels(node.left,level)
elif(node.right is None and node.left is None):
for i in range(0,level):
print("| ",end="")
level = level + 1
print("{} = {} (ID:{}) : {}".format(node.attribute, node.value,(node.nodeId if node.nodeId is not None else ""),(node.label if node.label is not None else "")))
else:
for i in range(0,level):
print("| ",end="")
level = level + 1
print("{} = {} (ID:{}) : {}".format(node.attribute, node.value,(node.nodeId if node.nodeId is not None else ""),(node.label if node.label is not None else "")))
self.printTreeLevels(node.left,level)
self.printTreeLevels(node.right,level)
def printTree(self, node):
self.printTreeLevels(node.left,0)
self.printTreeLevels(node.right,0)
def predictLabel(self, data, root):
if root.label is not None:
return root.label
elif data[root.left.attribute][data.index.tolist()[0]] == 1:
return self.predictLabel(data, root.right)
else:
return self.predictLabel(data, root.left)
def countNodes(self,node):
if(node.left is not None and node.right is not None):
return 2 + self.countNodes(node.left) + self.countNodes(node.right)
return 0
def countLeaf(self,node):
if(node.left is None and node.right is None):
return 1
return self.countLeaf(node.left) + self.countLeaf(node.right)
def searchNode(tree, x):
tmp = None
res = None
if(tree.nodeType != "L"):
if(tree.nodeId == x):
return tree
else:
res = searchNode(tree.left,x)
if (res is None):
res = searchNode(tree.right,x)
return res
else:
return tmp
def postPruning(pNum,newTree):
for i in range(pNum):
x = random.randint(2,pruneTree.countNodes(pruneTree.root)-1)
tempNode = Node()
tempNode = searchNode(newTree,x)
if(tempNode is not None):
tempNode.left = None
tempNode.right = None
tempNode.nodeType = "L"
if(tempNode.negativeCount >= tempNode.positiveCount):
tempNode.label = 0
else:
tempNode.label = 1
def calculateAccuracy(data, tree):
correctCount = 0
for i in data.index:
val = tree.predictLabel(data.iloc[i:i+1, :].drop(['Class'], axis=1),tree.root)
if val == data['Class'][i]:
correctCount = correctCount + 1
return correctCount/data.shape[0]*100
dtree = Tree()
dtree.createDecisionTree(df, dtree.root)
maxAccuracy = calculateAccuracy(dvalidation, dtree)
bestTree = copy.deepcopy(dtree)
countOfNodes = bestTree.countNodes(bestTree.root)
c = 0
while c < 10:
c += 1
pruneNum = round(countOfNodes*pruneFactor)
# print(pruneNum)
pruneTree = Tree()
pruneTree = copy.deepcopy(bestTree)
postPruning(pruneNum,pruneTree.root)
temp = calculateAccuracy(dvalidation, pruneTree)
if temp > maxAccuracy:
# print("Accuracy Improved")
maxAccuracy = temp
bestTree = copy.deepcopy(pruneTree)
countOfNodes = bestTree.countNodes(bestTree.root)
print("-------------------------------------")
print("Pre-Pruned Tree")
print("-------------------------------------")
dtree.printTree(dtree.root)
print("")
print("-------------------------------------")
print("Pre-Pruned Accuracy")
print("-------------------------------------")
print("Number of training instances = " +str(df.shape[0]))
print("Number of training attributes = " +str(df.shape[1] -1))
print("Total number of nodes in the tree = "+ str(dtree.countNodes(dtree.root)))
print("Number of leaf nodes in the tree = " +str(dtree.countLeaf(dtree.root)))
print("Accuracy of the model on the training dataset = " + str(calculateAccuracy(df,dtree))+"%")
print("")
print("Number of validation instances = " +str(dvalidation.shape[0]))
print("Number of validation attributes = " +str(dvalidation.shape[1]-1))
print("Accuracy of the model on the validation dataset before pruning = "+ str(calculateAccuracy(dvalidation,dtree))+"%")
print("")
print("Number of testing instances = "+str(dtest.shape[0]))
print("Number of testing attributes = "+str(dtest.shape[1]-1))
print("Accuracy of the model on the testing dataset = "+ str(calculateAccuracy(dtest,dtree))+"%")
print("")
print("-------------------------------------")
print("Post-Pruned Tree")
print("-------------------------------------")
bestTree.printTree(bestTree.root)
print("")
print("-------------------------------------")
print("Post-Pruned Accuracy")
print("-------------------------------------")
print("Number of training instances = " + str(df.shape[0]))
print("Number of training attributes = " + str(df.shape[1] - 1))
print("Total number of nodes in the tree = " + str(bestTree.countNodes(bestTree.root)))
print("Number of leaf nodes in the tree = " + str(bestTree.countLeaf(bestTree.root)))
print("Accuracy of the model on the training dataset = " + str(calculateAccuracy(df, bestTree)) + "%")
print("")
print("Number of validation instances = " + str(dvalidation.shape[0]))
print("Number of validation attributes = " + str(dvalidation.shape[1] - 1))
print("Accuracy of the model on the validation dataset after pruning = " + str(calculateAccuracy(dvalidation, bestTree)) + "%")
print("")
print("Number of testing instances = " + str(dtest.shape[0]))
print("Number of testing attributes = " + str(dtest.shape[1] - 1))
print("Accuracy of the model on the testing dataset = " + str(calculateAccuracy(dtest, bestTree)) + "%")
print("")
if(maxAccuracy > calculateAccuracy(dvalidation, dtree)):
print("Successfully Pruned with improvement in Accuracy on validation data set.")
else:
print("Pruned but Accuracy didn't improved after 10 attempts so returning same tree.")