-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameter_optimization_location.py
More file actions
366 lines (294 loc) · 10.9 KB
/
parameter_optimization_location.py
File metadata and controls
366 lines (294 loc) · 10.9 KB
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
from __future__ import division
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from Bio import SeqIO
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import ShuffleSplit
from sklearn.linear_model import LogisticRegression
from future_module import extract_motifs_pos
fh = open("optimized.txt", "w")
def parameters(f1, f2, x1, x2, x3, x4, x5, x6, x7, x8):
corpus1=[]#make a blank list of corpus for the training set
tag=[]#make the list of outcomes
true = open("subcellular_localization_without multilabels.fasta")
count = 1
for lines in SeqIO.parse(true, "fasta"):
protid= lines.id
line = lines.seq.tostring().lower().replace("x","")
line = line.replace('-', "")
line = line.upper()
if "Cytoplasmic" in protid:
tag.append(0)
elif "Extracellular" in protid:
tag.append(1)
elif "Inner" in protid:
tag.append(2)
elif "Outer" in protid:
tag.append(3)
elif "Periplasmic" in protid:
tag.append(4)
#print count
# print line
fullstring = extract_motifs_pos(line,x1, x2, x3, x4, x5, x6, x7, x8)
#fullstring = fullstring+ " "+ pos_prot_1st_word(line)
corpus1.append(fullstring) #apperd string from each protein to corpus
true.close()
corpus = np.array(corpus1) #convert corpus into numpy array
tag = np.array(tag) # convert tag into numpy array
#print corpus # print for debugging
#print tag # print for debugging
del fullstring
del corpus1
try:
count = CountVectorizer(max_features=15000000, vocabulary = None, max_df=0.20, min_df = 7, stop_words=[1,2])#giving the CountVectorizer function a short name
#get the vocabulary of train set to use for the test set
bag = count.fit_transform(corpus) #transform the corpus(bag of words into sparse martrix)
#print (count.vocabulary_) #count the occurence of number of words
##get the vocabulary of train set to use for the test set. Next time put the "voc" in
#the vocabulary parameter of count
voc = count.get_feature_names()
#print len(voc)
if len(voc)>150000:
return 0.0
bag= bag.toarray() #convert the sparsematrix into an array
np.place(bag, bag>0, [1])
except:
return 0.0
cv = ShuffleSplit(n_splits=5, test_size=0.2, random_state =1)
clf= LogisticRegression(C= 1.0, random_state=1)
return np.mean(cross_val_score(clf, bag, tag, cv =cv))
def seeding1(f1, f2, x_seed = 0, iteration = 0, x5=1, x6=1, x7=5, x8=1, step = 3, first_time = True):
for i in range(0,26,step):
#print "seed: ", i
if iteration%2!=0:
x= i
x2 = x_seed
#print "x4 is parameter"
else:
x = i
x4 = x_seed
#print "x2 is parameter"
p = 0.0
if first_time:
p_new= -1
p_best = -1
else:
p_new = p_best
first_time = False
used = False
while True:
if iteration%2!=0:
x4= x
else:
x2 = x
#print "count: ", count
#print "value of x4: ", x4
p = parameters(f1, f2, 0, x2, 0, x4, x5, x6, x7, x8)
#print "accuracy for seed %d is: %f " % (i, p)
#print "x4 value is" , x4
#print "value of p", p
#print "value of p_new", p_new
if p_new < p:
p_new = p
#print "new value of p_new is: ", p_new
x+=1
#print "yes"
used = True
#print used
else:
#print "want to see if use is true or false :", used
#print "no"
p_best = p_new
if used:
x_best = x-1
#print 'best accuracy for seed %d is %f' %(i, p_best)
#print "best parameter so far: %d" %x_best
break
#print "best accuracy for seed is %f" % p_best
#print "best parameter so far: %d" %x_best
return x_best, p_best
def seeding2(f1, f2, x_seed = 0, iteration = 0, x2=1, x4=1, x7=5, x8=1, step = 1, first_time = True):
for i in range(0,11,step):
#print "seed: ", i
if iteration%2!=0:
x= i
x5 = x_seed
#print "x4 is parameter"
else:
x = i
x6= x_seed
#print "x2 is parameter"
p = 0.0
if first_time:
p_new= -1
p_best = -1
else:
p_new = p_best
first_time = False
used = False
while True:
if iteration%2!=0:
x6= x
else:
x5 = x
#print "count: ", count
#print "value of x4: ", x4
p = parameters(f1, f2, 0, x2, 0, x4, x5, x6, x7, x8)
#print "accuracy for seed %d is: %f " % (i, p)
#print "x4 value is" , x4
#print "value of p", p
#print "value of p_new", p_new
if p_new < p:
p_new = p
#print "new value of p_new is: ", p_new
x+=1
#print "yes"
used = True
#print used
else:
#print "want to see if use is true or false :", used
#print "no"
p_best = p_new
if used:
x_best = x-1
#print 'best accuracy for seed %d is %f' %(i, p_best)
#print "best parameter so far: %d" %x_best
break
#print "best accuracy for seed is %f" % p_best
#print "best parameter so far: %d" %x_best
return x_best, p_best
def seeding3(f1, f2, x_seed = 1, iteration = 0, x2=1, x4=1, x5=1, x6=1, step = 1, first_time = True):
for i in range(1,11,step):
#print "seed: ", i
if iteration%2!=0:
x= i
x7 = x_seed
#print "x4 is parameter"
else:
x = i
x8= x_seed
#print "x2 is parameter"
p = 0.0
if first_time:
p_new= -1
p_best = -1
else:
p_new = p_best
first_time = False
used = False
while True:
if iteration%2!=0:
x8= x
else:
x7 = x
#print "count: ", count
#print "value of x4: ", x4
p = parameters(f1, f2, 0, x2, 0, x4, x5, x6, x7, x8)
#print "accuracy for seed %d is: %f " % (i, p)
#print "x4 value is" , x4
#print "value of p", p
#print "value of p_new", p_new
if p_new < p:
p_new = p
#print "new value of p_new is: ", p_new
x+=1
#print "yes"
used = True
#print used
else:
#print "want to see if use is true or false :", used
#print "no"
p_best = p_new
if used:
x_best = x-1
#print 'best accuracy for seed %d is %f' %(i, p_best)
#print "best parameter so far: %d" %x_best
break
#print "best accuracy for seed is %f" % p_best
#print "best parameter so far: %d" %x_best
return x_best, p_best
def sub_tuning1(f1, f2, x5=1, x6=1, x7=5, x8=1):
x5=1; x6=1; x7=1;
initial = True
iteration = 0
while True:
if initial:
best_accuracy = -1
p_best = -1
x = 1
else:
if best_accuracy < p_best:
best_accuracy = p_best
x = x_best
else:
print "Best accuracy %f" % best_accuracy
return x2_best, x4_best
break
iteration += 1
initial = False
x_best, p_best = seeding1(f1, f2, x, iteration)
if iteration%2!=0:
x4_best= x_best
print "x4 best so far:", x4_best
else:
x2_best = x_best
print "x2 best so far:", x2_best
def sub_tuning2(f1, f2, x2=1, x4=1, x7=5, x8=1):
initial = True
iteration = 0
while True:
if initial:
best_accuracy = -1
p_best = -1
x = 0
else:
if best_accuracy < p_best:
best_accuracy = p_best
x = x_best
else:
print "Best accuracy %f" % best_accuracy
return x5_best, x6_best
break
iteration += 1
initial = False
x_best, p_best = seeding2(f1, f2, x, iteration, x2, x4)
if iteration%2!=0:
x6_best= x_best
print "x6 best so far:", x6_best
else:
x5_best = x_best
print "x5 best so far:", x5_best
print "Best accuracy %f" % best_accuracy
def sub_tuning3(f1, f2, x2=1, x4=1, x5=1, x6=1):
initial = True
iteration = 0
while True:
if initial:
best_accuracy = -1
p_best = -1
x = 1
else:
if best_accuracy < p_best:
best_accuracy = p_best
x = x_best
else:
print "Best accuracy %f" % best_accuracy
return x7_best, x8_best, best_accuracy
break
iteration += 1
initial = False
x_best, p_best = seeding3(f1, f2, x, iteration, x2, x4, x5, x6)
if iteration%2!=0:
x8_best= x_best
print "x8 best so far:", x8_best
else:
x7_best = x_best
print "x7 best so far:", x7_best
def tune(f1, f2):
x2, x4 = sub_tuning1(f1, f2)
x5, x6 = sub_tuning2(f1, f2, x2, x4)
x7, x8, b = sub_tuning3(f1, f2, x2, x4, x5, x6)
return 1, x2, 1, x4, x5, x6, x7, x8, b
print tune("amp.fasta", "non_amp.fasta" )
fh.write(tune("amp.fasta", "non_amp.fasta" ))
fh.close()