-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance_tests3_ForestPrune.py
166 lines (132 loc) · 5.57 KB
/
performance_tests3_ForestPrune.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
from imodels.util.data_util import get_clean_dataset
import numpy as np
from treesmoothing import ShrinkageClassifier
from sklearn.model_selection import cross_val_score
import matplotlib.pyplot as plt
from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import balanced_accuracy_score
from sklearn.metrics import roc_auc_score
import sys
from ForestPrune import ForestPrune
clf_datasets = [
("heart", "heart", "imodels"),
("breast-cancer", "breast_cancer", "imodels"),
("haberman", "haberman", "imodels"),
("ionosphere", "ionosphere", "pmlb"),
("diabetes-clf", "diabetes", "pmlb"),
("german", "german", "pmlb"),
("juvenile", "juvenile_clean", "imodels"),
("recidivism", "compas_two_year_clean", "imodels")
]
clf_datasets = [
("heart", "heart", "imodels"),
("breast-cancer", "breast_cancer", "imodels"),
("haberman", "haberman", "imodels"),
("ionosphere", "ionosphere", "pmlb"),
("diabetes-clf", "diabetes", "pmlb"),
("german", "german", "pmlb")
]
clf_datasets = [
("haberman", "haberman", "imodels")
]
clf_datasets = [
("breast-cancer", "breast_cancer", "imodels")
]
clf_datasets = [
("heart", "heart", "imodels")
]
clf_datasets = [
("diabetes-clf", "diabetes", "pmlb")
]
####
clf_datasets = [
("breast-cancer", "breast_cancer", "imodels")
]
# scoring
#sc = "balanced_accuracy"
sc = "roc_auc"
#ntrees = 100
for ntrees in [5, 10, 50, 100]:
iterations = np.arange(0, 50, 1)
for ds_name, id, source in clf_datasets:
X, y, feature_names = get_clean_dataset(id, data_source=source)
scores = {}
print(ds_name)
scores["vanilla"] = []
scores["hs"] = []
scores["beta"] = []
scores["ForestPrune"] = []
for xx in iterations:
# train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)
# vanilla
print("Vanilla Mode")
shrink_mode="vanilla"
#scores[shrink_mode] = []
clf = RandomForestClassifier(n_estimators=ntrees)
clf.fit(X_train, y_train)
if sc == "balanced_accuracy":
pred_vanilla = clf.predict(X_test)
scores[shrink_mode].append(balanced_accuracy_score(y_test, pred_vanilla))
if sc == "roc_auc":
pred_vanilla = clf.predict_proba(X_test)[:,1]
scores[shrink_mode].append(roc_auc_score(y_test, pred_vanilla))
# hs
print("HS Mode")
shrink_mode="hs"
#scores[shrink_mode] = []
param_grid = {
"lmb": [0.001, 0.01, 0.1, 1, 10, 25, 50, 100, 200],
"shrink_mode": ["hs"]}
grid_search = GridSearchCV(ShrinkageClassifier(RandomForestClassifier(n_estimators=ntrees)), param_grid, cv=5, n_jobs=-1, scoring=sc)
grid_search.fit(X_train, y_train)
best_params = grid_search.best_params_
print(best_params)
clf = ShrinkageClassifier(RandomForestClassifier(n_estimators=ntrees),shrink_mode=shrink_mode, lmb=best_params.get('lmb'))
#print(clf)
clf.fit(X_train, y_train)
if sc == "balanced_accuracy":
pred_hs = clf.predict(X_test)
scores[shrink_mode].append(balanced_accuracy_score(y_test, pred_hs))
if sc == "roc_auc":
pred_hs = clf.predict_proba(X_test)[:,1]
scores[shrink_mode].append(roc_auc_score(y_test, pred_hs))
# beta
print("Beta Shrinkage")
shrink_mode="beta"
#scores[shrink_mode] = []
param_grid = {
"alpha": [8000, 5000, 2000, 1000, 500, 100, 50, 30, 10, 1],
"beta": [8000, 5000, 2000, 1000, 500, 100, 50, 30, 10, 1],
"shrink_mode": ["beta"]}
grid_search = GridSearchCV(ShrinkageClassifier(RandomForestClassifier(n_estimators=ntrees)), param_grid, cv=5, n_jobs=-1, scoring=sc)
grid_search.fit(X_train, y_train)
best_params = grid_search.best_params_
print(best_params)
clf = ShrinkageClassifier(RandomForestClassifier(n_estimators=ntrees),shrink_mode=shrink_mode, alpha=best_params.get('alpha'), beta=best_params.get('beta'))
clf.fit(X_train, y_train)
if sc == "balanced_accuracy":
pred_beta = clf.predict(X_test)
scores[shrink_mode].append(balanced_accuracy_score(y_test, pred_beta))
if sc == "roc_auc":
pred_beta = clf.predict_proba(X_test)[:,1]
scores[shrink_mode].append(roc_auc_score(y_test, pred_beta))
#ForestPrune
print("ForestPrune")
shrink_mode="ForestPrune"
res = ForestPrune(X_train, y_train, X_test, y_test, ntrees)
#print(res)
if res == False:
scores[shrink_mode].append(np.NAN)
continue
if sc == "balanced_accuracy":
scores[shrink_mode].append(balanced_accuracy_score(y_test, res[0]))
if sc == "roc_auc":
scores[shrink_mode].append(roc_auc_score(y_test, res[1]))
print(scores)
RES = np.vstack([scores['vanilla'],scores['hs'],scores['beta'], scores['ForestPrune']])
print(RES)
np.savetxt(str(ntrees),RES, delimiter='\t')