-
Notifications
You must be signed in to change notification settings - Fork 2
/
decision_tree - bagging:boosting.py
316 lines (253 loc) · 11 KB
/
decision_tree - bagging:boosting.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# implements adaboost and bagging classifier using decision tree as base estimator
import numpy as np
import os
import math
import random
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.ensemble import BaggingClassifier, AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
def entropy(y, wts=None):
counter = {}
for idx, i in enumerate(y):
if i in counter:
counter[i] += wts[idx]*1
else:
counter[i] = wts[idx]*1
entr = 0
for k,v in counter.items():
entr += -(v/sum(wts)) * math.log(v/sum(wts), 2)
return entr
def id3(x, y, attribute_value_pairs=None, depth=0, max_depth=5, weights=[]):
"""
creates a decision tree in dictionary format -
{(3, 2, False):
{(0, 1, False):
{(4, 2, True): 1,
(4, 2, False): 0},
(0, 1, True):
{(2, 1, True): 0,
(2, 1, False): 1}},
(3, 2, True): 1}
"""
# initialize default weights
if len(weights) == 0:
weights = np.ones(len(x)) / len(x)
# initialize attribute-value pairs
if attribute_value_pairs == None:
# generate all combinations of (column, value)
aggr = {}
# initialize empty list for each index
for idx, col in enumerate(x[0]):
aggr[idx] = set()
for row in x:
for idx, col in enumerate(row):
aggr[idx].add(col)
attribute_value_pairs = []
for k,v in aggr.items():
for vi in v:
attribute_value_pairs.append((k, vi))
# if all elements of list are the same, a set formed from the list will be of length 1
if len(set(y)) <= 1:
return y[0]
# if max depth reached or no further values to split on, return majority element
if len(attribute_value_pairs) == 0 or depth == max_depth:
# store a weighted counter for all unique elements
counter = {}
for idx, label in enumerate(y):
if label not in counter:
counter[label] = weights[idx]*1
else:
counter[label] += weights[idx]*1
# save the label with max weight
maj_ele = 0
max_val = 0
for k,v in counter.items():
if v > max_val:
maj_ele, max_val = k, v
return maj_ele
max_attr = None
max_info_gain = 0
cur_entropy = entropy(y, weights)
# for each possible column/value pair, split that column into 1s and 0s based on if it is equal to the value
# save attribute which gives max possible information gain
for attr in attribute_value_pairs:
column_index = attr[0]
value_to_split_on = attr[1]
new_column = [int(val == value_to_split_on) for val in x[:, column_index]]
# calculate mutual information if we choose this column to split on with this value
new_label_split_true = []
new_label_split_true_weights = []
new_label_split_false = []
new_label_split_false_weights = []
before_entropy = entropy(y, weights)
for idx, row in enumerate(new_column):
if row == 1:
new_label_split_true.append(y[idx])
new_label_split_true_weights.append(weights[idx])
else:
new_label_split_false.append(y[idx])
new_label_split_false_weights.append(weights[idx])
possible_entropy = (sum(new_label_split_true_weights)/sum(weights)) * entropy(new_label_split_true, new_label_split_true_weights) + \
(sum(new_label_split_false_weights)/sum(weights)) * entropy(new_label_split_false, new_label_split_false_weights)
mutual_info = abs(before_entropy - possible_entropy)
if (mutual_info > max_info_gain):
max_info_gain, max_attr = mutual_info, attr
# remove the selected next max attribute-value pair from the list of pairs
new_attribute_value_pairs = attribute_value_pairs.copy()
new_attribute_value_pairs.remove(max_attr)
# separate previous dataset into two datasets, based on rows which satisfy attr
x_true_elements = []
x_false_elements = []
y_true_elements = []
y_false_elements = []
for idx, val in enumerate(x):
if val[max_attr[0]] == max_attr[1]:
x_true_elements.append(val)
y_true_elements.append(y[idx])
else:
x_false_elements.append(val)
y_false_elements.append(y[idx])
x_true_elements = np.asarray(x_true_elements)
x_false_elements = np.asarray(x_false_elements)
# set the key as specified in comments above and value as recursive call to id3
max_attr_true = (max_attr[0], max_attr[1], True)
max_attr_false = (max_attr[0], max_attr[1], False)
tree = {}
tree[max_attr_true] = id3(x_true_elements, y_true_elements, new_attribute_value_pairs.copy(), depth+1, max_depth)
tree[max_attr_false] = id3(x_false_elements, y_false_elements, new_attribute_value_pairs.copy(), depth+1, max_depth)
return tree
def predict_item(x, tree):
# check if leaf label reached
if type(tree) is not dict:
return tree
for key in tree.keys():
true_option = tree[(key[0], key[1], True)]
false_option = tree[(key[0], key[1], False)]
if x[key[0]] == key[1]:
return predict_item(x, true_option)
else:
return predict_item(x, false_option)
def print_tree(tree, depth=0):
if type(tree) is not dict:
print(depth*"\t" + str(tree))
return
for idx, key in enumerate(tree):
print(depth*"\t" + "data[" + str(key[0]) + "] == " + str(key[1]) + "? " + str(key[2]))
print_tree(tree[key], depth+1)
def bagging(x, y, max_depth, num_trees):
trees_ensemble = []
for i in range(num_trees):
# randomly sample with replacement
sample_indexes = np.random.choice(np.arange(len(x)), len(x), replace=True)
xsample = x[sample_indexes]
ysample = y[sample_indexes]
dt = id3(xsample, ysample, max_depth=max_depth)
trees_ensemble.append(dt)
return trees_ensemble
def adaboost(Xtrn, ytrn, max_depth, num_stumps):
ensemble = []
# init weights to 1/N each
weights = np.ones(len(Xtrn)) / len(Xtrn)
for i in range(num_stumps):
dtree = id3(Xtrn, ytrn, max_depth=max_depth, weights=weights)
# predict using the newly learnt stump
y_pred = [predict_item(X, dtree) for X in Xtrn]
# calculate error
err = 0
for idx, predicted_item in enumerate(y_pred):
if predicted_item != ytrn[idx]:
err += weights[idx]
err /= sum(weights)
# calculate alpha
alpha = 0.5 * np.log((1 - err) / err)
# save the hypothesis stump along with alpha weight
ensemble.append((dtree, alpha))
# recalculate weights
new_weights = []
for idx, weight in enumerate(weights):
if y_pred[idx] == ytrn[idx]:
new_weights.append(weight * np.exp(-alpha))
else:
new_weights.append(weight * np.exp(alpha))
# normalize weights
newsum = weights / (2 * np.sqrt((1 - err) * err))
new_weights = new_weights / sum(newsum)
weights = new_weights
return ensemble
def predict_example(x, h_ens):
predictions = []
# for each testing example
for item in x:
# keep count of the weighted number of times each label is predicted
options = {}
for tree in h_ens:
pred_label = predict_item(item, tree[0])
if pred_label not in options:
options[pred_label] = 0
options[pred_label] += 1*tree[1] # multiply by weight of this ensemble
# save the label with max weight
selected_label = 0
max_val = 0
for k,v in options.items():
if v > max_val:
selected_label, max_val = k, v
predictions.append(selected_label)
return predictions
if __name__ == "__main__":
# load training data
dataset1 = np.genfromtxt('./mushroom.train', missing_values=0, delimiter=',', dtype=int)
ytrn = dataset1[:, 0] # select prediction column
Xtrn = dataset1[:, 1:] # select all other columns
dataset2 = np.genfromtxt('./mushroom.test', missing_values=0, delimiter=',', dtype=int)
ytst = dataset2[:, 0] # select prediction column
Xtst = dataset2[:, 1:] # select all other columns
# BAGGING
print("BAGGING:")
for depth in [3, 5]:
for tree in [5, 10]:
print("\nLearning ensemble for depth = " + str(depth) + " and k = " + str(tree) + "")
ensemble = bagging(Xtrn, ytrn, max_depth=depth, num_trees=tree)
y_pred = predict_example(Xtst, [(e, 1) for e in ensemble])
# compute testing error
tst_err = sum(ytst != y_pred) / len(ytst)
print("Accuracy: " + str(100 - tst_err*100) + "%")
print("Confusion matrix: ")
print(confusion_matrix(ytst, y_pred))
# BOOSTING
print("\nBOOSTING:")
for depth in [1, 2]:
for stump in [5, 10]:
print("\nLearning ensemble for depth = " + str(depth) + " and k = " + str(stump) + "...")
ensemble = adaboost(Xtrn, ytrn, max_depth=depth, num_stumps=stump)
y_pred = predict_example(Xtst, ensemble)
# compute testing error
tst_err = sum(ytst != y_pred) / len(ytst)
print("Accuracy: " + str(100 - tst_err*100) + "%")
print(confusion_matrix(ytst, y_pred))
# BAGGING using scikit-learn
print("\nBAGGING using scikit-learn:")
for depth in [3, 5]:
for tree in [5, 10]:
print("\nLearning ensemble for depth = " + str(depth) + " and k = " + str(tree) + "")
y_pred = BaggingClassifier(base_estimator=DecisionTreeClassifier(criterion='entropy', max_depth=depth), n_estimators=tree).fit(Xtrn, ytrn).predict(Xtst)
#, max_depth=depth, num_trees=tree)
# y_pred = predict_example(Xtst, [(e, 1) for e in ensemble])
# compute testing error
tst_err = sum(ytst != y_pred) / len(ytst)
print("Accuracy: " + str(100 - tst_err*100) + "%")
print("Confusion matrix: ")
print(confusion_matrix(ytst, y_pred))
# BOOSTING using scikit-learn
print("\nBOOSTING using scikit-learn:")
for depth in [1, 2]:
for tree in [5, 10]:
print("\nLearning ensemble for depth = " + str(depth) + " and k = " + str(tree) + "")
y_pred = AdaBoostClassifier(base_estimator=DecisionTreeClassifier(criterion='entropy', max_depth=depth), n_estimators=tree).fit(Xtrn, ytrn).predict(Xtst)
#, max_depth=depth, num_trees=tree)
# y_pred = predict_example(Xtst, [(e, 1) for e in ensemble])
# compute testing error
tst_err = sum(ytst != y_pred) / len(ytst)
print("Accuracy: " + str(100 - tst_err*100) + "%")
print("Confusion matrix: ")
print(confusion_matrix(ytst, y_pred))