-
Notifications
You must be signed in to change notification settings - Fork 9
/
sample_submission.py
60 lines (37 loc) · 1.34 KB
/
sample_submission.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
"""
This file generates the sample_submission.csv baseline.
Ensure that the 'public_data' is in the same directory
as this file.
"""
import pandas as pd
import json
import numpy as np
import os
"""
Load the required metadata
"""
annotation_names = json.load(open(os.path.join('public_data', 'annotations.json')))
"""
Calculate training training class distribution
"""
prior_probs = np.zeros(len(annotation_names))
for ii in xrange(1, 11):
df = pd.read_csv(os.path.join('public_data', 'train', str(ii).zfill(5), 'targets.csv'))
non_nans = df[df.isnull().any(axis=1) == False]
prior_probs += np.asarray(non_nans.mean(axis=0)[annotation_names].tolist())
prior_probs /= prior_probs.sum()
prior_probs = prior_probs.tolist()
"""
Generate submission file
"""
se_cols = ['start', 'end']
with open(os.path.join('public_data', 'sample_submission.csv'), 'w') as fil:
for te_ind_str in sorted(os.listdir(os.path.join('public_data', 'test'))):
te_ind = int(te_ind_str)
meta = json.load(open(os.path.join('public_data', 'test', te_ind_str, 'meta.json')))
starts = range(meta['end'])
ends = range(1, meta['end'] + 1)
for start, end in zip(starts, ends):
row = [te_ind, start, end] + prior_probs
fil.write(','.join(map(str, row)))
fil.write('\n')