-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboosting_test.py
52 lines (46 loc) · 1.19 KB
/
boosting_test.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
import numpy as np
from sklearn.metrics import accuracy_score
import json
import data_loader
import decision_stump
import boosting
# load data
X_train, X_test, y_train, y_test = data_loader.binary_iris_dataset()
assert(len(X_train) == 80)
assert(len(X_train[0]) == 4)
assert(len(X_test) == 20)
assert(len(X_test[0]) == 4)
assert(len(y_train) == 80)
assert(len(y_test) == 20)
# set classifiers
h_set = set()
s_set = {1, -1}
b_set = set(np.linspace(0, 10, 51))
d_set = {0, 1, 2, 3}
for s in s_set:
for b in b_set:
for d in d_set:
h_set.add(decision_stump.DecisionStump(s,b,d))
# training
Adas = []
Logs = []
for idx, T in enumerate([10, 20, 30]):
Adas.append(boosting.AdaBoost(h_set, T=T))
Adas[idx].train(X_train, y_train)
Logs.append(boosting.LogitBoost(h_set, T=T))
Logs[idx].train(X_train, y_train)
# testing
Ada_preds = []
Log_preds = []
Ada_accus = []
Log_accus = []
for Ada in Adas:
pred = Ada.predict(X_test)
Ada_preds.append(pred)
Ada_accus.append(accuracy_score(pred, y_test))
for Log in Logs:
pred = Log.predict(X_test)
Log_preds.append(pred)
Log_accus.append(accuracy_score(pred, y_test))
print('AdaBoost testing accuracies:', Ada_accus)
print('LogitBoost testing accuracies:', Log_accus)