-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecisionTree.py
168 lines (106 loc) · 5.01 KB
/
DecisionTree.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import numpy as np
class Node():
def __init__(self, feature=None, threshold=None, left=None, right=None, split_factor=None, *, value=None):
self.feature = feature
self.threshold = threshold
self.left = left
self.right = right
self.split_factor = split_factor
self.value = value
def is_leaf_node(self):
return self.value is not None
class DecisionTree():
def __init__(self, min_samples_split=2, max_depth=10):
# stopping condition
self.min_samples_split = min_samples_split
self.max_depth = max_depth
self.root = None
def _build_tree(self, X, y, depth=0):
num_samples, _ = X.shape
if depth >= self.max_depth or num_samples <= self.min_samples_split:
return Node(value=self._eval_leaf_val(y))
split_feature, split_threshold, split_factor, left_idxs, right_idxs = self._best_split(X, y)
if split_factor == 0:
return Node(value=self._eval_leaf_val(y))
left_subtree = self._build_tree(X.iloc[left_idxs], y.iloc[left_idxs], depth+1)
right_subtree = self._build_tree(X.iloc[right_idxs], y.iloc[right_idxs], depth+1)
return Node(split_feature, split_threshold, left_subtree, right_subtree, split_factor)
def _best_split(self, X, y):
split_feature, split_threshold = None, None
best_left_idxs, best_right_idxs = None, None
best_split_factor = float("-inf")
for feature in X.columns:
X_col = X[feature]
thresholds = np.unique(X_col)
for thr in thresholds:
left_idxs, right_idxs = self._split(thr, X_col)
split_factor = self._eval_split_factor(y, y.iloc[left_idxs], y.iloc[right_idxs])
if split_factor > best_split_factor:
best_split_factor = split_factor
split_feature = feature
split_threshold = thr
best_left_idxs = left_idxs
best_right_idxs = right_idxs
return split_feature, split_threshold, best_split_factor, best_left_idxs, best_right_idxs
def _split(self, threshold, X_col):
left_idxs = np.argwhere(X_col <= threshold).flatten()
right_idxs = np.argwhere(X_col > threshold).flatten()
return left_idxs, right_idxs
def _make_prediction(self, record, node):
if node.is_leaf_node():
return node.value
if record[node.feature] <= node.threshold:
return self._make_prediction(record, node.left)
else:
return self._make_prediction(record, node.right)
def fit(self, X, y):
self.root = self._build_tree(X, y)
def predict(self, X):
arr = []
for _, record in X.iterrows():
arr.append(self._make_prediction(record, self.root))
return arr
class DecisionTreeClassifier(DecisionTree):
def __init__(self, min_samples_leaf=2, max_depth=10, criterion="gini"):
super().__init__(min_samples_leaf, max_depth)
self.criterion = criterion
def _eval_leaf_val(self, y):
class_labels = np.unique(y)
dp = np.zeros(len(class_labels))
for y_in in y:
for label_idx, label in enumerate(class_labels):
dp[label_idx] += 1 if y_in == label else 0
return class_labels[np.argmax(dp)]
def _eval_split_factor(self, y, y_left, y_right):
return self._info_gain(y, y_left, y_right)
def _info_gain(self, y, y_left, y_right):
num_samples, = y.shape
left_weight = y_left.shape[0] / num_samples
right_weight = y_right.shape[0] / num_samples
return self._entropy(y) - left_weight*self._entropy(y_left) - right_weight*self._entropy(y_right)
def _entropy(self, y):
class_labels = np.unique(y)
dp = np.zeros(len(class_labels))
for y_in in y:
for label_idx, label in enumerate(class_labels):
dp[label_idx] += 1 if y_in == label else 0
dp_prob = dp / len(y)
if self.criterion == "entropy":
return -np.sum([p*np.log2(p) if p != 0 else 0 for p in dp_prob])
# gini index
return -np.sum([p*p if p != 0 else 0 for p in dp_prob])
class DecisionTreeRegressor(DecisionTree):
def __init__(self, min_samples_leaf=2, max_depth=10):
super().__init__(min_samples_leaf, max_depth)
def _eval_leaf_val(self, y):
return 0 if y.shape[0] == 0 else np.mean(y)
def _eval_split_factor(self, y, y_left, y_right):
return self._variance_reduction(y, y_left, y_right)
def _variance_reduction(self, y, y_left, y_right):
num_samples, = y.shape
left_weight = y_left.shape[0] / num_samples
right_weight = y_right.shape[0] / num_samples
return self._variance(y) - (left_weight*self._variance(y_left) + right_weight*self._variance(y_right))
def _variance(self, y):
avg = np.mean(y) / (y.shape[0] -1)
return np.sum([(avg-x)**2 for x in y])