-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.py
More file actions
41 lines (29 loc) · 901 Bytes
/
Copy pathNode.py
File metadata and controls
41 lines (29 loc) · 901 Bytes
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
from abc import ABC, abstractmethod
class Node(ABC):
@abstractmethod
def predict(self, x):
pass
@abstractmethod
def accept_visitor(self, visitor):
pass
class Parent(Node):
def __init__(self, feature_index, threshold, depth):
self.feature_index = feature_index
self.threshold = threshold
self.depth = depth
self.left_child = None
self.right_child = None
def predict(self, x):
if (x[self.feature_index] < self.threshold):
return self.left_child.predict(x)
else:
return self.right_child.predict(x)
def accept_visitor(self, visitor):
visitor.visit_parent(self)
class Leaf(Node):
def __init__(self, value):
self.value = value
def predict(self, x):
return self.value
def accept_visitor(self, visitor):
visitor.visit_leaf(self)