【问题标题】:How to make SVD recommendations如何提出 SVD 建议
【发布时间】:2020-02-13 12:13:23
【问题描述】:

我正在尝试使用 SVD 根据购买情况进行图书推荐。我正在使用一个矩阵,其中列是书籍,行是用户名和内容,如果他/她购买了有问题的书(如果用户购买了则为 1,如果他没有购买则为 0 [我不知道是否这是最好的选择])。 我还创建了一个 cosine_similarity 矩阵,我可以在其中根据用户的购买情况查看用户之间的相似度。

# Matrix with users as rows, products as columns and count as content
df_matrix = pd.pivot_table(order_df, values='count', index='username', columns='product')
# Replace all NaN contents with 0
df_matrix_dummy = df_matrix.copy().fillna(0)
#Compute the cosine similarity matrix using the dummy matrix
cosine_sim = cosine_similarity(df_matrix_dummy, df_matrix_dummy)
#Convert into pandas dataframe 
cosine_sim = pd.DataFrame(cosine_sim, index=df_matrix.index, columns=df_matrix.index)


reader = Reader()
# Famous SVD algorithm 
algo = SVDpp()
data = Dataset.load_from_df(order_df[['username', 'product', 'count']], reader)
# Divide the data into train and test (80-20)
trainset, testset = train_test_split(data, test_size=0.2)
algo.train(trainset)

由于我是这种技术的新手,我的问题是如何在训练 SVD 后获得推荐列表。

【问题讨论】:

    标签: python python-2.7 machine-learning svd recommender-systems


    【解决方案1】:

    您可以按照此示例并将其与您的代码集成。

    from surprise import SVD
    from surprise import Dataset
    from surprise import accuracy
    from surprise.model_selection import train_test_split
    
    # Load the movielens-100k dataset (download it if needed),
    data = Dataset.load_builtin('ml-100k')
    
    trainset, testset = train_test_split(data, test_size=.25)
    
    algo = SVD()
    algo.fit(trainset)
    predictions = algo.test(testset)
    test = pd.DataFrame(predictions)
    test = test.rename(columns={'uid':'userId', 'iid': 'movieId', 
                                'r_ui':'actual', 'est':'prediction'})
    cf_model = test.pivot_table(index='userId', 
                                columns='movieId', values='prediction').fillna(0)
    
    def get_users_predictions(user_id, n, model):
        recommended_items = pd.DataFrame(model.loc[user_id])
        recommended_items.columns = ["predicted_rating"]
        recommended_items = recommended_items.sort_values('predicted_rating', ascending=False)    
        recommended_items = recommended_items.head(n)
        return recommended_items.index.tolist()
    
    def get_recs(model, k):
        recs = []
        for user in model.index:
            cf_predictions = get_users_predictions(user, k, model)
            recs.append(cf_predictions)
        return recs    
    
    # Top-10 recommendations for each user
    k = 10
    recs = get_recs(cf_model, k)
    preds = pd.DataFrame(index=cf_model.index)
    preds[f'Top-{k} Recommendation'] = recs
    preds.head()
    

    输出:

    |   userId | Top-10 Recommendation                                                     |
    |---------:|:--------------------------------------------------------------------------|
    |        1 | ['50', '174', '173', '268', '183', '89', '64', '251', '100', '136']       |
    |       10 | ['174', '178', '98', '530', '474', '511', '478', '132', '493', '709']     |
    |      100 | ['887', '292', '1235', '268', '1238', '348', '1234', '271', '326', '886'] |
    |      101 | ['181', '742', '237', '596', '405', '118', '255', '1028', '717', '928']   |
    |      102 | ['50', '98', '96', '187', '168', '79', '530', '435', '228', '185']        |
    

    【讨论】:

    • 你没有用火车做任何事情,怎么样?
    • 太好了,谢谢,我可以试试看效果如何
    猜你喜欢
    • 2018-10-26
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2020-07-27
    • 2017-01-30
    相关资源
    最近更新 更多