-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
93 lines (74 loc) · 2.88 KB
/
functions.py
File metadata and controls
93 lines (74 loc) · 2.88 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
from telnetlib import GA
from tkinter import E
from decisiontree.Table import Table
from decisiontree.GainRatio import Gain_Ratio
from decisiontree.Node import Node
def split_node_to_childrens(node: Node):
gain_ratio=node.gainratio()
rows=node.table
max_value = max(gain_ratio)
max_index = gain_ratio.index(max_value)
uniq_rows=[row[max_index] for row in rows]
new_uniq_rows=[]
for element in uniq_rows:
if element not in new_uniq_rows:
new_uniq_rows.append(element)
# uniq_rows = list(set([row[max_index] for row in rows]))
uniq_rows=new_uniq_rows
next_floor = []
for element in uniq_rows:
new_branch = []
for row in rows:
if row[max_index] == element:
new_branch.append(row)
next_floor.append(new_branch)
return next_floor, max_index ,max_value
def make_decision_for_nodes(node: Node, atribut_index, max_gain):
new_nodes=[]
for index, children_rows in enumerate(node.childrens):
new_node = Node()
new_node.table = children_rows
decisions=[row[-1] for row in children_rows]
if max_gain ==0:
new_node.decision=decisions[0]
# print(children_rows)
new_node.value=children_rows[0][atribut_index]
# print(atribut_index)
new_nodes.append(new_node)
else:
new_node.attribute= atribut_index
new_node.value = children_rows[0][atribut_index]
# print(new_node.attribute)
new_nodes.append(new_node)
#TODO set atribut to n number for this node
# fix atronute count
return new_nodes
def get_node(node):
new_children_node=[]
node.childrens,atribut_index, max_gain=split_node_to_childrens(node)
node.childrens=make_decision_for_nodes(node,atribut_index,max_gain)
node.attribute=atribut_index
for index, children_node in enumerate(node.childrens):
if children_node.decision is None:
children_node.childrens,atribut_index, max_gain=split_node_to_childrens(children_node)
children_node.childrens = make_decision_for_nodes(children_node,atribut_index, max_gain)
node.childrens[index]=get_node(children_node)
# new_children_node.append(children_node)
else:
new_children_node.append(children_node)
# node.childrens=new_children_node
return node
def showtree(node: Node,przesuniecie):
if node.attribute!=None:
print(" "*przesuniecie,end="")
if node.decision:
print(node.decision,end=" -> ")
if node.value:
print(node.value,"->","Atrybut",node.attribute)
if node.value==None:
print("Atrybut", node.attribute)
for p in node.childrens:
showtree(p,przesuniecie+7)
else:
print(" "*przesuniecie,end="")
print(node.value, "->" ,node.decision)