def get_recommendation(self, u, b): s = utils.scores_sorter(self.model[u].T[0]) rac = [] j = 0 while (len(rac) < 500): if (s[j] not in self.test_users[u][:b]): rac.append(self.items_list[s[j]]) j += 1 return rac
def build_selection(k, p2s_test, global_popularity, t2t_id, S, p2t, lengths, test_seed, P, power): selection = {} for user in p2s_test[test_seed]: if (p2t[str(user)] == "" or p2t[str(user)] not in t2t_id): selection[user] = global_popularity[:k] else: row = S[t2t_id[p2t[str(user)]], :]**power / lengths selection[user] = utils.scores_sorter(P.dot(row), k) print("selection built") utils.fl() return selection
def get_recommendation(self, u, b): s = utils.scores_sorter(self.model[u]) rac = [] j = 0 while (len(rac) < 500): if (s[j] not in self.test_users[u][:b]): rac.append(self.items_list[s[j]]) j += 1 return rac def get_selection(self, u, b, k): s = utils.scores_sorter(self.model[u], k + b) rac = [i for i in s if i not in self.test_users[u][:b]][:k] return rac
def get_selection(self, u, b, k): s = utils.scores_sorter(self.model[u], k + b) rac = [i for i in s if i not in self.test_users[u][:b]][:k] return rac
row.append(utr2utr_id[user]) col.append(itr2itr_id[song]) data += data_row R = sps.csr_matrix((data, (row, col))) print("R built with shape: " + str(R.shape)) utils.fl() itr_id2itr = {itr2itr_id[s]: s for s in itr2itr_id} Rhat = (Ku.power(q).dot(R)) print(Rhat.shape) recommendation = {} for user in u2u_id: print(user) scores = Rhat[u2u_id[user]].toarray()[0] idx = utils.scores_sorter(scores, 500) recommendation[str(user)] = [itr_id2itr[i] for i in idx if scores[i] > 0] recommendation[str(user)] = [ s_id2spt[i] for i in recommendation[str(user)] if i not in p2s_test[test_seed][user][:int(test_seed)] ][:500] k = 0 while len(recommendation[str(user)]) < 500: track = s_id2spt[global_popularity[k]] if track not in recommendation[str(user)] and global_popularity[ k] not in p2s_test[test_seed][user][:int(test_seed)]: recommendation[str(user)].append(track) k += 1 utils.save_recommendation("./1.csv", recommendation)
p2s_test = utils_matrix.load_test_set("./") s_id2spt = utils.jload("./s_id2spt.json") print "loading P...", utils.fl() P = sps.load_npz("./P.npz") print "done" utils.fl() print "loading P csc...", utils.fl() P_csc = sps.load_npz("./P_csc.npz") print "done" utils.fl() print("prediction with alpha=%f, q=%f started" % (alpha, q)) recommendation = {} for u in sorted(p2s_test[test_seed].keys()): Iu = p2s_test[test_seed][u][:int(test_seed)] Wu = (P_csc[:, Iu].power(alpha).multiply( P[Iu, :].power(1 - alpha).transpose())).power(q) scores = np.array(np.sum(Wu, axis=1)).flatten() recommendation[u] = [ s_id2spt[i] for i in utils.scores_sorter(scores) if i not in Iu ][:500] print(u) utils.fl() #here you should specify the directory where to save the recommendation utils.save_recommendation("./%s.csv" % (test_seed), recommendation)
data = [] col = [] row = [] for t in w2pop: for s in w2pop[t]: data.append(w2pop[t][s]) col.append(int(s)) row.append(t2t_id[t]) P = sps.csr_matrix((data, (col, row))) #the matrix needs to be transposed global_popularity = np.zeros(len(s2p)) for s in s2p: global_popularity[int(s)] = len(s2p[s]) global_popularity = np.argsort(-global_popularity) recommendation = {} for user in p2s_test[bucket]: if p2t[str(user)] in t2t_id: row = S[t2t_id[p2t[str(user)]], :]**q recommendation[user] = [ s_id2spt[s] for s in utils.scores_sorter(P.dot(row)) ][:500] else: recommendation[user] = [s_id2spt[s] for s in global_popularity[:500]] #here you should specify the directory where to save the recommendation utils.save_recommendation("./" + str(bucket) + ".csv", recommendation, intestation=True)