构建scipy稀疏矩阵的最佳方法之一是使用coo_matrix方法即。构建大型scipy稀疏矩阵
coo_matrix((data, (i, j)), [shape=(M, N)])
where:
data[:] are the entries of the matrix, in any order
i[:] are the row indices of the matrix entries
j[:] are the column indices of the matrix entries
但是,如果矩阵非常大,将整个i,j和数据向量载入内存是不实际的。
如何构建一个coo_matrix,使得(数据,(i,j))从磁盘进入(使用迭代器或生成器)并且磁盘上的数组/矢量对象使用.npy或pickle格式?
Pickle是更好的选择,因为numpy.save/load没有针对scipy sparse进行优化。也许还有另一种更快的格式。
numpy.genfromtext()和numpy.loadtxt()都是繁琐,慢速和内存耗尽的问题。
使用您的示例创建coo_matrix,内存包含对象data,rows,cols和M.问题是coo_matrix是否可以通过流行,列和数据递增创建。你的回答表明这是不可能的。 –
'coo_matrix'的'__init__'代码是用Python编写的,易于操作。 'bmat'的代码,从块中建立一个'coo'矩阵也是有启发性的。 – hpaulj