Browse Source

Update algorithm by request

master
sipp11 11 years ago
parent
commit
ef5eabed2b
  1. 14
      s1.py
  2. 62
      s2.py
  3. 38
      s3.py

14
s1.py

@ -24,7 +24,7 @@ def get_rank_thing(filename, **kwargs):
reader = csv.reader(f) reader = csv.reader(f)
reader.next() reader.next()
for r in reader: for r in reader:
mn, cnt = r[0], r[9] mn, rank_no = r[0], r[9]
w = [r[10], r[11], r[12], r[13], r[14]] w = [r[10], r[11], r[12], r[13], r[14]]
q = ( q = (
('A', r[1]), ('B', r[2]), ('C', r[3]), ('D', r[4]), ('A', r[1]), ('B', r[2]), ('C', r[3]), ('D', r[4]),
@ -36,10 +36,10 @@ def get_rank_thing(filename, **kwargs):
if result: if result:
# print(u'%s > %s' % (r[0], list(result))) # print(u'%s > %s' % (r[0], list(result)))
count += 1 count += 1
owriter.writerow([mn, ','.join(list(result)), cnt] + w) owriter.writerow([mn, ','.join(list(result)), rank_no] + w)
if cnt not in model_count[mn]: if rank_no not in model_count[mn]:
model_count[mn][cnt] = set() model_count[mn][rank_no] = set()
model_count[mn][cnt] = model_count[mn][cnt].union(result) model_count[mn][rank_no] = model_count[mn][rank_no].union(result)
print(u'[%s]\n Count = %s | Total = %s | Lo_w_match = %s' % ( print(u'[%s]\n Count = %s | Total = %s | Lo_w_match = %s' % (
filename, count, tot, lo_match)) filename, count, tot, lo_match))
return model_count return model_count
@ -56,7 +56,7 @@ def main(*argv):
x[i] = mc[i] x[i] = mc[i]
# print('x: ', x) # print('x: ', x)
mx = [set(j) for i, j in x.items()] mx = [set(j) for i, j in x.items()]
order = sorted(list(set.union(*mx)), reverse=True) order = sorted(list(set.union(*mx)))
sum_file = '%s-result.csv' % fs[f] sum_file = '%s-result.csv' % fs[f]
with open(os.path.join(os.getcwd(), 'build', sum_file), 'wb') as o: with open(os.path.join(os.getcwd(), 'build', sum_file), 'wb') as o:
result_writer = csv.writer(o, delimiter=',', quotechar='"', result_writer = csv.writer(o, delimiter=',', quotechar='"',
@ -64,7 +64,7 @@ def main(*argv):
m = {} m = {}
# print(j, ':', order) # print(j, ':', order)
for i in xrange(0, len(order)): for i in xrange(0, len(order)):
result_writer.writerow(['count', order[i]]) result_writer.writerow(['rank_no', order[i]])
# print('count , ', order[i], ' / ', type(order[i])) # print('count , ', order[i], ' / ', type(order[i]))
for j in x.keys(): for j in x.keys():
try: try:

62
s2.py

@ -14,17 +14,11 @@ xmodels = {
'CFSf': {}, 'CFSD': {}, 'CFHf': {}, 'CFHD': {}, 'PMSf': {}, 'PMSD': {}, 'CFSf': {}, 'CFSD': {}, 'CFHf': {}, 'CFHD': {}, 'PMSf': {}, 'PMSD': {},
'PMHf': {}, 'PMHD': {}, 'PFSf': {}, 'PFSD': {}, 'PFHf': {}, 'PFHD': {}, 'PMHf': {}, 'PMHD': {}, 'PFSf': {}, 'PFSD': {}, 'PFHf': {}, 'PFHD': {},
} }
rk_conv = { '''
'I': ['4', '3', '2', ''], แกตรงน >> RANK_ORDER << าจะเพ rank ใสอะไรกใสไปตามลำด
'P': ['4', '3', '2', ''], '''
'C': ['4', '3', '2', ''], RANK_ORDER = ['1', '2', '3', '4']
'F': ['5', '4', '3', '2', ''],
'M': ['5', '4', '3', '2', ''],
'H': ['4', '3', '2', ''],
'S': ['4', '3', '2', ''],
'f': ['4', '3', '2', ''],
'D': ['4', '3', '2', ''],
}
fs = ('Case1_LS.csv', 'Case1_Gender.csv', 'Case1_Level.csv', 'Case1_SciF.csv') fs = ('Case1_LS.csv', 'Case1_Gender.csv', 'Case1_Level.csv', 'Case1_SciF.csv')
@ -38,11 +32,11 @@ def process_s2_data():
with open(os.path.join(os.getcwd(), 'build', f_name), 'rb') as f: with open(os.path.join(os.getcwd(), 'build', f_name), 'rb') as f:
rows = csv.reader(f) rows = csv.reader(f)
for r in rows: for r in rows:
if r[0] in ('count', 'All Result'): if r[0] in ('rank_no', 'All Result'):
step = r[0] step = r[0]
rank = r[1] if r[0] == 'count' else '' rank = r[1]
continue continue
if step == 'count': if step == 'rank_no':
if r[0] not in rank_result: if r[0] not in rank_result:
rank_result[r[0]] = {} rank_result[r[0]] = {}
rank_result[r[0]][rank] = r[1].split(',') rank_result[r[0]][rank] = r[1].split(',')
@ -53,6 +47,7 @@ def process_s2_data():
# print('exists:', r[1], '|', step, '|', rank) # print('exists:', r[1], '|', step, '|', rank)
pass pass
sum_result[r[0]] = r[1].split(',') sum_result[r[0]] = r[1].split(',')
# print(rank_result['C'].keys())
return { return {
'sum': sum_result, 'rank': rank_result 'sum': sum_result, 'rank': rank_result
} }
@ -71,46 +66,19 @@ def produce_s2_part1(sum_result):
create_csv(_f, rows, directory='part2') create_csv(_f, rows, directory='part2')
def produce_s2_part2_old(rank_result): def produce_s2_part2(rank_result):
# create xxxx-2.csv # create xxxx-2.csv
for ii in xmodels.keys(): for ii in xmodels.keys():
rows = [] rows = []
# get rank count first for rank in RANK_ORDER:
ro = set() # print(ii, ':rank:', rank)
for rk in xrange(0, 4): rows.append(['Rank#%s' % rank, 'rank', rank])
for i in ii:
if i not in rank_result:
continue
ro = ro.union(set(rank_result[i].keys()))
rank_no = 1
for rk in sorted(list(ro), reverse=True):
# print(ii, ':', rk)
rows.append(['Rank#%s' % rank_no, 'rank', rank_no, 'count', rk])
rank_no += 1
for i in ii: for i in ii:
# find order
if i not in rank_result: if i not in rank_result:
continue continue
if rk in rank_result[i]: if rank not in rank_result[i]:
rows.append([i, ','.join(rank_result[i][rk])])
_f = '%s-2.csv' % ii
create_csv(_f, rows, directory='part2')
def produce_s2_part2(rank_result):
# create xxxx-2.csv
for ii in xmodels.keys():
rows = []
for rk in xrange(0, 4):
rank_no = rk + 1
# print(ii, ':rank:', rank_no)
rows.append(['Rank#%s' % rank_no, 'rank', rank_no])
for i in ii:
if not rk_conv[i][rk]:
continue continue
# print(i, '::', rk_conv[i][rk]) rows.append([i, ','.join(rank_result[i][rank])])
rows.append([i, ','.join(rank_result[i][rk_conv[i][rk]])])
_f = '%s-2.csv' % ii _f = '%s-2.csv' % ii
create_csv(_f, rows, directory='part2') create_csv(_f, rows, directory='part2')

38
s3.py

@ -8,6 +8,7 @@ from base import Lo, create_csv
import os import os
import csv import csv
import sys import sys
from s2 import RANK_ORDER
los = Lo() los = Lo()
@ -66,20 +67,21 @@ def produce_match_all_mra(xdata):
if j not in lo_count: if j not in lo_count:
lo_count[j] = 0 lo_count[j] = 0
for i in ii: for i in ii:
for rank in '1234': for rank in RANK_ORDER:
if i in xdata[ii][rank] and j in xdata[ii][rank][i]: if rank in xdata[ii] and i in xdata[ii][rank] \
and j in xdata[ii][rank][i]:
lo_count[j] += 1 lo_count[j] += 1
rows = [ rows = []
['4/4', ','.join([k for (k, v) in lo_count.items() if v == 4])], max_count = len(RANK_ORDER)
['3/4', ','.join([k for (k, v) in lo_count.items() if v == 3])], for n in xrange(max_count, 0, -1):
['2/4', ','.join([k for (k, v) in lo_count.items() if v == 2])], rows.append([
['1/4', ','.join([k for (k, v) in lo_count.items() if v == 1])], '%s/%s' % (n, max_count),
] ','.join([k for (k, v) in lo_count.items() if v == n])
])
_f = '%s-all.csv' % (ii,) _f = '%s-all.csv' % (ii,)
create_csv(_f, rows, directory='part3') create_csv(_f, rows, directory='part3')
def produce_match_rank_mra(xdata, rank): def produce_match_rank_mra(xdata, rank):
for ii in xmodels.keys(): for ii in xmodels.keys():
lo_count = {} lo_count = {}
@ -87,14 +89,16 @@ def produce_match_rank_mra(xdata, rank):
if j not in lo_count: if j not in lo_count:
lo_count[j] = 0 lo_count[j] = 0
for i in ii: for i in ii:
if i in xdata[ii][rank] and j in xdata[ii][rank][i]: if rank in xdata[ii] and i in xdata[ii][rank] and \
j in xdata[ii][rank][i]:
lo_count[j] += 1 lo_count[j] += 1
rows = [ rows = []
['4/4', ','.join([k for (k, v) in lo_count.items() if v == 4])], max_count = len(RANK_ORDER)
['3/4', ','.join([k for (k, v) in lo_count.items() if v == 3])], for n in xrange(max_count, 0, -1):
['2/4', ','.join([k for (k, v) in lo_count.items() if v == 2])], rows.append([
['1/4', ','.join([k for (k, v) in lo_count.items() if v == 1])], '%s/%s' % (n, max_count),
] ','.join([k for (k, v) in lo_count.items() if v == n])
])
_f = '%s-rank-%s.csv' % (ii, rank) _f = '%s-rank-%s.csv' % (ii, rank)
create_csv(_f, rows, directory='part3') create_csv(_f, rows, directory='part3')
@ -103,7 +107,7 @@ def main(*argv):
xmm = process_s2_data() xmm = process_s2_data()
produce_match_all(xmm) produce_match_all(xmm)
produce_match_all_mra(xmm) produce_match_all_mra(xmm)
for i in '1234': for i in RANK_ORDER:
produce_match_rank_mra(xmm, i) produce_match_rank_mra(xmm, i)

Loading…
Cancel
Save