使用pytables,效率更高:scipy.sparse还是numpy密集矩阵?

tdc*_*tdc 9 python numpy scipy sparse-matrix pytables

使用时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))
Run Code Online (Sandbox Code Playgroud)

麻烦的是该get_sparse函数需要一些时间(从磁盘读取),如果我理解正确也需要数据适合内存.

唯一的其他选择似乎是将矩阵转换为密集格式(numpy array)然后pytables正常使用.然而,这似乎是相当低效的,虽然我想也许pytables会处理压缩本身?

Ian*_*nSR 2

借用在 HDF5 (PyTables) 中存储 numpy 稀疏矩阵,您可以使用其、和属性(这三个常规对象)将scipy.sparse数组编组为 pytables 格式。dataindiciesindptrnumpy.ndarray