-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaselines.py
233 lines (205 loc) · 8.64 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import os
# for ALS MF
os.environ["OMP_NUM_THREADS"]="1"
os.environ["OPENBLAS_NUM_THREADS"]="1"
os.environ["MKL_NUM_THREADS"]="1"
os.environ["NUMEXPR_NUM_THREADS"]="1"
from math import ceil
from tqdm import tqdm
import numpy as np
import pandas as pd
from datasets.utils import *
from sklearn.neighbors import NearestNeighbors
import scipy.sparse as sp
import implicit
import torch
class TopPopularRecommender:
def __init__(self, X, item_idx):
self.item_idx = item_idx
self.popularities = np.array(X.sum(0))[0]
def fit(*args, **kwargs):
self.__init__(*args, **kwargs)
def predict_df(self, df, n=100, batch_size=1000):
X_test = get_sparse_matrix_from_dataframe(df, item_indices=self.item_idx)
n_batches = ceil(X_test.shape[0]/batch_size)
uids = df.user_id.cat.categories.to_numpy()
dfs=[]
for i in tqdm(range(n_batches)):
i_min = i*batch_size
i_max = i_min+batch_size
batch=X_test[i_min:i_max]
indices = np.argsort((1-batch.toarray())*self.popularities)[:,-n:]
indices = indices[:,::-1]
item_ids = self.item_idx.to_numpy()[indices]
batch_user_ids = uids[i_min:i_max]
values = self.popularities[indices]
df=pd.DataFrame({
"user_id": batch_user_ids,
"item_id": list(item_ids),
"value": list(values)
})
df = df.explode(["item_id", "value"])
df["item_id"] = df["item_id"].astype(str).astype("category")
df["user_id"] = df["user_id"].astype(str).astype("category")
dfs.append(df)
return pd.concat(dfs)
class KNNRecommender:
def __init__(self, X, item_idx, neighbors):
self.item_idx = item_idx
self.X = X
self.model = NearestNeighbors(algorithm="brute", n_neighbors=neighbors, metric="cosine")
self.model.fit(X)
self.neighbors =neighbors
def predict(self, X, **kwargs):
if X.count_nonzero() == 0:
return np.random.uniform(size=X.shape)
if kwargs.get("neighbors"):
n_distances, n_indices = self.model.kneighbors(X, n_neighbors=kwargs.get("neighbors"))
else:
n_distances, n_indices = self.model.kneighbors(X)
n_distances = 1 - n_distances
sums = n_distances.sum(axis=1)
n_distances = n_distances / sums[:, np.newaxis]
def f(dist, idx):
A = self.X[idx]
D = sp.diags(dist)
return D.dot(A).sum(axis=0)
vf = np.vectorize(f, signature="(n),(n)->(m)")
X_predict = vf(n_distances, n_indices)
X_predict[X.nonzero()] = 0
X_predict = np.array(X_predict)
#filt = (1-X.toarray())
return X_predict#*filt
def predict_df(self, df, n=100, batch_size=1000, neighbors=None):
if neighbors is None:
neighbors = self.neighbors
X_test = get_sparse_matrix_from_dataframe(df, item_indices=self.item_idx)
n_batches = ceil(X_test.shape[0]/batch_size)
uids = df.user_id.cat.categories.to_numpy()
dfs=[]
for i in tqdm(range(n_batches)):
i_min = i*batch_size
i_max = i_min+batch_size
batch=X_test[i_min:i_max]
preds=self.predict(batch, neighbors=neighbors)
preds=preds*(1-batch.toarray())
preds=torch.from_numpy(preds)
batch_uids = uids[i_min:i_max]
values_, indices_ = torch.topk(preds.to("cpu"), n)
df = pd.DataFrame({"user_id": np.stack([batch_uids]*n).flatten("F"), "item_id": np.array(
self.item_idx)[indices_].flatten(), "value": values_.flatten()})
df["user_id"] = df["user_id"].astype(str).astype('category')
df["item_id"] = df["item_id"].astype(str).astype('category')
dfs.append(df)
"""
#indices = np.argsort((1-batch.toarray())*preds)[:,-n:]
indices = np.argsort(preds)[:,-n:]
indices = indices[:,::-1]
item_ids = self.item_idx.to_numpy()[indices]
batch_user_ids = uids[i_min:i_max]
bb = []
for i in range(preds.shape[0]):
bb.append(preds[i][indices[i]])
values =np.vstack(bb)
df=pd.DataFrame({
"user_id": batch_user_ids,
"item_id": list(item_ids),
"value": list(values)
})
df = df.explode(["item_id", "value"])
df["item_id"] = df["item_id"].astype(str).astype("category")
df["user_id"] = df["user_id"].astype(str).astype("category")
dfs.append(df)
"""
return pd.concat(dfs)
class ALSMatrixFactorizer():
def __init__(self, factors: int, regularization: float, iterations: int, use_gpu: bool, item_idx, num_threads=0):
self.model = None
self.factors = factors
self.regularization = regularization
self.iterations = iterations
self.use_gpu = use_gpu
self.item_idx = item_idx
self.num_threads = num_threads
def name(self):
return "MF"
def fit(self, X, training=False):
self.model = implicit.als.AlternatingLeastSquares(
factors=self.factors,
regularization=self.regularization,
iterations=self.iterations,
use_gpu=self.use_gpu,
num_threads=self.num_threads,
)
self.model.fit(X)
def predict(self, X, **kwargs):
if not isinstance(X, scipy.sparse.csr_matrix):
X = scipy.sparse.csr_matrix(X)
if X.count_nonzero() == 0:
return np.random.uniform(size=X.shape)
recommended_item_ids, scores = self.model.recommend(
np.arange(X.shape[0]), X,
recalculate_user=True, filter_already_liked_items=False,
N=X.shape[1]
)
predicted_scores = np.zeros((X.shape[0], X.shape[1]))
np.put_along_axis(predicted_scores, recommended_item_ids, scores, axis=1)
min_score = scores.min()
if min_score < 0:
scores += abs(min_score) + 0.1
predicted_scores[X.nonzero()] = 0 # Just to be sure
return predicted_scores
def predict_df(self, df, k=100, batch_size=1000):
X_test = get_sparse_matrix_from_dataframe(df, item_indices=self.item_idx)
n_batches = ceil(X_test.shape[0]/batch_size)
uids = df.user_id.cat.categories.to_numpy()
dfs=[]
for i in tqdm(range(n_batches)):
i_min = i*batch_size
i_max = i_min+batch_size
batch=X_test[i_min:i_max]
preds=self.predict(batch)
preds=preds*(1-batch.toarray())
preds=torch.from_numpy(preds)
batch_uids = uids[i_min:i_max]
values_, indices_ = torch.topk(preds.to("cpu"), k)
df = pd.DataFrame({"user_id": np.stack([batch_uids]*k).flatten("F"), "item_id": np.array(
self.item_idx)[indices_].flatten(), "value": values_.flatten()})
df["user_id"] = df["user_id"].astype(str).astype('category')
df["item_id"] = df["item_id"].astype(str).astype('category')
dfs.append(df)
return pd.concat(dfs)
class EASERecommender:
def __init__(self, item_idx, lambda_):
self.item_idx = item_idx
self.lambda_ = lambda_
def fit(self, X):
G = X.T.dot(X).toarray()
diagIndices = np.diag_indices(G.shape[0])
G[diagIndices] += self.lambda_
P = np.linalg.inv(G)
B = P / (-np.diag(P))
B[diagIndices] = 0
self.B = B
def predict(self, X):
return np.dot(X, self.B)
def predict_df(self, df, k=100, batch_size=1000):
X_test = get_sparse_matrix_from_dataframe(df, item_indices=self.item_idx)
n_batches = ceil(X_test.shape[0]/batch_size)
uids = df.user_id.cat.categories.to_numpy()
dfs=[]
for i in tqdm(range(n_batches)):
i_min = i*batch_size
i_max = i_min+batch_size
batch=X_test[i_min:i_max].toarray()
preds=self.predict(batch)
preds=preds*(1-batch)
preds=torch.from_numpy(preds)
batch_uids = uids[i_min:i_max]
values_, indices_ = torch.topk(preds.to("cpu"), k)
df = pd.DataFrame({"user_id": np.stack([batch_uids]*k).flatten("F"), "item_id": np.array(
self.item_idx)[indices_].flatten(), "value": values_.flatten()})
df["user_id"] = df["user_id"].astype(str).astype('category')
df["item_id"] = df["item_id"].astype(str).astype('category')
dfs.append(df)
return pd.concat(dfs)