-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathktsp.Age.py
304 lines (250 loc) · 6.86 KB
/
ktsp.Age.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
#coding: utf-8
#July 23 2017
import time
import sys
import copy
import itertools
import cPickle as pickle
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontManager
from pylab import mpl
import subprocess
def get_matplot_zh_font():
fm = FontManager()
mat_fonts = set(f.name for f in fm.ttflist)
output = subprocess.check_output('fc-list :lang=zh -f "%{family}\n"', shell=True)
zh_fonts = set(f.split(',', 1)[0] for f in output.split('\n'))
available = list(mat_fonts & zh_fonts)
print '*' * 10, '可用的字体', '*' * 10
for f in available:
print f
return available
def set_matplot_zh_font():
available = get_matplot_zh_font()
if len(available) > 0:
mpl.rcParams['font.sans-serif'] = [available[0]] # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
def read_data(path):
with open(path + ".pickle", "r") as fp:
obj = pickle.load(fp)
print len(obj), path + " elements load over.", time.ctime()
return obj
def age_beta_D(line, GSM_info):
gsm = line[0]
age = GSM_info[gsm][1]
#print age
if age < 60:
label = "ABC"
else:
label = "D"
return label
def age_distribution(matrix):
count = {'ABC': 0, 'D': 0}
for line in matrix:
beta = age_beta_D(line, GSM_info)
count[beta] += 1
return count
def ktsp(matrix):
matrix = list(matrix)
#matrix = [e for e in matrix if matrix.index(e)%3!=k]
print len(matrix)
k = 3
c0, c1 = "ABC", "D"
tops = [[0] * 3 for i in range(100)]
L = len(matrix[0])
for i in range(1, L, 47):
#print i, time.ctime()
for j in range(i+1, L):
e = [i, j]
hit, nohit = {'ABC': 0, 'D': 0}, {'ABC': 0, 'D': 0}
#hit, nohit = {'M': 0, 'F': 0}, {'M': 0, 'F': 0}
for line in matrix:
beta = age_beta_D(line, GSM_info)
if line[i] < line[j]:
hit[beta] += 1
else:
nohit[beta] += 1
pijc0 = 1.0 * hit[c0] / (hit[c0] + nohit[c0])
pjic0 = 1.0 * nohit[c0] / (hit[c0] + nohit[c0])
pijc1 = 1.0 * hit[c1] / (hit[c1] + nohit[c1])
pjic1 = 1.0 * nohit[c1] / (hit[c1] + nohit[c1])
score = abs(pijc0 - pijc1)
if pijc0 > pijc1:
temp = [c0, e, score]
else:
temp = [c1, e, score]
if temp[-1] > tops[-1][-1]:
tops[-1] = temp
tops.sort(key=lambda x:x[-1], reverse=True)
#print top
#print pijc0, pjic0, pijc1, pjic1
final = []
final.append(tops[0])
s = set(tops[0][1])
tops = [e for e in tops if e[1][0] not in s and e[1][1] not in s]
tops.sort(key=lambda x:x[-1], reverse=True)
final.append(tops[0])
s = s | set(tops[0][1])
tops = [e for e in tops if e[1][0] not in s and e[1][1] not in s]
tops.sort(key=lambda x:x[-1], reverse=True)
final.append(tops[0])
print final, time.ctime()
return final
def sampling(data, n):
matrix = read_data(data)
subsum = age_distribution(matrix)
expect = {}
expect["ABC"] = n * 1.0 * subsum["ABC"] / len(matrix)
expect["D"] = n * 1.0 * subsum["D"] / len(matrix)
count = {}
m = []
for line in matrix:
beta = age_beta_D(line, GSM_info)
if count.get(beta):
if count[beta] <= expect[beta]:
m.append(line)
count[beta] += 1
else:
m.append(line)
count[beta] = 1
if len(m) >= n:
break
print count
return m
def timing():
css = []
t = []
for i in [1000, 2000, 4000, 6000, 8000, 10000]:
#i = int(i * 0.8)
sample = sampling("age_train", i)
start = time.clock()
cs = ktsp(sample)
elapsed = time.clock() - start
t.append(elapsed * 47)
css.append(cs)
with open("ktsp.pairs.pickle", "w") as fp:
pickle.dump(css, fp)
with open("ktsp.timing.pickle", "w") as fp:
pickle.dump(t, fp)
plt.plot([1000, 2000, 4000, 6000, 8000, 10000], t, "-")
plt.ylabel("CPU time(second)")
plt.xlabel("# of samples")
plt.grid(True)
plt.show()
def accuracy(matrix, tsp):
tc, fc = 0.0, 0.0
for line in matrix:
real = age_beta_D(line, GSM_info)
if line[tsp[1][0]] < line[tsp[1][1]]:
predict = tsp[0]
else:
if tsp[0] == "ABC":
predict = "D"
else:
predict = "ABC"
if real == predict:
tc += 1
else:
fc += 1
acc = tc / (tc + fc)
return acc
def accuracy2(matrix, ktsp):
tc, fc = 0.0, 0.0
for line in matrix:
real = age_beta_D(line, GSM_info)
#score = {'M': 0, 'F': 0}
score = {'ABC': 0, 'D': 0}
for tsp in ktsp:
if line[tsp[1][0]] < line[tsp[1][1]]:
predict = tsp[0]
else:
if tsp[0] == "ABC":
predict = "D"
else:
predict = "ABC"
score[predict] += 1
predict = max(score, key=score.get)
if real == predict:
tc += 1
else:
fc += 1
acc = tc / (tc + fc)
return acc
def acc():
cs = read_data("ktsp.pairs")
j = 0
t1, t2 = [], []
#sample = sampling("age_test", 2000)
sample = read_data("age_test")
for i in [1000, 2000, 4000, 6000, 8000, 10000]:
#i = int(i * 0.2)
print cs[j]
acc2 = accuracy2(sample, cs[j])
t2.append(acc2)
j += 1
with open("acc.ktsp.pickle", "w") as fp:
pickle.dump(t2, fp)
plt.plot([1000, 2000, 4000, 6000, 8000, 10000], t2, "-")
plt.ylabel("Accuracy")
plt.xlabel("# of samples")
plt.grid(True)
plt.show()
def compare_t():
t1 = read_data("pop.timing")
t2 = read_data("ktsp.timing")
t3 = read_data("svm_rfe.timing")
'''
fig, ax1 = plt.subplots()
ax1.plot([1000, 2000, 4000, 6000, 8000, 10000], t1, 'b-')
ax1.set_xlabel('# of samples')
# Make the y-axis label, ticks and tick labels match the line color.
ax1.set_ylabel('CPU time(second)', color='b')
ax1.tick_params('y', colors='b')
ax2 = ax1.twinx()
ax2.plot([1000, 2000, 4000, 6000, 8000, 10000], [e/3600 for e in t2], 'r-')
ax2.set_ylabel('CPU time(h)', color='r')
ax2.tick_params('y', colors='r')
fig.tight_layout()
plt.show()
'''
fig, ax = plt.subplots()
ax.plot([1000, 2000, 4000, 6000, 8000, 10000], t1, "-", label="k-ppt")
ax.plot([1000, 2000, 4000, 6000, 8000, 10000], [e/3 for e in t2], "-", label="k-tsp")
plt.plot([1000, 2000, 4000, 6000, 8000, 10000], t3, "-", label="svm-rfe")
ax.set_ylabel(u"CPU时间/秒")
ax.set_yscale("log")
ax.set_xlabel(u"样本数量")
plt.grid(True)
plt.legend()
plt.show()
def compare_a():
t1 = read_data("acc.pop")
t2 = read_data("acc.ktsp")
t3 = read_data("acc.svm_rfe")
print t1[-1], t2[-1], t3[-1]
plt.ylim((0,1))
plt.plot([1000, 2000, 4000, 6000, 8000, 10000], t1, "-", label="k-pop")
plt.plot([1000, 2000, 4000, 6000, 8000, 10000], t2, "-", label="k-tsp")
plt.plot([1000, 2000, 4000, 6000, 8000, 10000], t3, "-", label="svm-rfe")
plt.plot([1000, 2000, 4000, 6000, 8000, 10000], t1, "ko")
plt.plot([1000, 2000, 4000, 6000, 8000, 10000], t2, "ko")
plt.plot([1000, 2000, 4000, 6000, 8000, 10000], t3, "ko")
plt.ylabel(u"分类准确度")
plt.xlabel(u"样本数量")
plt.grid(True)
plt.legend()
plt.show()
def test():
cs = read_data("ktsp.pairs")
for c in cs:
print c
if __name__ == "__main__":
print "Start.", time.ctime()
set_matplot_zh_font()
GSM_info = read_data("GSM_info")
#tsp()
#timing()
#acc()
#test()
compare_a()
print "End.", time.ctime()