【发布时间】:2015-09-05 00:35:14
【问题描述】:
我有一个数据文件,它编码有关大型稀疏布尔矩阵的非零元素的信息。该矩阵没有任何特定的结构,即它不是对角线或块等。文件的每一行确定一个元素。现在我使用以下循环来填充矩阵:
from scipy.sparse import dok_matrix
nRows = 30000
nCols = 600000
data = dok_matrix((nRows,nCols), dtype=np.int8)
with open('input.txt','r') as fraw:
for line in fraw:
## Figure out iRow and iCol to set to 1 from line
data[iRow,iCol] = 1
这是有效的,但速度很慢。有没有更优化的不同类型的scipy.sparse 矩阵?
'Optimal' 表示矩阵的生成速度和矩阵的行和列块的访问速度,例如像
这样的向量运算someRows = data[rowIndex1:rowIndex2,]
someColumns = data[,colIndex1:colIndex2]
如果记忆比速度更重要,答案会改变吗?
谢谢
【问题讨论】:
标签: python scipy vectorization sparse-matrix