-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducer5.py
34 lines (33 loc) · 956 Bytes
/
reducer5.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
#!/user/bin/python3
from operator import itemgetter
import sys
import numpy as np
import pandas as pd
from scipy import stats
current_ind = ''
current_label_list = []
current_dist_list = []
k = 15
for line in sys.stdin:
line = line.strip()
test_ind,dist_label = line.split('\t', 1)
label,dist = dist_label.split(', ')
if test_ind == current_ind:
if label not in current_label_list:
current_label_list.append(int(label))
current_dist_list.append(float(dist))
else :
continue
else:
sorted_ind = np.argsort(current_dist_list)[0:k-1]
sorted_labels = []
for i in sorted_ind[0:(k-1)]:
sorted_labels.append(current_label_list[i])
final_label = stats.mode(sorted_labels)[0]
print('%s\t%s' % (str(current_ind), str(final_label)))
current_ind = test_ind
current_label_list = []
current_label_list.append(label)
current_dist_list = []
current_dist_list.append(float(dist))
print('%s\t%s' % (str(test_ind), str(final_label)))