-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathensemble.py
More file actions
268 lines (164 loc) · 8.47 KB
/
ensemble.py
File metadata and controls
268 lines (164 loc) · 8.47 KB
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
import numpy as np
import matplotlib.pyplot as plt
from numpy import *
from sklearn import datasets
from sklearn.metrics import mean_squared_error
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import Normalizer
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.metrics import roc_auc_score
from sklearn.neural_network import MLPClassifier
import random
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import export_text
from sklearn.neural_network import MLPRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import AdaBoostRegressor
import xgboost as xgb
from sklearn.metrics import roc_curve, auc
def read_data(run_num, prob):
normalise = False
if prob == 'classifification': #Source: Pima-Indian diabetes dataset: https://www.kaggle.com/kumargh/pimaindiansdiabetescsv
data_in = genfromtxt("data/pima.csv", delimiter=",")
data_inputx = data_in[:,0:8] # all features 0, 1, 2, 3, 4, 5, 6, 7
data_inputy = data_in[:,-1] # this is target - so that last col is selected from data
elif prob == 'regression': # energy - regression prob: https://archive.ics.uci.edu/dataset/242/energy+efficiency
data_in = genfromtxt('data/ENB2012_data.csv', delimiter=",")
data_inputx = data_in[:,0:8] # all features 0, - 7
data_inputy = data_in[:,8] # this is target - just the heating load selected from data
if normalise == True:
transformer = Normalizer().fit(data_inputx) # fit does nothing.
data_inputx = transformer.transform(data_inputx)
x_train, x_test, y_train, y_test = train_test_split(data_inputx, data_inputy, test_size=0.40, random_state=run_num)
return x_train, x_test, y_train, y_test
def scipy_models(x_train, x_test, y_train, y_test, type_model, hidden, learn_rate, run_num, problem):
print(run_num, ' is our exp run')
tree_depth = 2
if problem == 'classifification':
if type_model ==0: #SGD
model = MLPClassifier(hidden_layer_sizes=(hidden,), random_state=run_num, max_iter=100,solver='sgd', learning_rate_init=learn_rate )
elif type_model ==1: #https://scikit-learn.org/stable/modules/tree.html (see how tree can be visualised)
model = DecisionTreeClassifier(random_state=0, max_depth=tree_depth)
elif type_model ==2:
model = RandomForestClassifier(n_estimators=100, max_depth=tree_depth, random_state=run_num)
elif type_model ==3:
model = AdaBoostClassifier(n_estimators=100, random_state=run_num)
elif type_model ==4:
model = GradientBoostingClassifier(n_estimators=10, random_state=run_num)
elif problem == 'regression':
if type_model ==0: #SGD
model = MLPRegressor(hidden_layer_sizes=(hidden*3,), random_state=run_num, max_iter=500, solver='adam',learning_rate_init=learn_rate)
elif type_model ==1:
model = DecisionTreeRegressor(random_state=0, max_depth=tree_depth)
elif type_model ==2:
model = RandomForestRegressor(n_estimators=100, max_depth=tree_depth, random_state=run_num)
elif type_model ==3:
model = AdaBoostRegressor(n_estimators=100, random_state=run_num)
elif type_model ==4:
model = GradientBoostingRegressor(n_estimators=10, random_state=run_num)
# Train the model using the training sets
model.fit(x_train, y_train)
if type_model ==1:
r = export_text(model)
print(r)
# Make predictions using the testing set
y_pred_test = model.predict(x_test)
y_pred_train = model.predict(x_train)
if problem == 'regression':
perf_test = np.sqrt(mean_squared_error(y_test, y_pred_test))
perf_train= np.sqrt(mean_squared_error(y_train, y_pred_train))
if problem == 'classifification':
perf_test = accuracy_score(y_pred_test, y_test)
perf_train = accuracy_score(y_pred_train, y_train)
cm = confusion_matrix(y_pred_test, y_test)
#print(cm, 'is confusion matrix')
#auc = roc_auc_score(y_pred, y_test, average=None)
return perf_test #,perf_train
def xgboost_models(x_train, x_test, y_train, y_test, type_model, hidden, learn_rate, run_num, problem):
print(run_num, ' is our exp run')
tree_depth = 2
if problem == 'classifification':
if type_model ==0:
model = xgb.XGBClassifier(colsample_bytree = 0.3, learning_rate = 0.1,
max_depth = 5, alpha = 5, n_estimators = 100)
elif problem == 'regression':
if type_model ==0: #SGD
model = xgb.XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1,
max_depth = 5, alpha = 5, n_estimators = 100)
# Train the model using the training sets
model.fit(x_train, y_train)
if type_model ==1:
r = export_text(model)
print(r)
# Make predictions using the testing set
y_pred_test = model.predict(x_test)
y_pred_train = model.predict(x_train)
if problem == 'regression':
perf_test = np.sqrt(mean_squared_error(y_test, y_pred_test))
perf_train= np.sqrt(mean_squared_error(y_train, y_pred_train))
if problem == 'classifification':
perf_test = accuracy_score(y_pred_test, y_test)
perf_train = accuracy_score(y_pred_train, y_train)
cm = confusion_matrix(y_pred_test, y_test)
#print(cm, 'is confusion matrix')
#auc = roc_auc_score(y_pred, y_test, average=None)
return perf_test #,perf_train
def main():
max_expruns = 5
SGD_all = np.zeros(max_expruns)
forest_all = np.zeros(max_expruns)
tree_all = np.zeros(max_expruns)
adaboost_all = np.zeros(max_expruns)
xg_all = np.zeros(max_expruns)
gb_all = np.zeros(max_expruns)
learn_rate = 0.01
hidden = 8
prob = 'classifification' # classification or regression
#prob = 'regression' # classification or regression
# classifcation accurary is reported for classification and RMSE for regression
print(prob, ' is our problem')
for run_num in range(0,max_expruns):
x_train, x_test, y_train, y_test = read_data(run_num, prob)
acc_sgd = scipy_models(x_train, x_test, y_train, y_test, 0, hidden, learn_rate, run_num, prob) #SGD
acc_tree = scipy_models(x_train, x_test, y_train, y_test, 1, hidden, learn_rate, run_num, prob) #Decision Tree
acc_forest = scipy_models(x_train, x_test, y_train, y_test, 2, hidden, learn_rate, run_num, prob) #Random Forests
acc_adaboost = scipy_models(x_train, x_test, y_train, y_test, 3, hidden, learn_rate, run_num, prob) #adaboost
acc_gb = scipy_models(x_train, x_test, y_train, y_test, 4, hidden, learn_rate, run_num, prob) #gboost
acc_xg = xgboost_models(x_train, x_test, y_train, y_test, 0, hidden, learn_rate, run_num, prob) #adaboost
SGD_all[run_num] = acc_sgd
tree_all[run_num] = acc_tree
forest_all[run_num] = acc_forest
adaboost_all[run_num] = acc_adaboost
gb_all[run_num] = acc_gb
xg_all[run_num] = acc_xg
print(SGD_all,' nn_all')
print(np.mean(SGD_all), ' mean nn_all')
print(np.std(SGD_all), ' std nn_all')
print(tree_all, ' tree_all')
print(np.mean(tree_all), ' tree _all')
print(np.std(tree_all), ' tree _all')
print(forest_all, hidden,' forest_all')
print(np.mean(forest_all), ' forest _all')
print(np.std(forest_all), ' forest _all')
print(adaboost_all, 'adaboost_all')
print(np.mean(adaboost_all), ' adaboost _all')
print(np.std(adaboost_all), ' adaboost_all')
print(gb_all, 'gb_all')
print(np.mean(gb_all), ' gb _all')
print(np.std(gb_all), ' gb_all')
print(xg_all, 'xg_all')
print(np.mean(xg_all), ' xg _all')
print(np.std(xg_all), ' xg_all')
if __name__ == '__main__':
main()