-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecision_trees.py
214 lines (153 loc) · 5.34 KB
/
Decision_trees.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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 26 00:03:35 2021
@author: abdka
"""
import numpy as np
class SimpleBalancedDecisionTree():
def __init__(self, indices, test_values):
self.indices = indices
self.test_values = test_values
return
def predict(self, data_row):
i = 0
# Move to right if
while i < len(indices):
if data_row[indices[i]] < self.test_values[i]:
i = (2*i)+2
# If leaf node (also no more nodes)
if len(indices) <= i:
return 1
# Move to left
else:
i = (2*i)+1
# If leaf node (also no more nodes)
if len(indices) <= i:
return 0
class DecisionTree():
def __init__(self, head, attr_length):
self.head = head
self.nodes_list = list()
self.attr_length = attr_length
# Adding nodes
# The direction is an array containing zeros and ones
# It describes how to navigate in the tree when placing a node
# 0 means to left, 1 means to right
def add(self, node, directions):
temp = self.head
placed = False
# Repeat directions while not placed
while not placed:
for d in directions:
if d:
if temp.right == None:
temp.right = node
node.parent = temp
placed = True
self.nodes_list.append(node)
return
else:
temp = temp.right
else:
if temp.left == None:
temp.left = node
node.parent = temp
placed = True
self.nodes_list.append(node)
return
else:
temp = temp.left
def predict(self, data_row):
temp = self.head
direction = temp.test(data_row)
if direction:
temp = temp.right
# No nodes on the right
if temp == None:
return 1
else:
temp = temp.left
# No nodes on the left
if temp == None:
return 0
def predict_all(self, data_set):
results = np.zeros(len(data_set))
for i in range(len(data_set)):
results[i] = self.predict[data_set[i]]
def mutation(self):
temp = self.head
mutated = False
index = np.random.randint(0, len(self.nodes_list))
temp = self.nodes_list[index]
temp.indice = np.random.randint(0, self.attr_length)
temp.value = np.random.uniform(0, 10)
def crossover(self, other):
clone = self.clone_tree()
clone2 = other.clone_tree()
rand_node = self.random_node()
rand_node = other.random_node()
index = np.random.randint(0, len(self.nodes_list))
temp = self.nodes_list[index]
def random_node(self):
pass
def clone_tree(self):
clone_head = self.head.clone_with_children()
new_tree = DecisionTree(clone_head)
return new_tree
class SimpleDecisionNode():
def __init__(self, indice, test_value, parent=None, left=None, right=None):
self.parent = parent
self.left = left
self.right = right
self.indice = indice
self.test_value = test_value
def test(self, data_row):
if data_row[sum_val] < self.test_value:
# Move to right
return 1
else:
# Move to left
return 0
def clone_with_children(self):
clone = SimpleDecisionNode(self.indice, self.test_value)
if self.right != None:
clone.right = self.right.clone_with_children()
if self.left != None:
clone.left = self.left.clone_with_children()
return clone
class DecisionNode():
def __init__(self, indices, parameters, test_value):
self.parent = None
self.left = None
self.right = None
self.indices = indices
self.parameters = parameters
self.test_value = test_value
def test(self, data_row):
sum_val = sum(data_row[self.indices]*self.parameters)
if sum_val < self.test_value:
# Move to right
return 1
else:
# Move to left
return 0
def generate_random_tree(vocab_length):
import random
nr_of_nodes = np.random.randint(3, 15)
tree = None
for i in range(nr_of_nodes):
formula_length = np.random.randint(1,5)
indices = np.random.randint(0, vocab_length, formula_length)
parameters = np.random.uniform(0, 1, formula_length)
test_val = formula_length*np.random.uniform(0,5)
node = DecisionNode(indices, parameters, test_val)
directions = np.random.randint(0,2,6)
if tree == None:
tree = DecisionTree(node)
else:
tree.add(node, directions)
return tree
def mutate_tree
arr1 = [0, 1, 2, 3]
indices = [0, 3, 2, 1, 3]
values = [0, 3, 2, 1, 4]