0

对于许多算法,例如 SVD,现成的内置函数是:

  1. predictions = algo.fit(trainset).test(testset) -- 打印测试集的预测评分(因此对于用户已经给出评分的电影)

  2. predictions = algo.predict(uid, iid) -- 预测uid的iid的评分

但是我怎样才能top N为用户打印推荐的电影(即使这个用户还没有看过/给某些电影评分)。我试过了:"algo.fit(trainset).test(data)"但它给了我错误?

我也尝试过使用 KNN 来惊喜地打印k nearest neighbors用户在惊喜包示例中,它有 u.item 文件,但是如果我想使用我自己的数据(一个具有 uid、iid 和评级的表),如何我可以计算"raw id"用户和项目的吗?

4

1 回答 1

1

此代码片段是从惊喜文档常见问题中共享的,可能会有所帮助。

from collections import defaultdict

from surprise import SVD
from surprise import Dataset


def get_top_n(predictions, n=10):
    """Return the top-N recommendation for each user from a set of predictions.

    Args:
        predictions(list of Prediction objects): The list of predictions, as
            returned by the test method of an algorithm.
        n(int): The number of recommendation to output for each user. Default
            is 10.

    Returns:
    A dict where keys are user (raw) ids and values are lists of tuples:
        [(raw item id, rating estimation), ...] of size n.
    """

    # First map the predictions to each user.
    top_n = defaultdict(list)
    for uid, iid, true_r, est, _ in predictions:
        top_n[uid].append((iid, est))

    # Then sort the predictions for each user and retrieve the k highest ones.
    for uid, user_ratings in top_n.items():
        user_ratings.sort(key=lambda x: x[1], reverse=True)
        top_n[uid] = user_ratings[:n]

    return top_n


# First train an SVD algorithm on the movielens dataset.
data = Dataset.load_builtin('ml-100k')
trainset = data.build_full_trainset()
algo = SVD()
algo.fit(trainset)

# Than predict ratings for all pairs (u, i) that are NOT in the training set.
testset = trainset.build_anti_testset()
predictions = algo.test(testset)

top_n = get_top_n(predictions, n=10)

# Print the recommended items for each user
for uid, user_ratings in top_n.items():
    print(uid, [iid for (iid, _) in user_ratings])
于 2020-08-08T19:19:38.220 回答