I have a scipy.sparse.csr matrix and would like to dump it to a CSV file. Is there a way to preserve the sparsity of the matrix and write it to a CSV?
Asked
Active
Viewed 7,482 times
7
-
How are you going to use the sparse CSV file? Existing programs that read in the CSV file are going to assume that each line contains a row and fields are separated by commas. Perhaps compressing the file gets you what you want. – ditkin May 22 '11 at 11:30
-
I am using it for another task. CSV may probably be the wrong choice. I want a text file where for each row in the matrix I get the values of only the non zero values. Currently, I have done that using some code. I was just wondering if it could be done smarter or otherwise. – Dexter May 22 '11 at 12:28
2 Answers
8
SciPy includes functions that read/write sparse matrices in the MatrixMarket format via the scipy.io module, including mmwrite: http://docs.scipy.org/doc/scipy/reference/generated/scipy.io.mmwrite.html
MatrixMarket is not CSV, but close. It consists of a one-line header that has #rows, #cols, # of nonzeros, followed by one line per nonzero. Each of these lines is the row index, column index, value. You could write a simple script that turns whitespace into commas and you'd have a CSV.

Adam
- 16,808
- 7
- 52
- 98
0
Now with Scipy 0.19, it is super easy:
import scipy.sparse as sp
m = sp.csr_matrix([[1,0,0],[0,1,0],[0,0,1]])
sp.save_npz("file_name.npz", m)
Loading file to memory
new_m = sp.load_npz("file_name.npz")

Abhi25t
- 3,703
- 3
- 19
- 32