当我从文件中读取数据时,我试图构建和更新一个稀疏矩阵。
矩阵的大小是100000X40000
更新稀疏矩阵的多个条目的最有效方法是什么? 具体来说,我需要将每个条目增加1。
假设我有行索引[2, 236, 246, 389, 1691]
和列索引[117, 3, 34, 2757, 74, 1635, 52]
因此,以下所有条目必须递增一:
(2,117) (2,3) (2,34) (2,2757) ...
(236,117) (236,3) (236, 34) (236,2757) ...
等等。
我已经在使用lil_matrix
,因为当我试图更新一个条目时,它给了我一个使用警告。
lil_matrix
格式已不支持多次更新。
matrix[1:3,0] += [2,3]
给了我一个未实现的错误。
我可以很天真地做到这一点,将每个条目单独递增。我想知道是否有更好的方法来做到这一点,或更好的稀疏矩阵实现,我可以使用。
我的电脑也是一台普通的i5机器,内存为4GB,所以我必须小心不要把它炸掉:)
这个答案扩展了@behzad.nouri的评论。要在行和列索引列表的“外部产品”处增加值,只需将它们创建为配置为广播的numpy数组。在本例中,这意味着将行放入列中。例如
在新坐标系中用
1
s创建第二个矩阵并将其添加到现有矩阵中是一种可能的方法:我认为它运行良好,不需要循环。我直接跟着doc
相关问题 更多 >
编程相关推荐