I have a scipy.sparse matrix (csr_matrix()). But I need to save it to a file not in the .npz format but as a regular .txt or .csv file. My problem is that I don't have enough memory to convert the sparse matrix into a regular np.array() and then save it to a file. Is there a way to have the data as a sparse matrix in memory but save it directly as a regular matrix in the form:
0 0 0
0 1 0
1 0 1
to the disk? Or is there a way to "unzip" a .npz file without loading it into memory inside Python? (like for example gunzip or unzip in Bash).
Answer to new question:
And with begin/end brackets:
You may have to fiddle with
set_printoptionsdepending on your data.Answer to original question, which did not require that the matrix be written as dense.
Harwell-Boeing format is plain text:
So is Matrix Market:
If you want an even simpler format, although it involves more code:
To load:
But the important idea is that all you need to save are the
data,indices, andindptrattributes of thecsr_matrix.