-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_prior_weighted_csv.py
56 lines (41 loc) · 1.8 KB
/
generate_prior_weighted_csv.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
#!/usr/bin/env python
import argparse
import csv
import gzip
import neukrill_net.utils
import os
def main(csv_path, settings, weight = 0.01):
csv_path = os.path.abspath(csv_path)
csv_in = gzip.open(csv_path, 'rb')
reader = csv.reader(csv_in, delimiter = ',')
with gzip.open(csv_path.split('.')[0] + '_prior_weighted.csv.gz', 'w') as csv_out:
writer = csv.writer(csv_out, delimiter = ',')
# Write a header row
header = reader.next()
writer.writerow(header)
for j, row in enumerate(reader):
row_out = []
# Write image name
row_out.append(row[0])
# Iterate over predictions
for i in range(1, len(row)):
prediction = ((1 - weight) * float(row[i])) + (weight * settings.class_priors[i-1])
row_out.append(prediction)
if (j % 1000 == 0):
print j
# Write row
writer.writerow(row_out)
csv_in.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description = 'Generate a submission file'
'from an existing one with'
'predictions weighted by'
'class priors.')
parser.add_argument('csv_path', type = str, nargs = '?',
help = 'Path to csv file.')
parser.add_argument('--weight', nargs = '?', help = 'Weight to weight the'
'prediction by the class prior.', type = float,
default = 0.01)
args = parser.parse_args()
settings = neukrill_net.utils.Settings("settings.json")
main(args.csv_path, settings, weight = args.weight)