-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdt_learning.py
More file actions
164 lines (149 loc) · 7.05 KB
/
dt_learning.py
File metadata and controls
164 lines (149 loc) · 7.05 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
import math
class Node:
def __init__(self):
self.value = None
self.height = None
self.branch = []
self.children = []
class DecisionTree:
def __init__(self, attributes_indexes, target_position):
self.attributes_indexes = attributes_indexes
self.target_position = target_position
self.height = 0
def get_max_height(self):
return self.height
def height_update(self, h):
if h > self.height:
self.height = h
def same_classification(self, examples):
classification = examples[0][self.target_position]
for ex in examples:
if ex[self.target_position] != classification:
return False
return True
@staticmethod
def check_missing(examples, index):
for ex in examples:
if ex[index] == '':
return True
return False
@staticmethod
def get_values(examples, index):
attr_values = []
for ex in examples:
value = ex[index]
if value not in attr_values and value != '':
attr_values.append(value)
return attr_values
def plurality_value(self, examples, weights):
target_values = self.get_values(examples, self.target_position)
target_values_occur = self.get_weighted_occur(weights, target_values, examples, self.target_position)
return target_values[target_values_occur.index(max(target_values_occur))]
@staticmethod
def get_weighted_occur(weights, attr_values, examples, index):
weights_per_val = []
for val in attr_values:
wgt = 0
for ex in examples:
if ex[index] == val:
wgt += weights[examples.index(ex)]
weights_per_val.append(wgt)
return weights_per_val
@staticmethod
def get_prob(known_attr_values, weighted_occur, known_weights, attribute_values):
attr_probability = []
total_weight = sum(known_weights)
for val in attribute_values:
if val in known_attr_values:
attr_probability.append(weighted_occur[known_attr_values.index(val)] / total_weight)
else:
attr_probability.append(0)
return attr_probability
@staticmethod
def get_entropy(values, values_occurrence, total_weight):
entropy = 0
for i in range(len(values)):
if values_occurrence[i] != 0:
p_i = values_occurrence[i] / total_weight
entropy -= p_i * math.log(p_i, 2)
return entropy
def gain(self, entropy_s, examples, attributes_values, weights):
gain_per_attribute, prob = [], []
s = sum(weights)
for attr in self.attributes_indexes:
if self.check_missing(examples, attr) and s != 0:
known_examples, known_weights = [], []
for ex in examples:
if ex[attr] != '':
known_examples.append(ex)
known_weights.append(weights[examples.index(ex)])
known_attr_values = self.get_values(known_examples, attr)
weighted_occur = self.get_weighted_occur(known_weights, known_attr_values, known_examples, attr)
prob.append(self.get_prob(known_attr_values, weighted_occur, known_weights, attributes_values[attr]))
else:
prob.append([1 for _ in attributes_values[attr]])
remainder = 0
for v in range(len(attributes_values[attr])):
examples_per_v, weights_per_v = [], []
for ex in examples:
if ex[attr] == attributes_values[attr][v]:
examples_per_v.append(ex)
weights_per_v.append(weights[examples.index(ex)])
elif ex[attr] == '':
examples_per_v.append(ex)
weights_per_v.append(weights[examples.index(ex)] * prob[self.attributes_indexes.index(attr)][v])
if len(examples_per_v) != 0 and s != 0:
target_values_per_v = self.get_values(examples_per_v, self.target_position)
target_values_occur_per_v = self.get_weighted_occur(weights_per_v, target_values_per_v,
examples_per_v, self.target_position)
s_v = sum(weights_per_v)
remainder += (s_v / s) * self.get_entropy(target_values_per_v, target_values_occur_per_v, s_v)
gain_per_attribute.append(entropy_s - remainder)
max_gain_index = gain_per_attribute.index(max(gain_per_attribute))
return self.attributes_indexes[max_gain_index], prob[max_gain_index]
def dt_learning(self, examples, parent_examples, attributes, attributes_values, weights, parent_wgt, height, max_h):
root = Node()
root.height = height
if root.height == max_h:
if len(examples) == 0:
root.value = self.plurality_value(parent_examples, parent_wgt)
return root
elif self.same_classification(examples):
root.value = examples[0][self.target_position]
return root
else:
root.value = self.plurality_value(examples, weights)
return root
if len(examples) == 0:
self.height_update(height)
root.value = self.plurality_value(parent_examples, parent_wgt)
return root
elif len(self.attributes_indexes) == 0:
self.height_update(height)
root.value = self.plurality_value(examples, weights)
return root
elif self.same_classification(examples):
self.height_update(height)
root.value = examples[0][self.target_position]
return root
else:
target_values = self.get_values(examples, self.target_position)
target_values_occur = self.get_weighted_occur(weights, target_values, examples, self.target_position)
entropy_s = self.get_entropy(target_values, target_values_occur, sum(weights))
index, prob = self.gain(entropy_s, examples, attributes_values, weights)
root.value = attributes[index]
for value in attributes_values[index]:
root.branch.append(value)
exs, wgt = [], []
for ex in examples:
if ex[index] == value:
exs.append(ex)
wgt.append(weights[examples.index(ex)])
elif ex[index] == '':
exs.append(ex)
wgt.append(weights[examples.index(ex)] * prob[attributes_values[index].index(value)])
if len(self.attributes_indexes) > 0 and index in self.attributes_indexes:
self.attributes_indexes.pop(self.attributes_indexes.index(index))
root.children.append(
self.dt_learning(exs, examples, attributes, attributes_values, wgt, weights, height + 1, max_h))
return root