-
Notifications
You must be signed in to change notification settings - Fork 8
/
baselines.py
151 lines (128 loc) · 4.14 KB
/
baselines.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
import numpy as np
import gzip,pickle
import random,math
import operator
# Baselines: random-k and top-k
# INPUTS: fname= csv file with relevance scores, k= recommendation size
def generate_random_n_top_k(fname,k):
# relevance scores
V=np.loadtxt(fname,delimiter=',')
m=V.shape[0] #number of customers
n=V.shape[1] #number of producers
U=range(m) #Customers
P=range(n) #Producers
# baseline1 = top-k recommendations
B1={}
# baseline2 = random-k recommendations
B2={}
for u in U:
scores=V[u,:]
B1[u]=scores.argsort()[-k:][::-1]
B2[u]=random.sample(P,k)
# Saving the results in pickle format
f_out=gzip.open(fname[:-4]+"_top_k_"+str(k)+".pkl.gz","wb")
pickle.dump(B1,f_out,-1)
f_out.close()
f_out=gzip.open(fname[:-4]+"_random_k_"+str(k)+".pkl.gz","wb")
pickle.dump(B2,f_out,-1)
f_out.close()
# Baseline: mixedTR-k (mix of top-k/2 and random-k/2)
# INPUTS: fname= csv file with relevance scores, k= recommendation size
def generate_mixedTR_k(fname,k):
# relevance scores
V=np.loadtxt(fname,delimiter=',')
m=V.shape[0] #number of customers
n=V.shape[1] #number of producers
U=range(m) #Customers
P=range(n) #Producers
# mixedTR-k
B={}
for u in U:
scores=V[u,:]
l=int(math.ceil((k+0.0)/2))
half=scores.argsort()[-l:][::-1]
remaining_P=[]
for p in P:
if p not in half:
remaining_P.append(p)
other_half=random.sample(remaining_P,int(k-l))
B[u]=[]
# first half from top-k/2
for i in half:
B[u].append(i)
# second half from random-k/2
for i in other_half:
B[u].append(i)
# Saving the results in pickle format
f_out=gzip.open(fname[:-4]+"_mixedTR_k_"+str(k)+".pkl.gz","wb")
pickle.dump(B,f_out,-1)
f_out.close()
# Baseline: poorest-k
# INPUTS: fname= csv file with relevance scores, k= recommendation size
def generate_poorest_k(fname,k):
# relevance scores
V=np.loadtxt(fname,delimiter=',')
m=V.shape[0] #number of customers
n=V.shape[1] #number of producers
U=range(m) #Customers
P=range(n) #Producers
# Exposures
E={}
for p in P:
E[p]=0.0
# poorest-k
B={}
for u in U:
B[u]=[]
# greedy round robin of producer-centric allocation: poorest-k
for i in range(k):
for u in U:
# producers sorted based on increasing exposures and allocating the first feasible producer
prod_sorted=sorted(E.items(),key=operator.itemgetter(1))
for p_tuple in prod_sorted:
p=p_tuple[0]
if p not in B[u]:
E[p]+=1
B[u].append(p)
break
# Saving the results in pickle format
f_out=gzip.open(fname[:-4]+"_poorest_k_"+str(k)+".pkl.gz","wb")
pickle.dump(B,f_out,-1)
f_out.close()
# Baseline: mixedTP-k (mix of top-k/2 and poorest-k/2)
# INPUTS: fname= csv file with relevance scores, k= recommendation size
def generate_mixedTP_k(fname,k):
# relevance scores
V=np.loadtxt(fname,delimiter=',')
m=V.shape[0] #number of customers
n=V.shape[1] #number of producers
U=range(m) #Customers
P=range(n) #Producers
# Recommendations
B={}
# Exposures
E={}
for p in range(n):
E[p]=0.0
for u in U:
B[u]=[]
scores=V[u,:]
#top-k/2
top_half=scores.argsort()[-int(math.ceil((k+0.0)/2)):][::-1]
for p in top_half:
B[u].append(p)
# producers sorted based on increasing exposures and allocating the first feasible producer
prod_sorted=sorted(E.items(),key=operator.itemgetter(1))
prod_index=0
while len(B[u])<k:
p=prod_sorted[prod_index][0]
#print(p)
if p not in B[u]:
B[u].append(p)
prod_index+=1
for p in B[u]:
E[p]+=1.0
# Saving the results in pickle format
f_out=gzip.open(fname[:-4]+"_mixedTP_k_"+str(k)+".pkl.gz","wb")
pickle.dump(B,f_out,-1)
f_out.close()