9

使用时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会处理压缩本身?

4

1 回答 1

2

借用Storing numpy sparse matrix in HDF5 (PyTables),您可以使用它的、 和属性(三个常规对象)将scipy.sparse数组编组为 pytables 格式。dataindiciesindptrnumpy.ndarray

于 2014-01-23T20:54:36.597 回答