将稀疏矩阵写入压缩的gzip文件。

huangapple go评论64阅读模式
英文:

Writing out sparse matrix as a compressed gzip file

问题

我有一个稀疏矩阵 m(scipy.sparse.coo_matrix)和一个输出路径 p(示例 p="~/matrix.mtx.gz")。我正在使用 scipy.io.mmwrite 来将 m 写入到路径 p,但似乎没有压缩选项。有没有一种方法可以将稀疏矩阵写入为Matrix Market格式的gzip文件?我不想将其保存为.mtx文件,然后再次读取文件以进行gzip压缩。

英文:

I have a sparse matrix m (scipy.sparse.coo_matrix) and a output path p (example p="~/matrix.mtx.gz"). I'm using scipy.io.mmwrite to write out m (to the path p), but it doesn't appear to have any compression options. Is there a way to write out the sparse matrix as a gzipped file in Matrix Market format? I don't want to save as a .mtx file and have to read in the file again to gzip it.

答案1

得分: 2

这应该可以工作:

import gzip
with gzip.open('matrix.mtx.gz', 'wb') as f:
    scipy.io.mmwrite(f, array)
英文:

Untested, but this should work:

import gzip
with gzip.open('matrix.mtx.gz', 'wb') as f:
    scipy.io.mmwrite(f, array)

huangapple
  • 本文由 发表于 2023年5月28日 02:08:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定