使用时pytables
,不支持(据我所知)scipy.sparse
矩阵格式,因此要存储矩阵,我必须进行一些转换,例如
def store_sparse_matrix(self):
grp1 = self.getFileHandle().createGroup(self.getGroup(), 'M')
self.getFileHandle().createArray(grp1, 'data', M.tocsr().data)
self.getFileHandle().createArray(grp1, 'indptr', M.tocsr().indptr)
self.getFileHandle().createArray(grp1, 'indices', M.tocsr().indices)
def get_sparse_matrix(self):
return sparse.csr_matrix((self.getGroup().M.data, self.getGroup().M.indices, self.getGroup().M.indptr))
问题是该get_sparse
功能需要一些时间(从磁盘读取),如果我理解正确,还需要数据适合内存。
唯一的其他选择似乎是将矩阵转换为密集格式(numpy array
)然后pytables
正常使用。然而,这似乎是相当低效的,虽然我想也许pytables
会处理压缩本身?