Daniel Wyatt
Daniel Wyatt

Reputation: 1151

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:

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?

Upvotes: 1

Views: 651

Answers (1)

d_kennetz
d_kennetz

Reputation: 5359

It is the same command as you would use in numpy or 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.

Correspondingly, there is also a 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.

Check it out here.

Upvotes: 3

Related Questions