英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论