-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
executable file
·152 lines (123 loc) · 3.68 KB
/
node.py
File metadata and controls
executable file
·152 lines (123 loc) · 3.68 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
class Node:
def __init__(self, label = False, train_mode = None):
self.label = label # Class type or attribute to split on
self.children = {}
self.type = "Leaf"
self.train_mode = train_mode
self.prune_test = False # Only used for additional prune method Prune A
self.prune_hold = None # Only used for additional prune method Prune A
# Core
def add_subtree(self,subtree,attribute):
"""
Adds children to current node.
"""
self.children[attribute] = subtree
self.type = "Split"
def evaluate(self,example):
"""
Performs evaluation. If node is a leaf, returns label as classification.
If not, attempts to send down tree. If cannot send down because
split attribute not present, classified as training mode.
Itype: Dictionary
Rtype: String (Classification)
"""
if self.is_leaf():
return self.label
else:
try:
split = example[self.label]
sub = self.children[split]
return sub.evaluate(example)
except:
return self.train_mode
def is_leaf(self):
"""
Checks if current node is of type leaf. Redundantly checks
type for safe pruning.
"""
if len(self.children) == 0 or self.type == "Leaf" or self.type == "Prune":
return True
else:
return False
def all_children_leaf(self):
"""
Checks is all children are leaves. Returns Boolean.
"""
if self.is_leaf():
return False
else:
for i in self.children.values():
if i.is_leaf() != True:
return False
return True
def some_leaf(self):
"""
Checks is some children are leaves. Returns Boolean.
"""
if self.is_leaf():
return False
else:
for i in self.children.values():
if i.is_leaf():
True
return False
def self_prune(self,bool):
"""
Permanantly prunes self. All childen are released from memory.
To prune, input bool must be of type True.
"""
if bool == True:
self.children = {}
self.type = "Leaf"
self.label = self.train_mode
self.prune_test = False
else:
return
# Prune core with get_method_2
def node_leaves(self):
"""
Additional Prune method A helper. Returns all leafs.
"""
my_leaves = []
for claim, i in self.children.items():
if i.is_leaf() == True:
my_leaves.append(claim)
return my_leaves
def prune_this_leaf(self,name):
"""
Additional Prune method A helper. Performs temporary prune.
"""
self.prune_hold = (name,self.children.pop(name))
def finish_leaf_prune(self):
"""
Additional Prune method A helper. Performs permanent prune.
"""
self.prune_hold = None
def unprune_leaf(self):
"""
Additional Prune method A helper. Mutates temporary prune back
to original state.
"""
name = self.prune_hold[0]
sub = self.prune_hold[1]
self.children[name] = sub
def prune_evaluate(self,example):
"""
Additional Prune method B helper.
"""
if self.all_children_leaf() == True:
return self.train_mode
else:
try:
split = example[self.label]
sub = self.children[split]
return sub.evaluate(example)
except:
return self.train_mode
#split = example[self.label]
#sub = self.children[split]
#return sub.prune_evaluate(example)
#def add_parent_split(self,parent_split):
#self.parent_split = parent_split
#def split_type(self):
#return self.label