简体   繁体   English

如何在python中保存从稀疏库创建的稀疏矩阵

[英]How do I save a sparse matrix created from the sparse library in python

I have created a 5 dimensional sparse matrix using the sparse library in python, My code looks similar to the documentation: 我在python中使用稀疏库创建了一个5维稀疏矩阵,我的代码看起来类似于文档:

import sparse
coords = [[0, 1, 2, 3, 4],
           [0, 1, 2, 3, 4]]
data = [10, 20, 30, 40, 50]
s = sparse.COO(coords, data, shape=(5, 5))

I want to save this matrix so i can load it later with something like: 我想保存这个矩阵,所以我可以稍后加载它,例如:

sp.save_npz("filename.npz", s)

What is the equivalent function to save_npz (in scipy) in the sparse package? 稀疏包中save_npz(scipy)的等效函数是什么?

It is the same command as you would use in numpy or scipy: 它与你在numpy或scipy中使用的命令相同:

sparse.save_npz('filename.npz', s, compressed=True)

It actually saves it in numpy's npz format, but this is included in the sparse API. 它实际上将它保存为numpy的npz格式,但这包含在稀疏API中。

Correspondingly, there is also a sparse.load_npz() . 相应地,还有一个sparse.load_npz()

Sparse has pretty easy integration with scipy and numpy, and you can actually even convert between numpy or scipy and sparse matrices. Sparse很容易与scipy和numpy集成,你甚至可以在numpy或scipy和稀疏矩阵之间进行转换。

Check it out here . 在这里查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM