-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
60 lines (52 loc) · 2.49 KB
/
node.py
File metadata and controls
60 lines (52 loc) · 2.49 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
class Node:
def __init__(self, catDict = None, classDict = None):
self.cat = None
self.threshold = None
self.gain = None
self.samples = None
self.mostCommonValue = None
self.percentage = None
self.nodeClass = None
self.false_branch = None
self.true_branch = None
self.countOfEach = None
self.catDict = catDict
self.classDict = classDict
self.time = None
def __str__(self):
self.stringNode(0)
return ""
# =====Classifiers======
def classify(self, catDict):
if self.false_branch == None:
return self.nodeClass
catValue = catDict[self.cat]
if catValue < self.threshold:
return self.false_branch.classify(catDict)
else:
return self.true_branch.classify(catDict)
def classifyPercent(self, catDict):
if self.false_branch == None:
return self.nodeClass, self.percentage
catValue = catDict[self.cat]
if catValue < self.threshold:
return self.false_branch.classifyPercent(catDict)
else:
return self.true_branch.classifyPercent(catDict)
# =====Printers=====
def stringNode(self,n):
ROUND = 3
if not self.false_branch == None:
if self.catDict == None:
print(" "*n+"=>", "{} >= {}, Gain: {}".format(self.cat, round(self.threshold, ROUND), round(self.gain, ROUND) if not self.gain == None else "None"), ", MCV: ", self.mostCommonValue, ", P: ", round(self.percentage, ROUND), '% --- COE: ', self.countOfEach)
else:
print(" "*n+"=>", "{}({}) >= {}, Gain: {}".format(self.catDict[self.cat],self.cat, round(self.threshold, ROUND), round(self.gain, ROUND) if not self.gain == None else "None"), ", MCV: ", self.mostCommonValue, ", P: ", round(self.percentage, ROUND), '% --- COE: ', self.countOfEach)
self.false_branch.stringNode(n+1)
self.true_branch.stringNode(n+1)
else:
if self.classDict == None:
print(" "*n+"->", "Class: {}".format(self.nodeClass), ", MCV: ", self.mostCommonValue, ", P: ", round(self.percentage, ROUND), '% --- COE: ', self.countOfEach)
else:
print(" "*n+"->", "Class: {}({})".format(self.classDict[self.nodeClass],self.nodeClass), ", MCV: ", self.mostCommonValue, ", P: ", round(self.percentage, ROUND), '% --- COE: ', self.countOfEach)
def setTime(self, time):
self.time = time