-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoML.py
411 lines (380 loc) · 14.3 KB
/
autoML.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn.model_selection import train_test_split, GroupKFold, cross_val_score
import numpy as np
from sklearn.feature_selection import SelectKBest, chi2, SelectFromModel, RFE
# from seaborn import heatmap
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LinearRegression, LogisticRegression
from collections import defaultdict
# from keras.models import Sequential
# from keras.layers import Dense, Activation, Input, Dropout, Flatten, Reshape
# from keras.wrappers.scikit_learn import KerasClassifier
# from keras.optimizers import SGD
from random import choice
def list2str(x):
return ','.join(map(str,x))
def oversample(X,y,names):
total_allowed=sum(y)
legal_rows=range(len(y))
out_X=[]
out_y=[]
out_names=[]
acutes_seen=0
chronics_seen=0
while True:
id=choice(legal_rows)
recency_val=y[id]
if recency_val==0:
if acutes_seen!=total_allowed:
acutes_seen+=1
out_X.append(X[id])
out_y.append(recency_val)
out_names.append(names[id])
else:
if chronics_seen==total_allowed:
return out_X,out_y,out_names
else:
if chronics_seen!=total_allowed:
chronics_seen+=1
out_X.append(X[id])
out_y.append(recency_val)
out_names.append(names[id])
# print('chronic,'+names[id])
else:
if acutes_seen==total_allowed:
return out_X,out_y,out_names
def custom_tt_split(X_train,y_train):
y_train=np.reshape(np.array(y_train),[len(y_train),1])
data=np.concatenate([X_train,y_train],axis=1)
acutes=[]
chronics=[]
for row in data:
yval=int(row[-1])
if yval==1:
chronics.append(row)
else:
acutes.append(row)
num_chron=len(chronics)
num_acute=len(acutes)
if num_chron<num_acute:
chronics_o,acutes_o=oversample(chronics,acutes)
chronics_u,acutes_u=undersample(chronics,acutes)
elif num_chron>num_acute:
acutes_o,chronics_o=oversample(acutes,chronics)
acutes_u,chronics_u=undersample(acutes,chronics)
data_o=np.concatenate([acutes_o,chronics_o])
data_u=np.concatenate([acutes_u,chronics_u])
return data_o[:,:-1],data_o[:,-1],data_u[:,:-1],data_u[:,-1]
def undersample(small_class,big_class):
random_bigclass=np.random.permutation(big_class)
out=[]
for i in range(len(small_class)):
out.append(random_bigclass[i])
return small_class,out
def oversample(small_class,big_class):
num_2_add=len(big_class)-len(small_class)
out=small_class[:]
for i in range(num_2_add):
out.append(choice(small_class))
return out,big_class
import numpy as np
import sys, os
from Bio import SeqIO
from pyseqdist import hamming
from itertools import combinations
def getseqs(input): #get sequences from 2 files
seqs=[]
with open(input) as input_handle:
for record in SeqIO.parse(input_handle, "fasta"): # for FASTQ use "fastq", for fasta "fasta"
seqs.append(record.seq)
return seqs
def calcDistanceMatrix(seqs1,seqs2): #calculate distance matrix from the 1-step list
hdist=hamming(seqs1,seqs2,ignore_gaps=False)
l=len(seqs1)
w=len(seqs2)
arr=np.zeros([l,w])
for id in range(len(hdist)):
item=hdist[id]
arr[:,id]=item[:,0]
return arr
def trimfh(file):
return os.path.splitext(os.path.basename(file))[0]
if __name__=="__main__":
files={}
for file in os.listdir(os.getcwd()):
if file.endswith(".fas"):
files[trimfh(file)]=getseqs(file)
pairwise_min_hamming=[]
for f1,f2 in combinations(files,2):
seqs1=files[f1]
seqs2=files[f2]
array=calcDistanceMatrix(seqs1,seqs2)
val=int(np.amin(array))
pairwise_min_hamming.append(f1,f2)
#====
modeldic={
0:'random forest',
1:'extra_random_forest',
2:'svm',
3:'logistic regression',
4:'naive_bayes'
}
total_storage=[]
data_end=-3
for i in range(len(sourcefiledic)):
reg=[]
over=[]
under=[]
qq=sourcefiledic[i]
data=pd.read_csv(qq,index_col=0)
X=data.iloc[:,0:data_end]
y=data['status']
groups=data['10_hamming']
var_names=list(data)
sample_names=data.index.values
x_train,_,y_train,_=train_test_split(X,y)
x_over_train,y_over_train,x_under_train,y_under_train=custom_tt_split(x_train,y_train) #make sure train and test don't have the same guy
for modelid in range(len(modeldic)):
if modelid==0:
clf=RandomForestClassifier(n_estimators=100)
clf_over=RandomForestClassifier(n_estimators=100)
clf_under=RandomForestClassifier(n_estimators=100)
elif modelid==1:
clf=ExtraTreesClassifier(n_estimators=100)
clf_over=ExtraTreesClassifier(n_estimators=100)
clf_under=ExtraTreesClassifier(n_estimators=100)
elif modelid==2:
clf=SVC(kernel='linear')
clf_over=SVC(kernel='linear')
clf_under=SVC(kernel='linear')
elif modelid==3:
clf=GaussianNB()
clf_over=GaussianNB()
clf_under=GaussianNB()
elif modelid==4:
clf=LogisticRegression(solver='liblinear')
clf_over=LogisticRegression(solver='liblinear')
clf_under=LogisticRegression(solver='liblinear')
clf.fit(x_train,y_train)
clf_over.fit(x_over_train,y_over_train)
clf_under.fit(x_under_train,y_under_train)
scores=cross_val_score(clf,X,y,cv=10)
scores_over=cross_val_score(clf_over,X,y,cv=10)
scores_under=cross_val_score(clf_under,X,y,cv=10)
reg.append(np.mean(scores))
over.append(np.mean(scores_over))
under.append(np.mean(scores_under))
total_storage.append([under,reg,over])
over_under_names=['Undersampling','Regular sampling','Oversampling']
sourcefiledic={0:'pelin_model.csv',2:'11_no_phacelia_model.csv',3:'11_model.csv',1:'phacelia_only.csv'}
desired_names=['GSU','Phacelia','DORIS w/o Phacelia','DORIS']
x=np.zeros([3,4])
for i in sourcefiledic:
print(desired_names[i]+",Random forest,Extra Random Forest,SVM,logistic regression,naive bayes,average")
item=total_storage[i]
for samp_id in range(3):
avg=np.mean(item[samp_id])
l=item[samp_id]
val=l[1]
l.insert(0,over_under_names[samp_id])
l.append(str(avg))
x[samp_id,i]=val
print(list2str(l))
print()
print(','+list2str(desired_names))
for a in range(3):
qw=list(x[a])
qw.insert(0,over_under_names[a])
print(list2str(qw))from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn.model_selection import train_test_split, GroupKFold, cross_val_score
import numpy as np
from sklearn.feature_selection import SelectKBest, chi2, SelectFromModel, RFE
# from seaborn import heatmap
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LinearRegression, LogisticRegression
from collections import defaultdict
# from keras.models import Sequential
# from keras.layers import Dense, Activation, Input, Dropout, Flatten, Reshape
# from keras.wrappers.scikit_learn import KerasClassifier
# from keras.optimizers import SGD
from random import choice
def list2str(x):
return ','.join(map(str,x))
def oversample(X,y,names):
total_allowed=sum(y)
legal_rows=range(len(y))
out_X=[]
out_y=[]
out_names=[]
acutes_seen=0
chronics_seen=0
# print(len(names))
while True:
id=choice(legal_rows)
# print(id)
# print(names[id])
recency_val=y[id]
if recency_val==0:
if acutes_seen!=total_allowed:
acutes_seen+=1
out_X.append(X[id])
out_y.append(recency_val)
out_names.append(names[id])
# print('acute,'+names[id])
else:
if chronics_seen==total_allowed:
return out_X,out_y,out_names
else:
if chronics_seen!=total_allowed:
chronics_seen+=1
out_X.append(X[id])
out_y.append(recency_val)
out_names.append(names[id])
# print('chronic,'+names[id])
else:
if acutes_seen==total_allowed:
return out_X,out_y,out_names
def custom_tt_split(X_train,y_train):
y_train=np.reshape(np.array(y_train),[len(y_train),1])
data=np.concatenate([X_train,y_train],axis=1)
acutes=[]
chronics=[]
for row in data:
yval=int(row[-1])
if yval==1:
chronics.append(row)
else:
acutes.append(row)
num_chron=len(chronics)
num_acute=len(acutes)
if num_chron<num_acute:
chronics_o,acutes_o=oversample(chronics,acutes)
chronics_u,acutes_u=undersample(chronics,acutes)
elif num_chron>num_acute:
acutes_o,chronics_o=oversample(acutes,chronics)
acutes_u,chronics_u=undersample(acutes,chronics)
data_o=np.concatenate([acutes_o,chronics_o])
data_u=np.concatenate([acutes_u,chronics_u])
# print(len(y_train))
# print(sum(y_train)[0]*2)
# print((len(y_train)-sum(y_train)[0])*2)
# print(np.shape(data_o))
# print(np.shape(data_u))
# print('under')
# for row in data_u:
# print(list2str(row))
# print("===")
# print('over')
# print("===")
# for row in data_o:
# print(list2str(row))
# exit()
return data_o[:,:-1],data_o[:,-1],data_u[:,:-1],data_u[:,-1]
def undersample(small_class,big_class):
random_bigclass=np.random.permutation(big_class)
out=[]
for i in range(len(small_class)):
out.append(random_bigclass[i])
return small_class,out
def oversample(small_class,big_class):
num_2_add=len(big_class)-len(small_class)
out=small_class[:]
for i in range(num_2_add):
out.append(choice(small_class))
return out,big_class
data_end=-3
# sourcefiledic={0:'17',1:'16',2:'13',3:'12',4:'10_1',5:'10_2',6:'5',7:'pelin',8:'6',9:'9'}
# sourcefiledic={0:'11_model.csv'}
# sourcefiledic={0:'17',1:'5',2:'pelin',3:'11',4:'11_no_phacelia'}
# sourcefiledic={0:'11_model.csv',1:'11_no_phacelia_model.csv',2:'pelin.csv',3:'phacelia_only.csv'}
sourcefiledic={0:'pelin_model.csv',1:'11_no_phacelia_model.csv',2:'11_model.csv',3:'phacelia_only.csv'}
# sourcefiledic={0:'tmp.csv'}
modeldic={
0:'random forest',
1:'extra_random_forest',
2:'svm',
3:'logistic regression',
4:'naive_bayes'
}
groupdic={
0:'genotype',
1:'10 hamming'
}
total_storage=[]
data_end=-3
for i in range(len(sourcefiledic)):
reg=[]
over=[]
under=[]
qq=sourcefiledic[i]
# print(qq)
# print(type(qq))
data=pd.read_csv(qq,index_col=0)
X=data.iloc[:,0:data_end]
y=data['status']
groups=data['10_hamming']
var_names=list(data)
sample_names=data.index.values
# X,y,names,groups,samples=ml_data_parser(source_file)
x_train,_,y_train,_=train_test_split(X,y)
x_over_train,y_over_train,x_under_train,y_under_train=custom_tt_split(x_train,y_train) #make sure train and test don't have the same guy
for modelid in range(len(modeldic)):
if modelid==0:
clf=RandomForestClassifier(n_estimators=100)
clf_over=RandomForestClassifier(n_estimators=100)
clf_under=RandomForestClassifier(n_estimators=100)
elif modelid==1:
clf=ExtraTreesClassifier(n_estimators=100)
clf_over=ExtraTreesClassifier(n_estimators=100)
clf_under=ExtraTreesClassifier(n_estimators=100)
elif modelid==2:
clf=SVC(kernel='linear')
clf_over=SVC(kernel='linear')
clf_under=SVC(kernel='linear')
elif modelid==3:
clf=GaussianNB()
clf_over=GaussianNB()
clf_under=GaussianNB()
elif modelid==4:
clf=LogisticRegression(solver='liblinear')
clf_over=LogisticRegression(solver='liblinear')
clf_under=LogisticRegression(solver='liblinear')
clf.fit(x_train,y_train)
clf_over.fit(x_over_train,y_over_train)
clf_under.fit(x_under_train,y_under_train)
scores=cross_val_score(clf,X,y,cv=10)
scores_over=cross_val_score(clf_over,X,y,cv=10)
scores_under=cross_val_score(clf_under,X,y,cv=10)
reg.append(np.mean(scores))
over.append(np.mean(scores_over))
under.append(np.mean(scores_under))
total_storage.append([under,reg,over])
over_under_names=['Undersampling','Regular sampling','Oversampling']
sourcefiledic={0:'pelin_model.csv',2:'11_no_phacelia_model.csv',3:'11_model.csv',1:'phacelia_only.csv'}
desired_names=['GSU','Phacelia','DORIS w/o Phacelia','DORIS']
x=np.zeros([3,4])
for i in sourcefiledic:
print(desired_names[i]+",Random forest,Extra Random Forest,SVM,logistic regression,naive bayes,average")
item=total_storage[i]
for samp_id in range(3):
avg=np.mean(item[samp_id])
l=item[samp_id]
val=l[1]
l.insert(0,over_under_names[samp_id])
l.append(str(avg))
x[samp_id,i]=val
print(list2str(l))
print()
print(','+list2str(desired_names))
for a in range(3):
qw=list(x[a])
qw.insert(0,over_under_names[a])
print(list2str(qw))