-
Notifications
You must be signed in to change notification settings - Fork 5
/
algo_comparison.py
253 lines (193 loc) · 7.07 KB
/
algo_comparison.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
'''
Babayan, Orton & Streicker
Predicting Reservoir Hosts and Arthropod Vectors from Evolutionary Signatures in RNA Virus Genomes
-- Algorithm comparison
'''
# COMPARISON BETWEEN ALGORITHMS USING DEFAULT SETTINGS
#%%
# modules and functions
import pandas as pd
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.model_selection import cross_val_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
import xgboost as xgb
from xgboost import XGBClassifier
import h2o
from h2o.estimators.gbm import H2OGradientBoostingEstimator
h2o.init() # load H2O server
# h2o.cluster().shutdown() # uncomment if h2o clusters already running
# fix random seed for consistency between runs and algorithms
seed = 4
#############################################################################
# PREDICTING RESERVOIR TAXON
#############################################################################
# Load datasets from disk #
#%%
df_reservoirs = pd.read_csv("<filename of the appropriate database>")
df_h2o_reservoirs = h2o.import_file("<filename of the appropriate database>")
#%%
# cross-validation settings
validation_size = 0.3 # use 30% of the dataset for testing amd 70% for trainging
num_splits = 10 # repeat splitting 10 times
scoring = "accuracy" # a simple metric is sufficient
sss = StratifiedShuffleSplit(n_splits=num_splits, test_size=validation_size, random_state=seed) # take random splits preserving proportions of each class
# models for comparison
models = []
models.append(("KNN", KNeighborsClassifier()))
models.append(("LR", LogisticRegression(random_state=seed)))
models.append(("SGD", SGDClassifier(random_state=seed)))
models.append(("NB", GaussianNB()))
models.append(("SVM", SVC(random_state=seed)))
models.append(("RF", RandomForestClassifier(random_state=seed)))
models.append(("XGB", XGBClassifier(random_state=seed)))
h2o_spot = H2OGradientBoostingEstimator(
nfolds=num_splits)
# label data
X = df_reservoirs
y = df_reservoirs.index
## for H2O models
X_h2o = df_h2o_reservoirs.columns
del X_h2o[0]
y_h2o = "class"
train, test = df_h2o_reservoirs.split_frame(ratios=[1-validation_size], seed=seed)
h2o_spot.train(x=X_h2o,
y=y_h2o,
training_frame=train,
validation_frame=test)
h2o_cv_results = h2o_spot.cross_validation_metrics_summary(
).as_data_frame().iloc[0, 3:].values.astype("float64")
# prepare placeholders for results
results = []
names = []
# for Scikit models
for name, model in models:
cv_results = cross_val_score(model, X, y, cv=sss, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "Cross val score for {0}: {1:.2%} ± {2:.2%}".format(
name, cv_results.mean(), cv_results.std())
print(msg)
# append H2O info
name = "H2O_GBM"
names.append(name)
results.append(h2o_cv_results)
msg = "Cross val score for {0}: {1:.2%} ± {2:.2%}".format(
name, h2o_cv_results.mean(), h2o_cv_results.std())
print(msg)
#############################################################################
# PREDICTING IF ARTHROPOD-BORNE
#############################################################################
# Load datasets from disk #
#%%
df_arth_borne = pd.read_csv("<filename of the appropriate database>")
df_h2o_arth_borne = h2o.import_file("<filename of the appropriate database>")
# Run model comparison #
#%%
# cross-val settings
validation_size = 0.3
num_splits = 5
scoring = "accuracy"
sss = StratifiedShuffleSplit(n_splits=num_splits, test_size=validation_size, random_state=seed)
# models for comparison
models = []
models.append(("KNN", KNeighborsClassifier()))
models.append(("LR", LogisticRegression()))
models.append(("SGD", SGDClassifier()))
models.append(("NB", GaussianNB()))
models.append(("SVM", SVC()))
models.append(("RF", RandomForestClassifier()))
models.append(("XGB", XGBClassifier()))
# data
X = df_arth_borne
y = df_arth_borne.index
## for H2O models
X_h2o = df_h2o_arth_borne.columns
y_h2o = "class"
X_h2o.remove(y_h2o)
df_h2o_arth_borne[0] = df_h2o_arth_borne[0].asfactor()
train, valid, test = df_h2o_arth_borne.split_frame(ratios=[0.7, 0.15], seed=seed)
h2o_spot = H2OGradientBoostingEstimator(nfolds=num_splits)
h2o_spot.train(x=X_h2o,
y=y_h2o,
training_frame=train,
validation_frame=test)
h2o_cv_results = h2o_spot.cross_validation_metrics_summary().as_data_frame().iloc[0,3:].values.astype("float64")
# prepare placeholders for results
results = []
names = []
# for Scikit models
for name, model in models:
cv_results = cross_val_score(model, X, y, cv=sss, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "Cross val score for {0}: {1:.2%} ± {2:.2%}".format(
name, cv_results.mean(), cv_results.std())
print(msg)
# append H2O info
name = "H2O_GBM"
names.append(name)
results.append(h2o_cv_results)
msg = "Cross val score for {0}: {1:.2%} ± {2:.2%}".format(
name, h2o_cv_results.mean(), h2o_cv_results.std())
print(msg)
#############################################################################
# PREDICTING VECTOR TAXON
#############################################################################
# Load datasets from disk #
#%%
df__vec_taxon = pd.read_csv("<filename of the appropriate database>")
df_h2o__vec_taxon = h2o.import_file("<filename of the appropriate database>")
# Run model comparison #
#%%
# cross-val settings
validation_size = 0.3
num_splits = 5
scoring = "accuracy"
sss = StratifiedShuffleSplit(n_splits=num_splits, test_size=validation_size, random_state=seed)
# models for comparison
models = []
models.append(("KNN", KNeighborsClassifier()))
models.append(("LR", LogisticRegression()))
models.append(("SGD", SGDClassifier()))
models.append(("NB", GaussianNB()))
models.append(("SVM", SVC()))
models.append(("RF", RandomForestClassifier()))
models.append(("XGB", XGBClassifier()))
h2o_spot = H2OGradientBoostingEstimator(
nfolds=num_splits)
# data
X = df__vec_taxon
y = df__vec_taxon.index
## for H2O models
X_h2o = df_h2o__vec_taxon.columns
del X_h2o[0]
y_h2o = "class"
train, test = df_h2o__vec_taxon.split_frame(ratios=[1-validation_size], seed=seed)
h2o_spot.train(x=X_h2o,
y=y_h2o,
training_frame=train,
validation_frame=test)
h2o_cv_results = h2o_spot.cross_validation_metrics_summary().as_data_frame().iloc[0,3:].values.astype("float64")
# prepare placeholders for results
results = []
names = []
# for Scikit models
for name, model in models:
cv_results = cross_val_score(model, X, y, cv=sss, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "Cross val score for {0}: {1:.2%} ± {2:.2%}".format(
name, cv_results.mean(), cv_results.std())
print(msg)
# append H2O info
name = "H2O_GBM"
names.append(name)
results.append(h2o_cv_results)
msg = "Cross val score for {0}: {1:.2%} ± {2:.2%}".format(
name, h2o_cv_results.mean(), h2o_cv_results.std())
print(msg)