-
Notifications
You must be signed in to change notification settings - Fork 6
/
d22.py
300 lines (233 loc) · 11 KB
/
d22.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
292
293
294
import os
import gc
import sys
import pickle
import numpy as np
import pandas as pd
from sklearn.model_selection import KFold, cross_val_predict
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.linear_model import Ridge, OrthogonalMatchingPursuit, BayesianRidge, ElasticNet, OrthogonalMatchingPursuitCV, HuberRegressor, Lasso
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import DotProduct
from sklearn.neural_network import MLPRegressor
from sklearn.ensemble import BaggingRegressor
from sklearn.kernel_ridge import KernelRidge
from rgf.sklearn import RGFRegressor
from sklearn.svm import NuSVR
from tqdm import tqdm
from trends import NMAE, TrendsModelSklearn
from utils import scale_select_data, save_pickle, read_pickle
def run(seed):
# create folders for scores models and preds
folder_models = './models/domain2_var2/scores/'
if not os.path.exists(folder_models):
os.makedirs(folder_models)
folder_preds = './predicts/domain2_var2/scores/'
if not os.path.exists(folder_preds):
os.makedirs(folder_preds)
print('Loading data...')
# load biases
ic_bias = read_pickle('./data/biases/ic_biases.pickle')
ic_bias_site = read_pickle('./data/biases/ic_biases_site.pickle')
fnc_bias = read_pickle('./data/biases/fnc_biases.pickle')
fnc_bias_site = read_pickle('./data/biases/fnc_biases_site.pickle')
pca_bias = read_pickle('./data/biases/200pca_biases.pickle')
pca_bias_site = read_pickle('./data/biases/200pca_biases_site.pickle')
# load classifier and add extra sites2
extra_site = pd.DataFrame()
extra_site['Id'] = np.load('./predicts/classifier/site2_test_new_9735.npy')
# load competiton data
ids_df = pd.read_csv('./data/raw/reveal_ID_site2.csv')
fnc_df = pd.read_csv('./data/raw/fnc.csv')
loading_df = pd.read_csv('./data/raw/loading.csv')
labels_df = pd.read_csv('./data/raw/train_scores.csv')
ids_df = ids_df.append(extra_site)
print('Detected Site2 ids count: ', ids_df['Id'].nunique())
# load created features
agg_df = pd.read_csv('./data/features/agg_feats.csv')
im_df = pd.read_csv('./data/features/im_feats.csv')
dl_df = pd.read_csv('./data/features/dl_feats.csv')
pca_df = pd.read_csv('./data/features/200pca_feats/200pca_3d_k0.csv')
for i in range(1, 6):
part = pd.read_csv('./data/features/200pca_feats/200pca_3d_k{}.csv'.format(i)); del part['Id']
pca_df = pd.concat((pca_df, part), axis=1)
# merge data
ic_cols = list(loading_df.columns[1:])
fnc_cols = list(fnc_df.columns[1:])
agg_cols = list(agg_df.columns[1:])
im_cols = list(im_df.columns[1:])
pca_cols = list(pca_df.columns[1:])
dl_cols = list(dl_df.columns[1:])
pca0_cols = [c for c in pca_cols if 'k0' in c]
df = fnc_df.merge(loading_df, on='Id')
df = df.merge(agg_df, how='left', on='Id')
df = df.merge(im_df, how='left', on='Id')
df = df.merge(pca_df, how='left', on='Id')
df = df.merge(dl_df, how='left', on='Id')
df = df.merge(labels_df, how='left', on='Id')
del loading_df, fnc_df, agg_df, im_df, pca_df
gc.collect()
# split train and test
df.loc[df['Id'].isin(labels_df['Id']), 'is_test'] = 0
df.loc[~df['Id'].isin(labels_df['Id']), 'is_test'] = 1
train = df.query('is_test==0'); del train['is_test']
test = df.query('is_test==1'); del test['is_test']
y = train['domain2_var2'].copy().reset_index(drop=True)
d22_index = list(train['domain2_var2'].dropna().index)
# apply biases
for c in ic_bias_site.keys():
test.loc[~test['Id'].isin(ids_df['Id']), c] += ic_bias[c]
test.loc[test['Id'].isin(ids_df['Id']), c] += ic_bias_site[c]
for c in fnc_bias_site.keys():
test.loc[~test['Id'].isin(ids_df['Id']), c] += fnc_bias[c]
test.loc[test['Id'].isin(ids_df['Id']), c] += fnc_bias_site[c]
for c in pca_bias_site.keys():
test.loc[~test['Id'].isin(ids_df['Id']), c] += pca_bias[c]
test.loc[test['Id'].isin(ids_df['Id']), c] += pca_bias_site[c]
# save df for scaling
df_scale = pd.concat([train, test], axis=0)
# I. Create fnc score
print('Creating FNC score...')
# prepare datasets for fnc score
train_for_score, test_for_score = scale_select_data(train, test, df_scale, fnc_cols)
# define models
names = ['ENet', 'BRidge', 'Huber', 'OMP']
names = [name + '_fnc_seed{}'.format(seed) for name in names]
pack = [ElasticNet(alpha=0.05, l1_ratio=0.5, random_state=0),
BayesianRidge(),
HuberRegressor(epsilon=2.5, alpha=1),
OrthogonalMatchingPursuit(n_nonzero_coefs=300)]
# train models
zoo = TrendsModelSklearn(pack, seed=seed)
zoo.fit([train_for_score]*4, y)
score_blend = zoo.blend_oof()
pred = zoo.predict([test_for_score]*4, names)
# save oof, pred, models
np.save(folder_preds + 'fnc_score_seed{}.npy'.format(seed), score_blend)
np.save(folder_preds + 'fnc_score_test_seed{}.npy'.format(seed), pred)
zoo.save_models(names, folder=folder_models)
# II. Create agg score
print('Creating AGG score...')
# prepare datasets for agg score
train_for_score, test_for_score = scale_select_data(train, test, df_scale, agg_cols)
# define models
names = ['RGF', 'ENet', 'Huber']
names = [name + '_agg_seed{}'.format(seed) for name in names]
pack = [RGFRegressor(max_leaf=1000, reg_depth=5, min_samples_leaf=100, normalize=True),
ElasticNet(alpha=0.05, l1_ratio=0.3, random_state=0),
HuberRegressor(epsilon=2.5, alpha=1)]
# train models
zoo = TrendsModelSklearn(pack, seed=seed)
zoo.fit([train_for_score]*3, y)
score_blend = zoo.blend_oof()
pred = zoo.predict([test_for_score]*3, names)
# save oof, pred, models
np.save(folder_preds + 'agg_score_seed{}.npy'.format(seed), score_blend)
np.save(folder_preds + 'agg_score_test_seed{}.npy'.format(seed), pred)
zoo.save_models(names, folder=folder_models)
# III. Create pca score
print('Creating PCA score...')
# prepare datasets for pca score
train_for_score, test_for_score = scale_select_data(train, test, df_scale, pca_cols)
# define models
names = ['ENet', 'BRidge', 'OMP']
names = [name + '_pca_seed{}'.format(seed) for name in names]
pack = [ElasticNet(alpha=0.2, l1_ratio=0.2, random_state=0),
BayesianRidge(),
OrthogonalMatchingPursuit()]
# train models
zoo = TrendsModelSklearn(pack, seed=seed)
zoo.fit([train_for_score]*3, y)
score_blend = zoo.blend_oof()
pred = zoo.predict([test_for_score]*3, names)
# save oof, pred, models
np.save(folder_preds + 'pca_score_seed{}.npy'.format(seed), score_blend)
np.save(folder_preds + 'pca_score_test_seed{}.npy'.format(seed), pred)
zoo.save_models(names, folder=folder_models)
# IV. Create im score
print('Creating IM score...')
# prepare datasets for pca score
train_for_score, test_for_score = scale_select_data(train, test, df_scale, im_cols)
# define models
names = ['ENet', 'BRidge', 'OMP']
names = [name + '_im_seed{}'.format(seed) for name in names]
pack = [ElasticNet(alpha=0.2, l1_ratio=0.2, random_state=0),
BayesianRidge(),
OrthogonalMatchingPursuit()]
# train models
zoo = TrendsModelSklearn(pack, seed=seed)
zoo.fit([train_for_score]*3, y)
score_blend = zoo.blend_oof()
pred = zoo.predict([test_for_score]*3, names)
# save oof, pred, models
np.save(folder_preds + 'im_score_seed{}.npy'.format(seed), score_blend)
np.save(folder_preds + 'im_score_test_seed{}.npy'.format(seed), pred)
zoo.save_models(names, folder=folder_models)
# V. Create dl score
print('Creating DL score...')
# prepare datasets for pca score
train_for_score, test_for_score = scale_select_data(train, test, df_scale, dl_cols)
# define models
names = ['ENet', 'BRidge', 'OMP']
names = [name + '_dl_seed{}'.format(seed) for name in names]
pack = [ElasticNet(alpha=0.2, l1_ratio=0.2, random_state=0),
BayesianRidge(),
OrthogonalMatchingPursuit()]
# train models
zoo = TrendsModelSklearn(pack, seed=seed)
zoo.fit([train_for_score]*3, y)
score_blend = zoo.blend_oof()
pred = zoo.predict([test_for_score]*3, names)
# save oof, pred, models
np.save(folder_preds + 'dl_score_seed{}.npy'.format(seed), score_blend)
np.save(folder_preds + 'dl_score_test_seed{}.npy'.format(seed), pred)
zoo.save_models(names, folder=folder_models)
# VI. Training and predicting procedure
print('Training has started...')
# add scores
for prefix in ['fnc', 'agg', 'im', 'pca', 'dl']:
train.loc[d22_index, prefix + '_score'] = np.load(folder_preds + '/{}_score_seed{}.npy'.format(prefix, seed))
test.loc[:, prefix + '_score'] = np.load(folder_preds + '/{}_score_test_seed{}.npy'.format(prefix, seed))
score_cols = [c for c in train.columns if c.endswith('_score')]
# save df for scaling
df_scale = pd.concat([train, test], axis=0)
# create differents datasets
# linear
linear_cols = sorted(list(set(ic_cols + fnc_cols + pca_cols) - set(['IC_20'])))
train_linear, test_linear = scale_select_data(train, test, df_scale, linear_cols)
# kernel
kernel_cols = sorted(list(set(ic_cols + pca_cols) - set(['IC_20'])))
train_kernel, test_kernel = scale_select_data(train=train, test=test, df_scale=df_scale, cols=kernel_cols, scale_factor=0.1, scale_cols=pca_cols, sc=StandardScaler())
# score
sc_cols = sorted(list(set(ic_cols + score_cols) - set(['IC_20'])))
train_sc, test_sc = scale_select_data(train, test, df_scale, sc_cols)
# learning process on different datasets
names = ['GP', 'SVM', 'BR', 'KR']
names = [name + '_seed{}'.format(seed) for name in names]
pack = [GaussianProcessRegressor(DotProduct(), random_state=0),
NuSVR(C=2, kernel='rbf'),
BayesianRidge(),
KernelRidge(kernel='poly', alpha=30)]
zoo = TrendsModelSklearn(pack, seed=seed)
zoo.fit([train_sc] + [train_kernel] + [train_linear]*2, y)
de_blend = zoo.blend_oof()
preds = zoo.predict([test_sc] + [test_kernel] + [test_linear]*2, names, is_blend=True)
# rewrite folders for models and preds
folder_models = './models/domain2_var2/stack/'
if not os.path.exists(folder_models):
os.makedirs(folder_models)
folder_preds = './predicts/domain2_var2/stack/'
if not os.path.exists(folder_preds):
os.makedirs(folder_preds)
print('Saving models to', folder_models)
print('Saving predictions to', folder_preds)
# save oofs and models
zoo.save_oofs(names, folder=folder_preds)
zoo.save_models(names, folder=folder_models)
# stacking predictions
print('Stacking predictions...')
d22_prediction = pd.DataFrame()
d22_prediction['Id'] = test['Id'].values
d22_prediction['pred'] = preds
d22_prediction.to_csv(folder_preds + 'domain2_var2_stack_seed{}.csv'.format(seed), index=False)
print('domain2_var2 seed pred is saved as', folder_preds + 'domain2_var2_stack_seed{}.csv'.format(seed))