-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_on_real_data.py
executable file
·181 lines (140 loc) · 5.08 KB
/
run_on_real_data.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
#!/usr/bin/python
import os
import sys
import math
import time
import random
from optparse import OptionParser
import make_svm_data as make_d
LIBSVM_PATH = '/home/schudoma/tools/libsvm-3.1/python'
if not os.path.exists(LIBSVM_PATH):
sys.stderr.write('Missing LIBSVM_PATH. Aborting.\n')
sys.exit(1)
sys.path.append(LIBSVM_PATH)
import svmutil
import svm
C_RANGE = -5, 15, 2
GAMMA_RANGE = 3, -15, -2
N_RUNS = 1000
TIMESTAMP = ''
KERNEL_TYPE = 'LINEAR'
RANDOMIZE_DATA = False
def init(argv):
global TIMESTAMP
TIMESTAMP = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime())
parser = OptionParser()
parser.add_option('-k', '--kernel', dest='kernel_type')
parser.add_option('-n', type='int', dest='n_runs')
parser.add_option('--random', dest='randomize_data', action='store_true')
options, args = parser.parse_args(argv)
global KERNEL_TYPE
if options.kernel_type is not None:
KERNEL_TYPE = options.kernel_type
global RANDOMIZE_DATA
if options.randomize_data is not None:
RANDOMIZE_DATA = options.randomize_data
global N_RUNS
if options.n_runs is not None:
N_RUNS = options.n_runs
return None
###
def grid_search(y, x, param, grid, cv_func, n, c_range, gamma_range):
cstart, cend, cstep = c_range
gstart, gend, gstep = gamma_range
for c in xrange(cstart, cend + 1, cstep):
param.C = 2.0 ** c
for gamma in xrange(gstart, gend - 1, gstep):
param.gamma = 2.0 ** gamma
key = (c, gamma)
grid[key] = grid.get(key, []) + cv_func(y, x, param, n=n)
return grid
###
def leave_one_out(y, x, param, n='DUMMY'):
results = []
for i, test in enumerate(zip(y, x)):
training_y = y[:i] + y[i+1:]
training_x = x[:i] + x[i+1:]
problem = svm.svm_problem(training_y, training_x)
model = svmutil.svm_train(problem, param, '-q')
result = svmutil.svm_predict(y[i:i+1], x[i:i+1], model, '-b 1')
results.append(result + (test[0], make_d.decode(x[i], make_d.decode_dic)))
return results
###
def compute_accuracy(results):
return sum(map(float, map(lambda x: x[0]==x[1], results)))/len(results)
###
def main(argv):
global C_RANGE
global GAMMA_RANGE
init(argv[2:])
fn = argv[0]
dataset = make_d.read_data(open(fn))
items = dataset.items()
keys = [float(x[0].split('_')[0][3:]) for x in items]
dataset = zip(keys, [v[1] for v in items])
data = make_d.prepare_data(dataset)
print data.keys(), [len(v) for v in data.values()]
param = svm.svm_parameter('-b 1')
if KERNEL_TYPE == 'LINEAR':
param.kernel_type = svm.LINEAR
GAMMA_RANGE = 1, 0, -2
else:
param.kernel_type = svm.RBF
fn_test = argv[1]
testdata = make_d.read_data(open(fn_test))
testitems = testdata.items()
testkeys = [float(x[0].split('_')[0][3:]) for x in testitems]
testdataset = zip(testkeys, [v[1] for v in testitems])
testdata = make_d.prepare_data(testdataset)
cvfunc = leave_one_out
n_cv = None
outfile = os.path.basename(fn)
outfile = outfile.replace('.fasta', '')
outfile = outfile.replace('.fas', '')
log_name = '%s-%s-%i-%s.csv' % (TIMESTAMP,
KERNEL_TYPE,
int(RANDOMIZE_DATA),
outfile)
logfile = open(log_name, 'w')
i = 0
param_grid = {}
results = []
sum_acc = 0
sets = make_d.make_set(data, balanced_set=False, training_fraction=1.0)
train_y, train_x, test_y, test_x = sets
train_x = [make_d.encode(x, make_d.encode_dic) for x in train_x]
testsets = make_d.make_set(testdata, balanced_set=False,
training_fraction=0.0)
dummy0, dummy1, test_y, test_x = testsets
test_x = [make_d.encode(x, make_d.encode_dic) for x in test_x]
param_grid = {}
param_grid = grid_search(train_y, train_x, param, param_grid,
leave_one_out, n_cv, C_RANGE, GAMMA_RANGE)
ranking = []
for k, v in param_grid.items():
recognized = [v_i[0][0] == v_i[3] for v_i in v]
recog_rate = sum(map(int, recognized))/float(len(recognized))
ranking.append((recog_rate, k))
ranking.sort()
param.C, param.gamma = map(lambda x: 2**x, ranking[-1][1])
problem = svm.svm_problem(train_y, train_x)
model = svmutil.svm_train(problem, param, '-q')
result = svmutil.svm_predict(test_y, test_x, model, '-b 1')
print result
"""
cur_result = zip(result[0], test_y)
cur_acc = compute_accuracy(cur_result)
results.extend(cur_result)
total_acc = compute_accuracy(results)
sum_acc += cur_acc
mean_acc = sum_acc/(i+1)
# print cur_acc, mean_acc, total_acc
logfile.write('%f,%f,%f\n' % (cur_acc, mean_acc, total_acc))
print 'ACC', compute_accuracy(results)
"""
logfile.close()
return None
if __name__ == '__main__': main(sys.argv[1:])
"""
2011-11-18, 14:27, random label permutation, 3-class, Accuracy: 32.8%
"""