-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrain.py
More file actions
167 lines (132 loc) · 5.34 KB
/
Copy pathtrain.py
File metadata and controls
167 lines (132 loc) · 5.34 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
# Machine Learning Project
# By James Quaife: j.quiafe1@nuigalway.ie, SID: 14100104
# and Andrew East: a.east1@nuigalway.ie, SID: 16280042
# National University of Ireland, Galway
# Computer Science CT475: Machine Learning
# November 2018
# Supervisor: Dr. Michael Madden
# Teamwork Attribution: This file was written by James Quiafe, with some planning and pair programming with Andrew East
from classes.Case import Case
from classes.Model import Model, Tree
from math import log
def train(data_cases):
model = Model()
model.train = data_cases
model.treeRoot = buildModelTreeRecursive(data_cases)
return model
def buildModelTreeRecursive(data_cases):
tree = Tree()
tree.numCases = len(data_cases)
Tree.next_unique_id += 1
tree.unique_id = Tree.next_unique_id
# Terminating case 1
if len(data_cases) == 0:
tree.isLeaf = True
tree.predicted = "Default"
return tree
countClasses = countClassesInSet(data_cases)
# Terminating case 2
if len(countClasses) == 1:
tree.isLeaf = True
tree.predicted = list(countClasses.keys())[0]
return tree
# Terminating Case 3
haveFoundAnyNonExamined = False
for alreadyExamined in data_cases[0].attributesAlreadyExamined:
if alreadyExamined == False:
haveFoundAnyNonExamined = True
if haveFoundAnyNonExamined == False:
tree.isLeaf = True
max = -1
maxIndex = 0
for idx, item in enumerate(list(countClasses.values())):
if item > max:
max = item
maxIndex = idx
tree.predicted = list(countClasses.keys())[maxIndex]
tree.numCasesMajorityClass = max
return tree
# Internal Node still needs split
infoGained = []
thresholds = []
for attrib in range(len(Case.attributes_names)):
info, threshold = getBestInfoGain(data_cases, attrib)
infoGained.append(info)
thresholds.append(threshold)
# Choose Best One
max = -1.0
best = -1
for idx, item in enumerate(infoGained):
if item > max:
max = item
best = idx
tree.splitAttribute = best
tree.threshold = thresholds[best]
leftList = []
rightList = []
for item in data_cases:
item.attributesAlreadyExamined[best] = True
if item.attributes[best] < thresholds[best]:
leftList.append(item)
else:
rightList.append(item)
tree.leftChild = buildModelTreeRecursive(leftList)
tree.rightChild = buildModelTreeRecursive(rightList)
return tree
def getBestInfoGain(data_cases, attrib):
if data_cases[0].attributesAlreadyExamined[attrib]:
return -1, -1 # This is a check to see if attributes have been examined or not for these data cases
else:
allAttributeValues = []
for item in data_cases:
allAttributeValues.append(item.attributes[attrib])
allAttributeValues.sort()
pInfoGain = []
pThresholds = []
for idx in range(len(allAttributeValues) - 1):
if allAttributeValues[idx + 1] - allAttributeValues[idx] > 0.0001: # This prevents Threshold equalling a data point if data point is duplicate
midValue = (allAttributeValues[idx] + allAttributeValues[idx + 1]) / 2
gain = getInfoGain(data_cases, attrib, midValue)
pInfoGain.append(gain)
pThresholds.append(midValue)
if len(pInfoGain) == 0:
# When ALL the data_cases have an identical value for attrib, the above guard against duplicates means that there will be NO thresholds returned
# To prevent breaking the algorithm, make sure to return at least one data point, threshold chosen as the first data case's attrib
midValue = allAttributeValues[0]
gain = getInfoGain(data_cases, attrib, midValue)
pInfoGain.append(gain)
pThresholds.append(midValue)
max = -1.0
best = -1
for idx, item in enumerate(pInfoGain):
if item > max:
max = item
best = idx
return pInfoGain[best], pThresholds[best]
def countClassesInSet(data_cases):
countClasses = {}
for case in data_cases:
if case.label in countClasses:
countClasses[case.label] += 1
else:
countClasses[case.label] = 1
return countClasses
def getEntropy(countClasses, totalCount):
sum = 0.0
for item in list(countClasses.values()):
p = item / totalCount
sum -= p * log(p, 2)
return sum
def getInfoGain(data_cases, attrib, threshold):
countClasses = countClassesInSet(data_cases)
totalEntropy = getEntropy(countClasses, len(data_cases))
leftList = []
rightList = []
for item in data_cases:
if item.attributes[attrib] < threshold:
leftList.append(item)
else:
rightList.append(item)
leftEntropy = getEntropy(countClassesInSet(leftList), len(leftList))
rightEntropy = getEntropy(countClassesInSet(rightList), len(rightList))
return totalEntropy - leftEntropy * (len(leftList) / len(data_cases)) - rightEntropy * (len(rightList) / len(data_cases))