Lab 3 IA2
Lab 3 IA2
Lab 3 IA2
return result
dicc = {}
dicc['new'] = 0
dicc['old'] = 0
dicc['mid'] = 0
dicc['yes'] = 1
dicc['no'] = 1
dicc['swr'] = 2
dicc['hwr'] = 2
def id3(S, dicc):
maxx = -10000
for i in range(0, 2):
if maxx < gain_calc(i, S):
maxx = gain_calc(i, S)
pos = i
id3tree = Tree()
id3tree.data = S[0][pos]
id3tree.children = [Tree() for i in range(3)]
id3tree.children[0].leg = 'new'
id3tree.children[1].leg = 'mid'
id3tree.children[2].leg = 'old'
for i in range(len(id3tree.children)):
continueid3(S, id3tree.children[i], id3tree.children[i].leg, dic
c)
return id3tree
def continueid3(S, id3tree, leg, dicc):
newS = [S[0]]
for i in range(1, len(S)):
if S[i][dicc[leg]] == leg:
newS.append(S[i])
if len(getnrcls(newS)) == 1:
id3tree.data=getnrcls(newS)[0]
else:
maxx = -10000
for i in range(1,2):
print gain_calc(i, newS)
if maxx < gain_calc(i, newS):
maxx = gain_calc(i, newS)
pos = i
id3tree.data = newS[0][pos]
id3tree.children = [Tree() for i in range(2)]
id3tree.children[0].leg = 'yes'
id3tree.children[1].leg = 'no'
for i in range(len(id3tree.children)):
continueid3(newS, id3tree.children[i], id3tree.children[
i].leg, dicc)
copac = id3(S, dicc)
print copac.data
print copac.children[0].leg
print copac.children[0].data
print copac.children[1].leg
print copac.children[1].data
print copac.children[2].leg
print copac.children[2].data
print copac.children[1].children[0].leg
print copac.children[1].children[0].data
print copac.children[1].children[1].leg
print copac.children[1].children[1].data
dike = {}
dike['up'] = []
dike['down'] = []
def afis(copac, dic, currentval):
if copac.children is None:
dic[copac.data].append(currentval+copac.leg)
else:
if copac.leg is None:
currentval = currentval + copac.data + ' '
else:
currentval = currentval + copac.leg + ':' + copac.data +
' '
for i in copac.children:
dic = afis(i, dic, currentval)
return dic
print afis(copac, dike, '')