-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcf_nodes.py
47 lines (34 loc) · 1.21 KB
/
cf_nodes.py
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
class InternalNode:
def __init__(self, level, feature='', n_samples=0, node_id=None, child_left=None, child_right=None,
threshold=None,):
self.node_id = node_id
self.child_left = child_left
self.child_right = child_right
self.feature = feature
self.threshold = threshold
self.n_samples = n_samples
self.depth = level
self.values = {}
def predict(self, x):
if x[self.feature] <= self.threshold:
return self.child_left.predict(x)
else:
return self.child_right.predict(x)
def predict_verbose(self, x, path):
path.append(self.node_id)
if x[self.feature] <= self.threshold:
return self.child_left.predict_verbose(x, path)
else:
return self.child_right.predict_verbose(x, path)
class LeafNode:
def __init__(self, n_samples, level, node_id=None):
self.n_samples = n_samples
self.depth = level
self.node_id = node_id
self.values = {}
self.function = None
def predict(self, X):
return self.function
def predict_verbose(self, x, path):
path.append(self.node_id)
return path