+3 votes
in Programming Languages by (63.5k points)
I want to save the data of a CSR matrix in a CSV file. CSR matrix has a large number of 0s and fewer number of 1s. In the CSV file, I want to write both 1s and 0s.

How can I convert csr_matrix to a CSV file?

1 Answer

0 votes
by (271k points)

Since you want to write both 1s and 0s to the CSV file, you can use todense() function first to convert the sparse matrix to a dense matrix. Then you can convert the dense matrix to a pandas dataframe to write to a CSV file.

If you have a very large CSR matrix, this approach may be slow. Here is the python code to save CSR matrix to a CSV file.

import numpy as np
from scipy.sparse import csr_matrix
import pandas as pd

# create a test CSR matrix
r = np.array([0, 0, 1, 1, 2, 2, 2, 3, 4, 4, 5, 6, 6])
c = np.array([0, 3, 4, 1, 3, 5, 6, 3, 1, 6, 0, 1, 3])
data = np.array([1]*len(r))
X = csr_matrix((data, (r, c)), shape=(7, 7))

# save CSR matrix as csv
df = pd.DataFrame(csr_matrix.todense(X))
csv_file = "test_csv_file.csv"
print("Write data to a CSV file", csv_file)
df.to_csv(csv_file, index=False, header=None)


...