-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
165 lines (148 loc) · 4.62 KB
/
utils.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import sys
import numpy as np
import csv
def predict(result, path, user_id, domain='course'):
output = []
for i in range(len(result)):
pred = ""
for j in result[i]:
pred += (str(j) + " ")
pred = pred.strip()
output.append([user_id[i], pred])
if domain == 'course':
head = ["user_id", "course_id"]
else:
head = ["user_id", "subgroup"]
with open(path, "w") as f:
writer = csv.writer(f)
writer.writerow(head)
writer.writerows(output)
def predict_topic_from_course(result, user_idx, known_topic, course2subgroup):
pred_boost = []
for idx, i in enumerate(result):
weight = dict()
for j in i:
try:
topic = course2subgroup[str(j)]
except:
topic = [0]
for k in topic:
if k in weight:
weight[k] += 1
else:
weight[k] = 1
secret = known_topic[user_idx[idx]]
key = list(weight.keys())
val = list(weight.values())
pred = [
key[j]
for j in np.argsort(np.array(val))[::-1]
if key[j] not in secret
]
pred_boost.append(secret+pred)
return pred_boost
def predict_topic_from_course_no_secret(result, course2subgroup):
pred_boost = []
for idx, i in enumerate(result):
weight = dict()
for j in i:
try:
topic = course2subgroup[str(j)]
except:
topic = [0]
for k in topic:
if k in weight:
weight[k] += 1
else:
weight[k] = 1
key = list(weight.keys())
val = list(weight.values())
pred = [
key[j]
for j in np.argsort(np.array(val))[::-1]
if key[j]
]
pred_boost.append(pred)
return pred_boost
def knn_predict_course(indices, user_idx, known_course):
pred = []
for i in user_idx:
known = known_course[i]
weight = dict()
for j in indices[i][1:]:
for k in known_course[j]:
if k not in known and k in weight:
weight[k] += 1
elif k not in known and k not in weight:
weight[k] = 1
else:
continue
key = list(weight.keys())
val = list(weight.values())
pred.append([
key[j]
for j in np.argsort(np.array(val))[::-1]
])
return pred
def knn_predict_topic(indices, user_idx, known_topic):
pred_boost = []
for i in user_idx:
weight = dict()
for j in indices[i][1:]:
for k in known_topic[j]:
if k in weight:
weight[k] += 1
elif k not in weight:
weight[k] = 1
else:
continue
secret = known_topic[i]
key = list(weight.keys())
val = list(weight.values())
pred = [
key[j]
for j in np.argsort(np.array(val))[::-1]
if key[j] not in secret
]
pred_boost.append(secret+pred)
return pred_boost
def mix2_rearrange(base, ref1):
pred = []
for (idx, i) in enumerate(base):
weight = dict()
ref = ref1[idx]
for idx_j, j in enumerate(i):
weight[j] = idx_j
if j in ref:
weight[j] += (np.where(np.array(ref) == j)[0][0])
else:
weight[j] += len(ref)
key = list(weight.keys())
val = list(weight.values())
pred.append([
key[j]
for j in np.argsort(np.array(val))
])
return pred
def mix3_rearrange(base, ref1, ref2):
pred = []
for (idx, i) in enumerate(base):
weight = dict()
ref_1, ref_2 = ref1[idx], ref2[idx]
for idx_j, j in enumerate(i):
weight[j] = idx_j
if j in ref_1 and j in ref_2:
weight[j] += (np.where(np.array(ref_1) == j)[0][0] + np.where(np.array(ref_2) == j)[0][0])
elif j in ref_1:
weight[j] += (np.where(np.array(ref_1) == j)[0][0] + len(ref_2))
elif j in ref_2:
weight[j] += (len(ref_1) + np.where(np.array(ref_2) == j)[0][0])
else:
weight[j] += (len(ref_1) + len(ref_2))
key = list(weight.keys())
val = list(weight.values())
pred.append([
key[j]
for j in np.argsort(np.array(val))
])
return pred