def topk(topk_data: TopkData, score_fn: Callable[[Dict[str, List[int]]], List[float]], ks=[10, 36, 100]): precisions, recalls = topk_evaluate(topk_data, score_fn, ks) for k, precision, recall in zip(ks, precisions, recalls): f1 = 2. * precision * recall / pr if (pr := precision + recall) else 0 print('[k=%d, precision=%.3f%%, recall=%.3f%%, f1=%.3f%%]' % (k, 100. * precision, 100. * recall, 100. * f1), end='')
def topk(): ks = [10, 36, 100] precisions, recalls = topk_evaluate( topk_data, lambda ui: fast_model(tf.constant(ui)).numpy(), ks) for k, precision, recall in zip(ks, precisions, recalls): tf.print( '[k=%d, precision=%.3f%%, recall=%.3f%%, f1=%.3f%%]' % (k, 100 * precision, 100 * recall, 200 * precision * recall / (precision + recall)), end='') tf.print()
if W[i][j] != 0: Wi[j] = W[i][j] most_similar_items.append(set(x[0] for x in sorted(Wi.items(), key=lambda x: x[1], reverse=True)[:N])) scores = [[0 for _ in range(n_item)] for _ in range(n_user)] # scores[u][i]是用户u对物品i的评分 for user_id in range(n_user): user_item_set = train_user_items[user_id] for i in user_item_set: for j in most_similar_items[i]: if j not in user_item_set: scores[user_id][j] += W[i][j] #for i in set(range(n_item)) - user_item_set: # for j in user_item_set & most_similar_items[i]: # scores[user_id][i] += W[i][j] print('(耗时', time.time() - start_time, '秒)', sep='') return scores if __name__ == '__main__': n_user, n_item, train_data, test_data, topk_data = data_process.pack(data_loader.ml1m, negative_sample_ratio=0) W = _item_similarity(train_data, n_user, n_item) scores = _user_item_score(train_data, n_user, n_item, W, N=10) ks = [10, 36, 100] precisions, recalls = topk_evaluate(topk_data, lambda uis: [scores[u][i] for u, i in uis], ks) for k, precision, recall in zip(ks, precisions, recalls): print('[k=%d, precision=%.3f%%, recall=%.3f%%, f1=%.3f%%]' % (k, 100 * precision, 100 * recall, 200 * precision * recall / (precision + recall)))