python - 如何使用 panda 为推荐系统转换大型数据集?

标签 python pandas recommendation-engine

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

df.pivot_table(index='用户 ID',columns='项目 ID',values='评级')

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

最佳答案

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

        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

希望对你有帮助!!!

关于python - 如何使用 panda 为推荐系统转换大型数据集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57174142/

相关文章:

python - Python中的未绑定(bind)本地错误

python - 如何使用 imp 在相同的绝对/相对路径中导入具有依赖关系的模块?

python - 多处理问题 [pyqt, py2exe]

python - 在 Pandas 中,如何在 Groupby 对象上使用 Group mean 应用自定义函数

Python Pandas - 自上次出现在 200 万行数据框中以来的分钟数

python - 无法生成列表以显示列表中的任何匹配项

python - 是否可以在不将编码器传递给 json.dumps() 的情况下将枚举转储到 json 中?

hadoop - 哪些距离度量在基于内容的推荐系统上表现良好?

php - 对网络集体编程感兴趣——Ruby 或 Python 或 PHP?

machine-learning - 使用机器学习来删除重复数据