-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecisionTreeClassifier.py
281 lines (243 loc) · 8.82 KB
/
DecisionTreeClassifier.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
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
import numpy as np
from collections import Counter
from typing import Union, List
class Node:
def __init__(self, features: int=None, threshold: int=None, left=None, right=None, value=None) -> None:
"""
Initialize the node of tree.
Parameters
----------
features : int
The feature index in node.
threshold : int
The threshold in this node.
left : Root left
The left Node conencted.
right :Root right
The right Node connected.
value : int or float
The value of Node.
"""
self.features = features
self.threshold = threshold
self.left = left
self.right = right
self.value = value
def is_leaf_node(self):
"""
Check if the present location is the leaf of tree
"""
return self.value is not None
class DecisionTreeClassifier:
def __init__(self, min_samples_split: Union[int, float]=2, max_depth: int=100, n_features: Union[int, float]=None) -> None:
"""
Initialize the condition needed of decision tree
"""
self.min_samples_split = min_samples_split
self.max_depth = max_depth
self.n_features = n_features
self.root = None
def fit(self, X: Union[np.ndarray, List[List]], y: Union[np.ndarray, List]) -> None:
"""
Create the binary decision tree form from input X and target y.
Parameters
----------
X : array-like of shape (n_samples, n_features)
Training vector, where `n_samples` and `n_features` is number of sample(s) of feature(s).
y : array-like of shape (n_features)
Target vector (labels).
Attributes
----------
root : Node
The full tree from input and target.
"""
X, y = np.array(X), np.array(y)
assert np.ndim(X)==2, Exception("ndim of X must be 2")
assert np.ndim(y)==1, Exception("ndim of y must be 1")
if self.n_features:
self.n_features = min(X.shape[1], self.n_features)
else:
self.n_features = X.shape[1]
self.root = self.__grow_tree(X, y)
def __grow_tree(self, X: np.ndarray, y: np.ndarray, depth: int=0):
"""
Parameters
----------
X : array-like of shape (n_samples, n_features)
Training vector.
y : array-like of shape (n_features)
Target vector (labels).
depth : int
The present heigh of node according to tree (or the deeply node location in tree).
Attributes
----------
n_labels : array-like of shape (n_labels)
The labels tag, where `n_labels` is unique of target vector y.
feature_idxs : array-like of shape (n_features of node, n_features)
The index of labels in node.
best_feature : np.ndarray
Optimzize feature index split.
best_threshold : np.ndarray
Optimzize threshold split.
left_idxs : np.ndarray
The index of node belower the threshold.
right_idx : np.ndarray
The index of node higher the threshold.
left : Node left.
right : Node right.
"""
n_samples, n_features = X.shape
n_labels = len(np.unique(y))
if (depth >= self.max_depth or n_labels == 1 or n_samples < self.min_samples_split):
leaf_node = self.__most_common_label(y)
return Node(value=leaf_node)
feature_idxs = np.random.choice(n_features, self.n_features, replace=False)
best_feature, best_threshold = self.__best_split(X, y, feature_idxs)
left_idxs, right_idxs = self.__split(X[:, best_feature], best_threshold)
left = self.__grow_tree(X[left_idxs, :], y[left_idxs], depth+1)
right = self.__grow_tree(X[right_idxs, :], y[right_idxs], depth+1)
return Node(best_feature, best_threshold, left, right)
def __best_split(self, X: np.ndarray, y: np.ndarray, feature_idxs: np.ndarray):
"""
Compare the gain to get opimize value
Parameters
----------
X : np.ndarray
The training vector of each node.
y : np.ndarray
The target vector of each node.
feature_idxs : np.ndarray
Index of features in node.
Attributes
----------
best_gain : int or float
The temp value to check the highest optimize
Return
------
split_idx : int or float
The split index when gain is highest
split_threshold
The split threshold when gain is highest
"""
best_gain = -1
split_idx, split_threshold = None, None
for feature_idx in feature_idxs:
X_column = X[:, feature_idx]
thresholds = np.unique(X_column)
for threshold in thresholds:
gain = self.__information_gain(y, X_column, threshold)
if gain > best_gain:
best_gain = gain
split_idx = feature_idx
split_threshold = threshold
return split_idx, split_threshold
def __information_gain(self, y: np.ndarray, X_column: np.ndarray, threshold: Union[int, float]):
"""
Calculate the information gain of decision tree to get value of two condition
Parameters
----------
y : np.ndarray
Target vector.
X_column : np.ndarray
Comparing vector.
threshold : int or float
Threshold to divide left and right indexes
Attributes
----------
n_l : int
len of samples in left side
n_r : int
len of samples in right side
e_l : float
entropy of left side
e_r : float
entropy of right side
Return
------
information_gain : float
gnini score
"""
parent_entropy = self.__entropy(y)
left_idxs, right_idxs = self.__split(X_column, threshold)
if len(left_idxs)==0 or len(right_idxs)==0:
return 0
n = len(y)
n_l, n_r = len(left_idxs), len(right_idxs)
e_l, e_r = self.__entropy(y[left_idxs]), self.__entropy(y[right_idxs])
children_entropy = (n_l/n)*e_l + (n_r/n)*e_r
return parent_entropy - children_entropy
def __split(self, X_column: np.ndarray, threshold: Union[int, float]):
"""
Split the left side and right side from threshold
Parameters
----------
X_column : np.ndarray
The samples
threshold : int or float
Threshold to devide left or right
Return
------
left_idxs : np.ndarray
The index of samples in left side (below or equal than threshold)
right_idxs : np.ndarray
The index of samples in right side (high than therhold)
"""
left_idxs = np.argwhere(X_column <= threshold).flatten()
right_idxs = np.argwhere(X_column > threshold).flatten()
return left_idxs, right_idxs
def __entropy(self, y: np.ndarray):
"""
Calculate the entropy
Parameters
----------
y : np.ndarray
The target want to know the apperent
Attributes
hist : np.ndarray
The phrequency of each value in y
ps : np.ndarray
p(x)
"""
hist = np.bincount(y)
ps = hist / len(y)
return -np.sum([p*np.log(p) for p in ps if p>0])
def __most_common_label(self, y: np.ndarray):
"""
Take the best phrequency of label
Parameters
----------
y : np.ndarray
The target
Return
------
return the value of best phrequency
"""
counter = Counter(y)
return counter.most_common(1)[0][0]
def predict(self, X_test: Union[np.ndarray, List[List]]):
"""
Predict class or regression value for X_test.
Parameters
----------
X_test : np.ndarray
vector of testign
Return
------
Labels of testing through model
"""
X_test = np.array(X_test)
assert np.ndim(X_test)==2, Exception("ndim of X_test must be 2")
return np.array([self.__traversal(x, self.root) for x in X_test])
def __traversal(self, x: np.ndarray, node):
"""
Traversal each node to find suitable position for X_train
Parameters
----------
x : int or float
Each value of X_test
"""
if node.is_leaf_node():
return node.value
if x[node.features] <= node.threshold:
return self.__traversal(x, node.left)
return self.__traversal(x, node.right)