forked from MLWave/Kaggle-Ensemble-Guide
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kaggle_rankavg.py
33 lines (30 loc) · 1.11 KB
/
kaggle_rankavg.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
from __future__ import division
from collections import defaultdict
from glob import glob
import sys
glob_files = sys.argv[1]
loc_outfile = sys.argv[2]
def kaggle_bag(glob_files, loc_outfile):
with open(loc_outfile,"wb") as outfile:
all_ranks = defaultdict(list)
for i, glob_file in enumerate( glob(glob_files) ):
file_ranks = []
print "parsing:", glob_file
for e, line in enumerate( open(glob_file) ):
if e == 0 and i == 0:
outfile.write( line )
elif e > 0:
r = line.strip().split(",")
file_ranks.append( (float(r[1]), e, r[0]) )
for rank, item in enumerate( sorted(file_ranks) ):
all_ranks[(item[1],item[2])].append(rank)
average_ranks = []
for k in sorted(all_ranks):
average_ranks.append((sum(all_ranks[k])/len(all_ranks[k]),k))
ranked_ranks = []
for rank, k in enumerate(sorted(average_ranks)):
ranked_ranks.append((k[1][0],k[1][1],rank/(len(average_ranks)-1)))
for k in sorted(ranked_ranks):
outfile.write("%s,%s\n"%(k[1],k[2]))
print("wrote to %s"%loc_outfile)
kaggle_bag(glob_files, loc_outfile)