-
Notifications
You must be signed in to change notification settings - Fork 0
/
ID3.py
466 lines (391 loc) · 11.8 KB
/
ID3.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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import pandas as pd
import numpy as np
import math
# Load the Training and Testing data
data=pd.read_csv("iriss.csv")
print(data.iloc[:].values)
header=list(data.columns)
header.remove("loan")
x_train=np.concatenate((data.iloc[:7817,:7].values,data.iloc[:7817,8:].values),axis=1)
y_train=data.iloc[:7817,7].values
x_test=np.concatenate((data.iloc[7817:10162,:7].values,data.iloc[7817:10162,8:].values),axis=1)
y_test=data.iloc[7817:10162,7].values
x1_test=np.concatenate((data.iloc[10162:,:7].values,data.iloc[10162:,8:].values),axis=1)
y1_test=data.iloc[10162:,7].values
# class node for building the decision tree
class node:
pass
# Entropy function to calculate the entropy
def Entropy(p,n):
if p==0 or n==0:
return 0
p1=p/(p+n)
n1=n/(p+n)
en=(p1)*(-1)*math.log(p1,2)+n1*(-1)*math.log(n1,2)
return en
# Function for counting number of positive and negative example in dataset
def count(y):
p=0
n=0
for i in range(len(y)):
if y[i]=="yes":
p+=1
else:
n+=1
return p,n
# Function for checking, attribute is numeric or not
def isstring(x):
try:
v=int(x)
return False
except:
v=str(x)
return True
# Function for counting the values and examples on a attribute
def count_attribut(x,y):
dict={}
dicp={}
for i in range(len(x)):
if dict.get(x[i])==None:
dict[x[i]]=1
if(y[i]=="yes"):
dicp[x[i]]=1
else:
dict[x[i]]+= 1
if (y[i] == "yes"):
if dicp.get(x[i])==None:
dicp[x[i]]= 1
else:
dicp[x[i]]+=1
return dict,dicp
# Function for calculating the median
def median(x):
x=sorted(x)
if len(x)%2==0:
return (x[int(len(x)/2)+1]+x[int(len(x)/2)])/2
else:
return x[int(len(x)/2)]
# Function for calculating the information gain for a attribute
def infoGain(x,y,cn):
p,n=count(y)
s=Entropy(p,n)
ln=len(y)
if(isstring(x[0][cn])):
sum=0
dict,dicp=count_attribut(x[:,cn].copy(),y.copy())
for i in dict.keys():
if dicp.get(i)==None:
p=0
n1=dict[i]
else:
p=(dicp[i])/dict[i]
n1=(dict[i]-dicp[i])/dict[i]
sum+=Entropy(p,n1)*dict[i]/ln
sum=s-sum;
li=[]
li.append(float(sum))
li.append(len(dict.keys()))
return li
else:
m=median(x[:,cn].copy())
lp=[0,0]
rp=[0,0]
for i in range(len(y)):
if(x[i][cn]<m):
if y[i]=="yes":
lp[0]+=1
else:
lp[1]+=1
else:
if y[i]=="yes":
rp[0]+=1
else:
rp[1]+=1
tlp=(lp[0]+lp[1])/ln
trp=(rp[0]+rp[1])/ln
sum=(s-(tlp*Entropy(lp[0],lp[1])+trp*Entropy(rp[0],rp[1])))
li=[]
li.append(float(sum))
li.append(-1)
return li
# Function for distributing the examples on their children in the case of numerical attribute
def make_tableR(x,y,c,la):
x1=np.ndarray(0)
y1=np.ndarray(0)
if(c==0):
m = median(x[:, la].copy())
for i in range(len(x)):
if(x[i][la]<m):
if x1.size == 0:
x1 = np.hstack(x[i, :])
x1 = np.vstack((x1, x[i, :]))
x1 = np.delete(x1, (0), axis=0)
y1 = np.append(y1, y[i])
else:
x1 = np.vstack((x1, x[i, :]))
y1 = np.append(y1, y[i])
elif (c == 1):
m = median(x[:, la].copy())
for i in range(len(x)):
if (x[i][la] >=m):
if x1.size == 0:
x1 = np.hstack(x[i, :])
x1 = np.vstack((x1, x[i, :]))
x1 = np.delete(x1, (0), axis=0)
y1 = np.append(y1, y[i])
else:
x1 = np.vstack((x1, x[i, :]))
y1 = np.append(y1, y[i])
return x1,y1
# Function for distributing the examples on their children in the case of non_numeric attribute
def make_tableC(x,y,lab,l):
x1=np.ndarray(0)
y1=np.ndarray(0)
for i in range(len(x)):
if x[i][l]==lab:
if x1.size==0:
x1=np.hstack(x[i,:])
x1=np.vstack((x1,x[i,:]))
x1=np.delete(x1,(0),axis=0)
y1=np.append(y1,y[i])
else:
x1=np.vstack((x1,x[i,:]))
y1=np.append(y1,y[i])
return x1,y1
# Main function for building the tree
def build_tree(x_train,y_train,he):
p,n=count(y_train)
if(len(he)==0):
h1=node()
h1.label="null"
h1.pn=[]
h1.branch=[]
h1.pn.append(p)
h1.pn.append(n)
h1.leaf=True
if(p>=n):
h1.leafv="yes"
else:
h1.leafv="no"
return h1
elif p<4 or n<6:
h1 = node()
h1.pn = []
h1.branch=[]
h1.pn.append(p)
h1.pn.append(n)
h1.label = "null"
h1.leaf = True
if (p >= n):
h1.leafv = "yes"
else:
h1.leafv = "no"
return h1
gain=[]
for i in range(len(he)):
gain.append(list(infoGain(x_train.copy(),y_train.copy(),i)))
l=[0.0000,0]
la=0.0000000000
labe=0
for i in range(0,len(gain)):
if la<=float(gain[i][0]):
l[0]=gain[i][0]
l[1]=gain[i][1]
la=l[0]
labe=i
if (l[1] == 1) or l[1] == 0 :
h1 = node()
h1.pn = []
h1.branch=[]
h1.pn.append(p)
h1.pn.append(n)
h1.label = "null"
h1.leaf = True
if (p >= n):
h1.leafv = "yes"
else:
h1.leafv = "no"
return h1
nw=node()
nw.branch=[]
nw.addr=[]
nw.leaf=False
nw.leafv="null"
nw.pn=[]
p,n=count(y_train)
nw.pn.append(p)
nw.pn.append(n)
nw.label=he[labe]
if l[1]!=-1:
dict, dicp = count_attribut(x_train[:, labe].copy(), y_train.copy())
hade = list(dict.keys())
he.remove(he[labe])
if l[1]==-1:
for i in range(2):
nw.branch.append(median(x_train[:,labe].copy()))
x_train1,y_train1=make_tableR(x_train,y_train,i,labe)
if x_train1.size!=0:
x_train1 =(np.concatenate((x_train1[:, :labe], x_train1[:, labe + 1:]), axis=1))
nw.addr.append(build_tree(x_train1.copy(),y_train1.copy(),he.copy()))
return nw
else:
for i in range(l[1]):
x_train1,y_train1=make_tableC(x_train,y_train,hade[i],labe)
nw.branch.append(hade[i])
if x_train1.size!=0:
x_train1 =(np.concatenate((x_train1[:, :labe], x_train1[:, labe + 1:]), axis=1))
nw.addr.append(build_tree(x_train1.copy(),y_train1.copy(),he.copy()))
return nw
# Function for giving the index of label
def Give_Hnumb(st,hd):
for i in range(len(hd)):
if(st==hd[i]):
return i
# Function for calculating the accuracy
def accuracy(x,y):
c=0
for i in range(len(x)):
if x[i]==y[i]:
c+=1
return(c/len(x))*100
# Function for testing the data on tree
def TestPhase(x_test,y_test,hn,hv,head,hd,v11):
predict=np.ndarray(0)
for i in range(len(x_test)):
h=head
while(h.leaf==False):
index=Give_Hnumb(h.label,hd)
c=0
co=0
if(isstring(x_test[i][index])):
for j in range(len(h.branch)):
if x_test[i][index]==h.branch[j]:
if(hv!="null"):
if(h.addr[j]==hn):
c=1
break
h=h.addr[j]
co=1
break;
if co==0:
co=-1
break;
else:
if x_test[i][index]<h.branch[0]:
if (hv != "null"):
if (h.addr[0] == hn):
c = 1
break
h=h.addr[0]
else:
if (hv != "null"):
if (h.addr[1] == hn):
c = 1
break
h=h.addr[1]
if c==1:
predict=np.append(predict,hv)
elif co==-1:
if h.pn[0]>h.pn[1]:
va="yes"
else:
va="no"
predict=np.append(predict,va)
else:
predict=np.append(predict,h.leafv)
if v11==1:
return accuracy(predict,y_test)
elif v11==0:
return accuracy(predict,y_test),predict
# Function, checks that the node has all branches leaf or not
def Notleaff(n1):
for i in range(len(n1.branch)):
n2=n1.addr[i]
if(n2.leaf==False):
return True
return False
# Function that use in pruning to find the node , that has all branches leaf, and their parent
def last_subtree(n2,j):
n1=n2
nw= n1
n1= n1.addr[j]
while(Notleaff(n1)==True):
nw=n1
for i in range(len(n1.branch)):
n2 = n1.addr[i]
if (n2.leaf == False):
n1 = n1.addr[i]
break;
return nw,n1
# Function for pruning the tree
def Post_Pruning(h,x_test,y_test,head):
n1=h
n2=h
for j in range(0,len(n1 .branch)):
n2=h
pre,ch=last_subtree(n2,j)
if pre.pn[0]>pre.pn[1]:
v="yes"
else:
v="no"
while(TestPhase(x_test,y_test,None,"null",h,head,1)<=TestPhase(x_test,y_test,ch,v,h,head,1)):
ch.leaf=True
ch.leafv=v
ch.branch=[]
ch.addr=[]
pre,ch=last_subtree(n2,j)
if ch.label==n2.addr[j].label:
break
if pre.pn[0] > pre.pn[1]:
v = "yes"
else:
v = "no"
return n1
# Function for splitting the dataset for k-fold cross_validation
def split(x_train,y_train,c):
if(c==0):
x_cross=x_train[:469]
y_cross=y_train[:469]
x_train1=x_train[469:]
y_train1 = y_train[469:]
else:
x_cross = x_train[c:c+469]
y_cross = y_train[c:c+469]
x_train1 = np.concatenate((x_train[:c,:],x_train[c+469:,:]),axis=0)
y_train1 = np.concatenate((y_train[:c],y_train[c+469:]),axis=0)
return x_cross,y_cross,x_train1,y_train1
# Function for k-fold cross validation
def K_fold(x_train,y_train,x1_test,y1_test):
c=0
acc=[]
nw=node()
nw.addr=[]
while(c!=2345):
x_cross,y_cross,x_train1,y_train1=split(x_train.copy(),y_train.copy(),c)
hed = list(data.columns)
hed.remove("loan")
n1=build_tree(x_train1.copy(),y_train1.copy(),hed.copy())
h1=Post_Pruning(n1,x_cross,y_cross,hed.copy())
acc.append(TestPhase(x1_test.copy(),y1_test.copy(),None,"null",h1,hed.copy(),1))
nw.addr.append(h1)
c+=469
c=0.0
index=0;
print("Accuracy list for different different tree that form using k-fold cross validation is: ")
print(acc)
for i in range(len(acc)):
if c<=acc[i]:
c=acc[i]
index=i
return nw.addr[index]
#Main part
if __name__=="__main__":
print(" ********** ID3 ALGORITHM ********** ")
head=K_fold(x_train,y_train,x1_test,y1_test)
acc,predict=TestPhase(x_test,y_test,None,"null",head,header.copy(),0)
print(" Predict values on Testing data set are: ")
print(predict)
print(" True value of Testing data set are: ")
print(y_test)
print("Accuracy on testing dataset is: ")
print(acc)