-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
330 lines (290 loc) · 9.49 KB
/
main.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
from __future__ import print_function
from pprint import pprint
from itertools import product
from timeit import default_timer as timer
from joblib import Memory
from sklearn import ensemble, linear_model, neural_network, svm, tree, kernel_ridge
from sklearn.cross_decomposition import PLSRegression
from sklearn.decomposition import PCA, NMF
from sklearn.feature_selection import (
SelectKBest, f_regression, mutual_info_regression
)
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import DotProduct, WhiteKernel
from sklearn.impute import SimpleImputer
from sklearn.model_selection import (
GridSearchCV,
KFold,
RandomizedSearchCV,
LeaveOneOut
)
from sklearn.neighbors import KNeighborsRegressor
from sklearn.pipeline import Pipeline, FeatureUnion
from tqdm import tqdm
import numpy as np
import pandas as pd
from transformations import (
ALPHABET,
memory,
DummyEstimator,
FFTEncoder,
OneHotEncoder,
OneHotPairEncoder,
#PyBioMedEncoder,
AAIndexEncoder,
get_index_names
)
EXPECTED_VARIANT_LENGTH = 4
FPATH_DATASET = 'DataSet for Assignment.xlsx - Sheet1 (1).csv'
NUM_FOLDS = None # if None, NUM_FOLDS == num_rows ==> leave one out
N_FEATURES_RATIOS = [
.01,
.1,
1
]
CV_VERBOSE = 10
NUM_CANDIDATES = 10
def main():
"""Main"""
search, estimator = innovSAR()
pick_candidates(estimator, NUM_CANDIDATES)
def innovSAR():
df = load_data()
Y = df['Fitness']
X = df[['Variants']]
index_names = get_index_names()
pipeline = Pipeline(
[
('encode', AAIndexEncoder()),
('fft', FFTEncoder()),
('impute', SimpleImputer(missing_values=np.nan, strategy='mean')),
('regress', PLSRegression())
],
#memory
)
print(len(index_names), 'indexes')
grid = {
'encode__index_name': index_names,
# TODO: search over models and model hyperparameters
}
if NUM_FOLDS:
cv = KFold(n_splits=NUM_FOLDS, random_state=0)
else:
cv = LeaveOneOut()
search = GridSearchCV(
pipeline,
grid,
error_score=np.nan,
n_jobs=-1,
cv=cv,
verbose=CV_VERBOSE
)
print('*' * 40)
print('Searching')
print('*' * 40)
start = timer()
search.fit(X, Y)
end = timer()
print('Finished in: {}'.format(end - start))
best_estimator = search.best_estimator_
best_params = search.best_params_
best_score = search.best_score_
best_index = search.best_index_
best_std = search.cv_results_['std_test_score'][best_index]
print('best_estimator:', best_estimator)
print('best_params:', best_params)
print('best_score:', best_score)
print('best_std:', best_std)
return search, best_estimator
def pick_candidates(estimator, num_candidates):
df = load_data()
all_candidates = set(product(*[ALPHABET] * EXPECTED_VARIANT_LENGTH))
existing_candidates = set(df['Variants'])
new_candidates = all_candidates - existing_candidates
score_candidate_tups = []
print('Scoring candidates...')
for candidate in tqdm(new_candidates):
prediction = estimator.predict(pd.DataFrame({'Variants': [candidate]}))
score_candidate_tups.append((prediction, candidate))
score_candidate_tups.sort(key=lambda tup: tup[0])
top_candidates = score_candidate_tups[:num_candidates]
print('Top candidates:')
pprint(top_candidates)
def load_data():
"""Load data and run some sanity checks"""
df = pd.read_csv(FPATH_DATASET)
df.append({'Variants': 'VDGV', 'Fitness': 1}, ignore_index=True)
variants = df['Variants']
unique_variants = variants.unique()
assert len(unique_variants) == len(variants)
alphabet = set()
lengths = set()
variants.apply(lambda variant: alphabet.update(set(variant)))
alphabet = sorted(list(alphabet))
assert alphabet == ALPHABET, (alphabet, ALPHABET)
variants.apply(lambda variant: lengths.add(len(variant)))
assert len(lengths) == 1
variant_length = list(lengths)[0]
assert variant_length == EXPECTED_VARIANT_LENGTH
return df
def get_combined_grids(grid_steps):
combined_grids = []
grid_step_combinations = product(*grid_steps)
for grid_step_combination in grid_step_combinations:
combined_grid = {}
for grid_step in grid_step_combination:
# don't overwrite steps
if any([k in combined_grid for k in grid_step.keys()]):
continue
combined_grid.update(grid_step)
combined_grids.append(combined_grid)
return combined_grids
def get_best_estimator():
"""Hyperparameter optimization"""
df = load_data()
Y = df['Fitness']
X = df[['Variants']]
features = FeatureUnion([
#('one_hot_encoder', OneHotEncoder()),
#('one_hot_pair_encoder', OneHotPairEncoder()),
#('pybiomed_encoder', PyBioMedEncoder()),
('aaindex_encoder', AAIndexEncoder())
])
print('*' * 40)
print('Extracting features...')
print('*' * 40)
start = timer()
X = features.transform(X)
end = timer()
print('Finished in: {}'.format(end - start))
num_rows, num_cols = X.shape
assert num_rows == len(df)
print('Got {} features'.format(num_cols))
# TODO include this in pipeilne
imp = SimpleImputer(missing_values=np.nan, strategy='mean')
imp.fit(X)
X = imp.transform(X)
assert not pd.DataFrame(X).isna().any().any()
X = FFTEncoder().fit_transform(X)
import ipdb; ipdb.set_trace()
n_features_options = [int(num_cols * ratio) for ratio in N_FEATURES_RATIOS]
print('n_features_options:', n_features_options)
feature_reduction_grid = [
{
'reduce': [
#PCA(),
NMF()
],
'reduce__n_components': n_features_options,
},
#{
# 'reduce': [SelectKBest()],
# 'reduce__score_func': [
# f_regression,
# mutual_info_regression
# ],
# 'reduce__k': n_features_options,
#},
]
# Random forest features
# Number of trees
n_estimators = [int(x) for x in np.linspace(start = 200, stop = 2000, num = 10)]
# Number of features to consider at every split
max_features = ['auto', 'sqrt']
# Maximum number of levels in tree
max_depth = [int(x) for x in np.linspace(10, 110, num = 11)]
max_depth.append(None)
# Minimum number of samples required to split a node
min_samples_split = [2, 5, 10]
# Minimum number of samples required at each leaf node
min_samples_leaf = [1, 2, 4]
# Method of selecting samples for training each tree
bootstrap = [True, False]
# TODO: search over more params
regression_grid = [
#{
# 'regress': [
# #KNeighborsRegressor(),
# #linear_model.ARDRegression(),
# #linear_model.BayesianRidge(),
# #linear_model.ElasticNet(),
# #linear_model.LassoLars(),
# #linear_model.LinearRegression(),
# #linear_model.Ridge(),
# #linear_model.SGDRegressor(),
# #tree.DecisionTreeRegressor(),
# #ensemble.AdaBoostRegressor(),
# #ensemble.BaggingRegressor(),
# #ensemble.GradientBoostingRegressor(),
# ]
#},
#{
# 'regress': [ensemble.RandomForestRegressor()],
# #'regress__n_estimators': n_estimators,
# #'regress__max_features': max_features,
# #'regress__max_depth': max_depth,
# #'regress__min_samples_split': min_samples_split,
# #'regress__min_samples_leaf': min_samples_leaf,
# #'regress__bootstrap': bootstrap
#},
{
'regress': [PLSRegression()]
}
#{
# 'regress': [svm.NuSVR()],
# 'regress__C': [1, 10, 100, 1000],
# 'regress__kernel': ['rbf', 'linear', 'poly'],
#},
#{
# 'regress': [svm.LinearSVR()],
# 'regress__C': [1, 10, 100, 1000]
#}
#{
# 'regress': [neural_network.MLPRegressor()],
# 'regress__hidden_layer_sizes': [(100,)]
#},
]
pipeline = Pipeline(
[
#('fft', FFTEncoder()),
#('reduce', DummyEstimator()),
('regress', DummyEstimator())
],
#memory=memory
)
grid_steps = [
#feature_reduction_grid,
regression_grid
]
combined_grids = get_combined_grids(grid_steps)
print('combined_grids:')
pprint(combined_grids)
kfold = KFold(n_splits=NUM_FOLDS or num_rows, random_state=0)
search = GridSearchCV(
pipeline,
combined_grids,
error_score=np.nan,
verbose=5,
n_jobs=-1,
cv=kfold
)
print('*' * 40)
print('Searching')
print('*' * 40)
start = timer()
search.fit(X, Y)
end = timer()
print('Finished in: {}'.format(end - start))
best_estimator = search.best_estimator_
best_params = search.best_params_
best_score = search.best_score_
best_index = search.best_index_
best_std = search.cv_results_['std_test_score'][best_index]
print('best_estimator:', best_estimator)
print('best_params:', best_params)
print('best_score:', best_score)
print('best_std:', best_std)
return Pipeline([('features', features), ('estimator', best_estimator)])
get_best_estimator__cached = memory.cache(get_best_estimator)
if __name__ == '__main__':
main()