構建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