-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecision_trees.py
284 lines (220 loc) · 9.33 KB
/
Decision_trees.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
282
283
284
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 13 19:09:48 2021
@author: Rajeev
"""
from sklearn.datasets import load_iris
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
np.random.seed(112)
iris = load_iris()
X, y = iris.data, iris.target
# Split the data into a training and test set
X_train, X_test, y_train, y_test = train_test_split(X, y)
print(f'Shape X_train: {X_train.shape}')
print(f'Shape y_train: {y_train.shape}')
print(f'Shape X_test: {X_test.shape}')
print(f'Shape y_test: {y_test.shape}')
class DecisionTree:
"""
Decision tree for classification
"""
def __init__(self):
self.root_dict = None
self.tree_dict = None
def split_dataset(self, X, y, feature_idx, threshold):
"""
Splits dataset X into two subsets, according to a given feature
and feature threshold.
Args:
X: 2D numpy array with data samples
y: 1D numpy array with labels
feature_idx: int, index of feature used for splitting the data
threshold: float, threshold used for splitting the data
Returns:
splits: dict containing the left and right subsets
and their labels
"""
left_idx = np.where(X[:, feature_idx] < threshold)
right_idx = np.where(X[:, feature_idx] >= threshold)
left_subset = X[left_idx]
y_left = y[left_idx]
right_subset = X[right_idx]
y_right = y[right_idx]
splits = {
'left': left_subset,
'y_left': y_left,
'right': right_subset,
'y_right': y_right,
}
return splits
def gini_impurity(self, y_left, y_right, n_left, n_right):
"""
Computes Gini impurity of a split.
Args:
y_left, y_right: target values of samples in left/right subset
n_left, n_right: number of samples in left/right subset
Returns:
gini_left: float, Gini impurity of left subset
gini_right: gloat, Gini impurity of right subset
"""
n_total = n_left + n_left
score_left, score_right = 0, 0
gini_left, gini_right = 0, 0
if n_left != 0:
for c in range(self.n_classes):
# For each class c, compute fraction of samples with class c
p_left = len(np.where(y_left == c)[0]) / n_left
score_left += p_left * p_left
gini_left = 1 - score_left
if n_right != 0:
for c in range(self.n_classes):
p_right = len(np.where(y_right == c)[0]) / n_right
score_right += p_right * p_right
gini_right = 1 - score_right
return gini_left, gini_right
def get_cost(self, splits):
"""
Computes cost of a split given the Gini impurity of
the left and right subset and the sizes of the subsets
Args:
splits: dict, containing params of current split
"""
y_left = splits['y_left']
y_right = splits['y_right']
n_left = len(y_left)
n_right = len(y_right)
n_total = n_left + n_right
gini_left, gini_right = self.gini_impurity(y_left, y_right, n_left, n_right)
cost = (n_left / n_total) * gini_left + (n_right / n_total) * gini_right
return cost
def find_best_split(self, X, y):
"""
Finds the best feature and feature index to split dataset X into
two groups. Checks every value of every attribute as a candidate
split.
Args:
X: 2D numpy array with data samples
y: 1D numpy array with labels
Returns:
best_split_params: dict, containing parameters of the best split
"""
n_samples, n_features = X.shape
best_feature_idx, best_threshold, best_cost, best_splits = np.inf, np.inf, np.inf, None
for feature_idx in range(n_features):
for i in range(n_samples):
current_sample = X[i]
threshold = current_sample[feature_idx]
splits = self.split_dataset(X, y, feature_idx, threshold)
cost = self.get_cost(splits)
if cost < best_cost:
best_feature_idx = feature_idx
best_threshold = threshold
best_cost = cost
best_splits = splits
best_split_params = {
'feature_idx': best_feature_idx,
'threshold': best_threshold,
'cost': best_cost,
'left': best_splits['left'],
'y_left': best_splits['y_left'],
'right': best_splits['right'],
'y_right': best_splits['y_right'],
}
return best_split_params
def build_tree(self, node_dict, depth, max_depth, min_samples):
"""
Builds the decision tree in a recursive fashion.
Args:
node_dict: dict, representing the current node
depth: int, depth of current node in the tree
max_depth: int, maximum allowed tree depth
min_samples: int, minimum number of samples needed to split a node further
Returns:
node_dict: dict, representing the full subtree originating from current node
"""
left_samples = node_dict['left']
right_samples = node_dict['right']
y_left_samples = node_dict['y_left']
y_right_samples = node_dict['y_right']
if len(y_left_samples) == 0 or len(y_right_samples) == 0:
node_dict["left_child"] = node_dict["right_child"] = self.create_terminal_node(np.append(y_left_samples, y_right_samples))
return None
if depth >= max_depth:
node_dict["left_child"] = self.create_terminal_node(y_left_samples)
node_dict["right_child"] = self.create_terminal_node(y_right_samples)
return None
if len(right_samples) < min_samples:
node_dict["right_child"] = self.create_terminal_node(y_right_samples)
else:
node_dict["right_child"] = self.find_best_split(right_samples, y_right_samples)
self.build_tree(node_dict["right_child"], depth+1, max_depth, min_samples)
if len(left_samples) < min_samples:
node_dict["left_child"] = self.create_terminal_node(y_left_samples)
else:
node_dict["left_child"] = self.find_best_split(left_samples, y_left_samples)
self.build_tree(node_dict["left_child"], depth+1, max_depth, min_samples)
return node_dict
def create_terminal_node(self, y):
"""
Creates a terminal node.
Given a set of labels the most common label is computed and
set as the classification value of the node.
Args:
y: 1D numpy array with labels
Returns:
classification: int, predicted class
"""
classification = max(set(y), key=list(y).count)
return classification
def train(self, X, y, max_depth, min_samples):
"""
Fits decision tree on a given dataset.
Args:
X: 2D numpy array with data samples
y: 1D numpy array with labels
max_depth: int, maximum allowed tree depth
min_samples: int, minimum number of samples needed to split a node further
"""
self.n_classes = len(set(y))
self.root_dict = self.find_best_split(X, y)
self.tree_dict = self.build_tree(self.root_dict, 1, max_depth, min_samples)
def predict(self, X, node):
"""
Predicts the class for a given input example X.
Args:
X: 1D numpy array, input example
node: dict, representing trained decision tree
Returns:
prediction: int, predicted class
"""
feature_idx = node['feature_idx']
threshold = node['threshold']
if X[feature_idx] < threshold:
if isinstance(node['left_child'], (int, np.integer)):
return node['left_child']
else:
prediction = self.predict(X, node['left_child'])
elif X[feature_idx] >= threshold:
if isinstance(node['right_child'], (int, np.integer)):
return node['right_child']
else:
prediction = self.predict(X, node['right_child'])
return prediction
tree = DecisionTree()
tree.train(X_train, y_train, max_depth=2, min_samples=1)
def print_tree(node, depth=0):
if isinstance(node, (int, np.integer)):
print(f"{depth * ' '}predicted class: {iris.target_names[node]}")
else:
print(f"{depth * ' '}{iris.feature_names[node['feature_idx']]} < {node['threshold']}, "
f"cost of split: {round(node['cost'], 3)}")
print_tree(node["left_child"], depth+1)
print_tree(node["right_child"], depth+1)
print_tree(tree.tree_dict)
all_predictions = []
for i in range(X_test.shape[0]):
result = tree.predict(X_test[i], tree.tree_dict)
all_predictions.append(y_test[i] == result)
print(f"Accuracy on test set: {sum(all_predictions) / len(all_predictions)}")