-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeVisitor.py
More file actions
55 lines (36 loc) · 1.31 KB
/
Copy pathTreeVisitor.py
File metadata and controls
55 lines (36 loc) · 1.31 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
from abc import ABC, abstractmethod
from Node import Parent
import logging
class TreeVisitor(ABC):
@abstractmethod
def visit_parent(self, parent):
pass
@abstractmethod
def visit_leaf(self, leaf):
pass
class PrintVisitor(TreeVisitor):
def __init__(self, depth):
self.depth = depth
def visit_parent(self, parent):
print('\t' * (self.depth-1) + "parent, feature index " + str(parent.feature_index) + ", threshold " + str(parent.threshold))
logging.debug("left:")
logging.debug(isinstance(parent.left_child, Parent))
self.depth += 1
parent.left_child.accept_visitor(self)
logging.debug("left:")
logging.debug(isinstance(parent.left_child, Parent))
parent.right_child.accept_visitor(self)
self.depth -= 1
def visit_leaf(self, leaf):
print('\t' * (self.depth-1) + "leaf, label " + str(leaf.value))
class FeatureImportanceVisitor(TreeVisitor):
def __init__(self,occurences):
self.occurences = occurences
def visit_parent(self, parent):
feature_index = parent.feature_index
self.occurences[feature_index] += 1
parent.left_child.accept_visitor(self)
parent.right_child.accept_visitor(self)
@classmethod
def visit_leaf(self,leaf):
pass