python - 在为推荐系统创建矩阵时如何处理非常大的数据集?
问题描述
我正在尝试创建一个交易和产品组矩阵,但我有一个非常大的交易数据(超过 10,000,000 行)和大约 100 个产品组。当我尝试使用此代码创建数据透视表时
df.pivot(index='transaction_id', columns='product_group', values='ratings')
它返回值错误“Unstacked DataFrame 太大,导致 int32 溢出”
除了减少数据的大小之外,还有什么方法可以解决这个问题吗?
谢谢!
解决方案
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).
推荐阅读
- php - 我想在这个中传递第三个日期变量......我怎么能通过???date= $date= date("Ymd"); INSERT INTO article_likes(用户、文章、日期)
- angular - 在 Angular 的函数参数中使用联合类型时出现编译错误
- java - 使用 MyBatis 从两个表的连接中检索地图中的值
- facebook - Facebook Graph API - 获取地址,但为空
- xamarin - Setting MainPage = NavigationPage during OnStart in Xamarin.Forms displays a blank screen in Android
- ios - 将 CLLocationCoordinate2D 返回到结构
- hyperledger-fabric - 运行 Hyperledger Composer 命令时出错:composer network start
- axapta - 如何检查是否可以使用 next 运算符?
- c# - Lync 客户端通用 COM 异常
- azure-container-service - aks 上每个节点的 pod 数量限制