-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecision_tree.py
More file actions
287 lines (238 loc) · 9.98 KB
/
decision_tree.py
File metadata and controls
287 lines (238 loc) · 9.98 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import numpy as np
def gini_inpurity(y):
if not isinstance(y, np.ndarray):
y = np.array(y)
probabilities = np.bincount(y)/y.shape[0]
return 1 - np.sum(probabilities**2)
def entropy_inpurity(y):
if not isinstance(y, np.ndarray):
y = np.array(y)
probabilities = np.bincount(y)/y.shape[0]
return np.sum(-probabilities * np.log2(probabilities, where=(probabilities > 0)))
def mse_impurity(y):
y_mean = np.mean(y)
return np.mean((y - y_mean)**2)
def mae_impurity(y):
y_mean = np.median(y)
return np.mean(y - y_mean)
class Node:
def __init__(self, feature_idx=None, threshold=None, left=None, right=None, value=None):
self.feature_idx = feature_idx
self.threshold = threshold
self.left = left
self.right = right
self.value = value
def is_leaf(self):
return self.value is not None
class DecisionTree:
def __init__(self, criterion="gini", max_depth=3, max_features=1, min_samples_split=2, regression=False):
self.root = None
self.criterion = criterion
if self.criterion == "gini":
self.criterion_fn = gini_inpurity
elif self.criterion == "entropy":
self.criterion_fn = entropy_inpurity
elif self.criterion == "mse":
self.criterion_fn = mse_impurity
else:
raise ValueError(f"{criterion} is not implemented.")
self.max_depth = max_depth
self.max_features = max_features
self.min_samples_split = min_samples_split
self.regression = regression
if self.regression:
if self.criterion not in {"mae", "mse"}:
raise ValueError("For regression criterion should be mae or mse.")
def _check_X(self, X):
if not isinstance(X, np.ndarray):
X = np.array(X, dtype=np.float32)
if X.size == 0:
raise ValueError("The array X must be non-empty")
return X
def _check_inputs(self, X, y):
X = self._check_X(X)
if y is None:
raise ValueError("Argument y is required.")
if not isinstance(y, np.ndarray):
y = np.array(y)
if y.size == 0:
raise ValueError("The array y must be non-empty")
return X, y
def _get_feature_thresholds(self, X, feature):
unique_values = np.unique(X[:, feature])
n_unique = len(unique_values)
thresholds = []
for i in range(1, n_unique):
thresholds.append((unique_values[i-1] + unique_values[i])/2)
return thresholds
def _split_mask(self, X, feature, threshold):
left_idx = (X[:, feature] <= threshold)
right_idx = (X[:, feature] > threshold)
return left_idx, right_idx
def _split(self, X, y, feature, threshold):
left_idx, right_idx = self._split_mask(X, feature, threshold)
y_left = y[left_idx]
y_right = y[right_idx]
return y_left, y_right
def _leafs_impurity(self, X, y, feature, threshold):
left_idx, right_idx = self._split_mask(X, feature, threshold)
y_left = y[left_idx]
y_right = y[right_idx]
n_left = len(y_left)
n_right = len(y_right)
n_total = n_left + n_right
return (n_left/n_total)*self.criterion_fn(y_left) \
+ (n_right/n_total)*self.criterion_fn(y_right)
def _best_split(self, X, y):
n_features = X.shape[1]
if self.max_features is None:
max_features = n_features
elif self.max_features == "sqrt":
max_features = int(np.sqrt(n_features))
elif self.max_features == "log2":
max_features = int(np.log2(n_features))
elif self.max_features == "div3":
max_features = int(n_features/3)
elif isinstance(self.max_features, int):
max_features = min(self.max_features, n_features)
else:
raise ValueError("max_features could be: None, 'sqrt', 'log2' or int.")
max_features = max(max_features, 1)
features_idx = np.random.choice(n_features, max_features, replace=False)
best_feature_idx = None
best_threshold = None
min_impurity = float("inf")
for feature_idx in features_idx:
thresholds = self._get_feature_thresholds(X, feature_idx)
for threshold in thresholds:
split_impurity = self._leafs_impurity(X, y, feature_idx, threshold)
if split_impurity < min_impurity:
best_feature_idx = feature_idx
best_threshold = threshold
min_impurity = split_impurity
return best_feature_idx, best_threshold
def _is_finished(self, y, depth):
unique_classes = len(np.unique(y))
n_samples = y.size
if (self.max_depth and (depth >= self.max_depth)) or (n_samples < self.min_samples_split) or (unique_classes == 1):
return True
return False
def _build_tree(self, X, y, depth):
if self._is_finished(y, depth):
if self.regression:
if self.criterion == "mse":
prediction = np.mean(y)
elif self.criterion == "mae":
prediction = np.median(y)
else:
raise ValueError(f"{self.criterion} is not allowed for regression! Please use mse or mae.")
else:
prediction = np.bincount(y, minlength=self.n_classes_)/y.shape[0]
return Node(value=prediction)
feature_idx, threshold = self._best_split(X, y)
left_idx, right_idx = self._split_mask(X, feature_idx, threshold)
left_child = self._build_tree(X[left_idx, :], y[left_idx], depth + 1)
right_child = self._build_tree(X[right_idx, :], y[right_idx], depth + 1)
return Node(feature_idx=feature_idx, threshold=threshold, left=left_child, right=right_child)
def _traverse_tree(self, x, node):
if node.is_leaf():
return node.value
feature_idx = node.feature_idx
threshold = node.threshold
if x[feature_idx] <= threshold:
return self._traverse_tree(x, node.left)
return self._traverse_tree(x, node.right)
def fit(self, X, y):
X, y = self._check_inputs(X, y)
self.n_classes_ = len(np.unique(y))
self.root = self._build_tree(X, y, depth=0)
return self
def predict(self, X):
raise NotImplementedError("Subclasses must implement predict method.")
def print_tree(tree):
def height(root):
if root is None:
return 0
return max(height(root.left), height(root.right))+1
def getcol(h):
if h == 1:
return 1
return getcol(h-1) + getcol(h-1) + 1
def printTree(M, root, col, row, height, type):
if root is None:
return
if root.is_leaf():
M[row][col] = f"{ root.value}"
else:
if type == "left":
M[row][col] = f"X[{root.feature_idx}]<={root.threshold :02.1f}"
else:
M[row][col] = f"X[{root.feature_idx}]> {root.threshold :02.1f}"
printTree(M, root.left, col-pow(2, height-2), row+1, height-1, "left")
printTree(M, root.right, col+pow(2, height-2), row+1, height-1, "right")
h = height(tree.root)
col = getcol(h)
M = [[0 for _ in range(col)] for __ in range(h)]
printTree(M, tree.root, col//2, 0, h, "left")
for i in M:
for j in i:
if j == 0:
print("", end="")
else:
print(j, end=" ")
print("")
class DecisionTreeClassifier(DecisionTree):
def __init__(self, criterion="gini", max_depth=3, max_features=None, min_samples_split=2):
super().__init__(criterion, max_depth, max_features, min_samples_split, regression=False)
def predict(self, X):
probabilities = self.predict_proba(X)
return np.argmax(probabilities, axis=1)
def predict_proba(self, X):
X = self._check_X(X)
predictions = [self._traverse_tree(x, self.root) for x in X]
return np.array(predictions)
class DecisionTreeRegressor(DecisionTree):
def __init__(self, criterion="mse", max_depth=3, max_features=None, min_samples_split=2):
super().__init__(criterion, max_depth, max_features, min_samples_split, regression=True)
def predict(self, X):
X = self._check_X(X)
predictions = [self._traverse_tree(x, self.root) for x in X]
return np.array(predictions)
if __name__ == "__main__":
from sklearn.datasets import make_classification, make_regression
X_train, y_train = make_classification(
n_features=6, n_redundant=0, n_informative=4, random_state=1, n_clusters_per_class=1
)
tree = DecisionTreeClassifier(criterion='gini', max_depth=2)
tree.fit(X_train, y_train)
print("DT Classifier fitted!")
tree.predict(X_train)
# draw_decision_tree(tree.tree)
print_tree(tree)
X_train, y_train = make_regression(
n_features=6, n_informative=4, random_state=1)
tree = DecisionTreeRegressor(criterion='mse', max_depth=2)
tree.fit(X_train, y_train)
print("DT Regressor fitted!")
tree.predict(X_train)
# draw_decision_tree(tree.tree)
print_tree(tree)
from sklearn.datasets import make_classification, make_regression
from sklearn.model_selection import train_test_split
from sklearn import metrics
X, y = make_classification(
n_features=20, n_redundant=2, n_informative=15, random_state=42, n_clusters_per_class=3, class_sep=0.1, n_classes=2
)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
rf = DecisionTreeClassifier(criterion='gini', max_depth=3, min_samples_split=10)
rf.fit(X_train, y_train)
print("=============")
print_tree(rf)
print()
pred = rf.predict(X_test)
pred_prob = rf.predict_proba(X_test)
print("Confusion Matrix:")
print(metrics.confusion_matrix(y_test, pred))
print(f"predictions: {pred[:10]}")
print(f"predictions proba: {pred_prob}")
print("Done")