-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrain_meta_predictor.py
295 lines (130 loc) · 4.32 KB
/
train_meta_predictor.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from mlxtend.feature_selection import ColumnSelector
from joblib import dump
# ## Parameters
# In[ ]:
INPUT='/home/tbrittoborges/Baltica/nanopore_benchmark/results/SJ_annotated.csv'
TEST_SIZE=0.2
RANDOM=1982
NANOPORE_CUTOFF=0.95
np.random.seed(RANDOM)
# ## Setup
# In[ ]:
# In[ ]:
data = pd.read_csv(INPUT)
# In[ ]:
data.head()
# In[ ]:
data.columns = data.columns.str.lower()
# In[ ]:
data = data.drop(columns=['is_novel', 'gene_name', 'transcript_name', 'class_code', 'exon_number', 'coordinates'])
# In[ ]:
data = data.fillna(0)
# In[ ]:
data.head()
# In[ ]:
y = data['orthogonal_NA']
y = np.where(y > 0.95, 1, 0) # classification problem
x = data.drop(columns='orthogonal_NA')
# ## Data split and preprocess
# In[ ]:
X_train, X_test, y_train, y_test = train_test_split(x, y, stratify=y, test_size=TEST_SIZE, random_state=RANDOM)
# In[ ]:
X_train.info()
# In[ ]:
np.unique(y_train, return_counts=True)[1]
# In[ ]:
np.unique(y_test, return_counts=True)
# ## Pipeline contruction
# ColumnSelector API http://rasbt.github.io/mlxtend/user_guide/feature_selection/ColumnSelector/#api
# In[ ]:
import itertools
# In[ ]:
col_selector = list(itertools.chain.from_iterable([itertools.combinations(range(4), i + 1) for i in range(4)]))
# In[ ]:
col_selector
# ## GBC
# In[ ]:
pipe = Pipeline(
[('select', ColumnSelector()),
('xboost', GradientBoostingClassifier(random_state=RANDOM))])
# In[ ]:
param_grid = {
"select__cols": col_selector,
"xboost__learning_rate": [1, 0.5, 0.25, 0.1, 0.05, 0.01],
"xboost__max_depth": [1, 2, 4, 8],
"xboost__subsample": [0.5, 0.8, 1.0],
"xboost__min_samples_split": [0.2, 0.4, 0.8],
"xboost__n_estimators": [1, 2, 4, 8, 16, 32, 64, 100, 200]}
print('Starting GCB grid search')
grid = GridSearchCV(pipe, param_grid, cv=10, n_jobs=-1, scoring='roc_auc')
# In[ ]:
print('Starting GCB grid search')
grid.fit(X_train, y_train)
print('Best parameters:', grid.best_params_)
print('Best performance:', grid.best_score_)
# In[ ]:
df_grid_results_1 = pd.DataFrame(grid.cv_results_)
# In[ ]:
df_grid_results_1 = df_grid_results_1.sort_values('rank_test_score')
# In[ ]:
df_grid_results_1
# In[ ]:
print('Saving GCB grid search')
df_grid_results_1.to_csv('gcb_grid_search_result.csv')
df_grid_results_1.groupby('param_select__cols').first().to_csv('gcb_grid_search_result_grouped.csv')
# In[ ]:
df_grid_results_1.shape
# In[ ]:
df_grid_results_1
# In[ ]:
df_grid_results_1.loc[
:,['param_select__cols', 'mean_test_score', 'rank_test_score']]
# In[ ]:
df_grid_results_1.loc[
:,['param_select__cols']].value_counts()
# In[ ]:
df_grid_results_1.loc[:, "param_select__cols"] = df_grid_results_1.loc[:, "param_select__cols"].astype(str)
# In[ ]:
df_grid_results_1.query("param_select__cols != '(0, 1, 2, 3)'").loc[
:, ["param_select__cols", "mean_test_score", "rank_test_score"]]
# In[ ]:
dump(grid.best_estimator_, 'gcb_meta_best_estimator.joblib')
dump(grid, 'gcb_grid_object.joblib')
# ## Logistic regression
# In[ ]:
pipe2 = Pipeline(
[('select', ColumnSelector()),
('log_reg', LogisticRegression(random_state=RANDOM))])
# In[ ]:
print("Starting LR grid search")
param_grid2= {
"select__cols": col_selector,
"log_reg__C": np.logspace(-3, 4, 5),
"log_reg__penalty":["l1", "l2"] }
# In[ ]:
grid2 = GridSearchCV(pipe2, param_grid2, cv=10, n_jobs=-1, scoring='roc_auc')
# In[ ]:
print("FItting LR grid search")
grid2.fit(X_train, y_train)
print('Best parameters:', grid2.best_params_)
print('Best performance:', grid2.best_score_)
# In[ ]:
df_grid2_results = pd.DataFrame(grid2.cv_results_)
# In[ ]:
dump(grid2.best_estimator_, 'lr_meta_best_estimator.joblib')
dump(grid2, 'lr_grid_object.joblib')
df_grid2_results.to_csv('lr_grid_search_result.csv')
df_grid2_results = df_grid2_results.sort_values('rank_test_score')
# In[ ]:
df_grid2_results.groupby('param_select__cols').first().to_csv('lr_grid_search_result_grouped.csv')
# In[ ]: