首页 > 解决方案 > 在为推荐系统创建矩阵时如何处理非常大的数据集?

问题描述

我正在尝试创建一个交易和产品组矩阵,但我有一个非常大的交易数据(超过 10,000,000 行)和大约 100 个产品组。当我尝试使用此代码创建数据透视表时

df.pivot(index='transaction_id', columns='product_group', values='ratings')

它返回值错误“Unstacked DataFrame 太大,导致 int32 溢出”

除了减少数据的大小之外,还有什么方法可以解决这个问题吗?

谢谢!

标签: pythonpandaspivotsparse-matrixrecommendation-engine

解决方案


Convert your axes columns to categories:

df['transaction_id'] = df['transaction_id'].astype('category')
df['product_group'] = df['product_group'].astype('category')

Make a sparse matrix using the encodings:

arr = csr_matrix((df['ratings'].values, (df['transaction_id'].cat.codes, df['product_group'].cat.codes)))

Then you just have to keep track of the order of your axes (df['transaction_id'].cat.categories will give you the labels that should be applied to the rows for example).


推荐阅读