-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecisionTree.py
More file actions
executable file
·214 lines (194 loc) · 6.75 KB
/
decisionTree.py
File metadata and controls
executable file
·214 lines (194 loc) · 6.75 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
import regex as re
from collections import OrderedDict
from math import log2
"""Reader. reads in ARFF-file
Can't handle '?' in data or
any valutypes not within {} for the attributes"""
def reader(filename):
attributes = {}
data = {}
file = open(filename)
input = file.read().lower()
inputLines = re.finditer('(.*\n)', input)
counterAttr = 0
counterData = 0
for line in inputLines:
word = line.group().replace('\n', '')
pos = word.find('%')
if(pos != -1):
word = word[0:pos]
if(word != '' and not re.match('@relation.*', word)
and not re.match('@data.*', word)):
if(re.match('@attribute .*', word)):
pos = word.find(' {')
if(pos != -1):
attr = word[11:pos]
types = word[pos+2:len(word)-1]+","
pos = types.find(',')
attributes[counterAttr] = [attr]
while(pos != -1):
attributes[counterAttr].append(types[0:pos])
types = types[pos+1:len(types)]
pos = types.find(',')
counterAttr = counterAttr+1
else:
word = word+","
for i in range(0,len(attributes.keys())):
pos = word.find(',')
if(pos != -1):
a = word[0:pos]
try:
data[counterData].append((attributes[i][0],a))
except:
data[counterData] = [(attributes[i][0],a)]
word = word[pos+1:len(word)]
counterData = counterData+1
attributesNew = OrderedDict()
for attr in attributes:
try:
attributesNew[attributes[attr][0]].append(attributes[attr][1:len(attributes)])
except:
attributesNew[attributes[attr][0]] = [attr]+ attributes[attr][1:len(attributes)]
i = len(data)
dataNew = []
for j in range(i):
dataNew.append(data[j])
return attributesNew,dataNew
"""Algorithm for Decision Tree"""
treePrint = []
def same_class(examples,types):
nbr = []
for k in range(len(types)):
nbr.append(0)
for e in examples:
for k in range(len(types)):
if e[-1][1] == types[k]:
nbr[k] = nbr[k] + 1
for k in range(len(types)):
if nbr[k] == len(examples):
return True
return False
def nbrPerVal(examples, attributes, attr, val,types):
nbr = []
for k in range(len(types)):
nbr.append(0)
indexAttr = attributes[attr][0]
for e in examples:
if e[indexAttr][1] == val:
for k in range(len(types)):
if e[-1][1] == types[k]:
nbr[k] = nbr[k] + 1
return nbr
def nbrTotal(examples, attributes, attr,types):
nbr = []
for k in range(len(types)):
nbr.append(0)
indexAttr = attributes[attr][0]
for e in examples:
for k in range(len(types)):
if e[-1][1] == types[k]:
nbr[k] = nbr[k] + 1
return nbr
def H(nbrTypes, sum):
if sum == 0:
return 0
res = 0
for t in nbrTypes:
if t != 0:
res = res - (t/sum)*log2(t/sum)
return res
def importance(attributes,examples,types):
max_attr = ''
max_gain = 0
for a in attributes:
if a != 'willwait':
remainder = 0
listOfNbrOfTotal = nbrTotal(examples,attributes,a,types)
sumT = 0
for k in range(len(types)):
sumT = sumT + listOfNbrOfTotal[k]
h = H(listOfNbrOfTotal,sumT)
for v in attributes[a][1:len(attributes[a])]:
listOfNbrOfVal = nbrPerVal(examples,attributes,a,v,types)
sumV = 0
for k in range(len(types)):
sumV = sumV + listOfNbrOfVal[k]
remainder = remainder + (sumV/sumT)*H(listOfNbrOfVal,sumV)
gain = h - remainder
if gain > max_gain:
max_attr = a
max_gain = gain
return max_attr
def plurality_value(examples,types):
nbr = []
for k in range(len(types)):
nbr.append(0)
for e in examples:
for k in range(len(types)):
if e[-1][1] == types[k]:
nbr[k] = nbr[k] + 1
ans = types[0]
max = 0
for k in range(len(types)):
if nbr[k] > max:
ans = types[k]
max = nbr[k]
return ans
def decision_tree_algorithm(examples, attributes,parent_examples,turn):
types = attributes['willwait'][1:len(attributes['willwait'])]
if not examples:
#no more data
treePrint.append((": " + plurality_value(parent_examples,types),0,True))
elif same_class(examples,types):
#alla kvarvarande exempel har samma resultat
treePrint.append((": " + examples[0][-1][1],0,True))
elif not attributes:
#no more attributes
treePrint.append((": " + plurality_value(examples,types),0,True))
else:
a = importance(attributes, examples,types)
i = attributes[a][0]
values = attributes[a][1:len(attributes[a])]
del attributes[a]
for v in values:
treePrint.append((a + " = " + v,turn,False))
exs = []
for j in range(len(examples)):
res = examples[j][i][1]
if res == v:
exs.append(examples[j])
decision_tree_algorithm(exs,attributes,examples,turn+1)
if __name__ == '__main__':
filename = 'input_2res.arff'
attributes, data = reader(filename)
decision_tree_algorithm(data, attributes, data,0)
print("Restaurant 2:")
printout = ""
for i in range(len(treePrint)-1):
if not treePrint[i][2] and treePrint[i+1][2]:
for k in range(treePrint[i][1]):
printout = printout + "\t"
printout = printout + treePrint[i][0]
else:
for k in range(treePrint[i][1]):
printout = printout + "\t"
printout = printout + treePrint[i][0] + "\n"
printout = printout + treePrint[-1][0]
print(printout)
treePrint = []
filename = 'input_3res.arff'
attributes, data = reader(filename)
decision_tree_algorithm(data, attributes, data,0)
print("\nRestaurant 3:")
printout = ""
for i in range(len(treePrint)-1):
if not treePrint[i][2] and treePrint[i+1][2]:
for k in range(treePrint[i][1]):
printout = printout + "\t"
printout = printout + treePrint[i][0]
else:
for k in range(treePrint[i][1]):
printout = printout + "\t"
printout = printout + treePrint[i][0] + "\n"
printout = printout + treePrint[-1][0]
print(printout)