【问题标题】:How to pivot a large dataset for recommender system with panda?如何使用 panda 为推荐系统旋转大型数据集?
【发布时间】:2019-07-24 01:41:47
【问题描述】:

我正在做一个有 300,000 个用户和 280,000 个项目的推荐系统,人们通常通过将数据框转换为表格来做推荐系统:

df.pivot_table(index='User-ID',columns='Item-ID',values='Rating')

但不可能将如此庞大的数据集转换为表格。处理这个问题的常用方法是什么?或者人们使用其他结构来做推荐系统?

【问题讨论】:

  • 将它们放入 index ,如 df = df.set_index(['User-ID','Item-ID'])

标签: python pandas recommendation-engine recommender-systems


【解决方案1】:

肯定有更好的方法来节省大量的计算和内存。我们创建的 user-item 矩阵通常是稀疏的并且包含大量的零,这极大地增加了计算和内存的复杂性。 例如:

        Per1    Per2    Per3    Per4    Per5    Per6    Per7    per8
Item1    5        0        1     0       0        0       0       0    
Ttem2    0        3        0     0       2        0       0       0

矩阵中的零会增加计算量。

存储矩阵的更好方法是使用压缩稀疏行矩阵算法来存储矩阵。它删除所有零值并仅存储非零值。这是为推荐系统创建 csr 矩阵的简单函数:-

def create_matrix(data, user_col, item_col, rating_col):
    """
    creates the sparse user-item interaction matrix

    Parameters
    ----------
    data : DataFrame
        implicit rating data

    user_col : str
        user column name

    item_col : str
        item column name

    ratings_col : str
        implicit rating column name
    """



    # create a sparse matrix of using the (rating, (rows, cols)) format
    rows = data[user_col].cat.codes
    cols = data[item_col].cat.codes
    rating = data[rating_col]
    ratings = csr_matrix((rating, (rows, cols)))
    ratings.eliminate_zeros()
    return ratings, data

希望对你有帮助!!!

【讨论】:

    猜你喜欢
    • 2012-05-28
    • 2013-02-28
    • 2013-08-16
    • 1970-01-01
    • 2018-10-21
    • 2011-03-18
    • 1970-01-01
    • 2017-08-08
    • 2021-08-28
    相关资源
    最近更新 更多