0
0
SciPydata~5 mins

Sparse matrix file I/O in SciPy

Choose your learning style9 modes available
Introduction

Sparse matrices save memory by storing mostly zeros efficiently. Saving and loading them helps keep data safe and reuse it later.

You want to save a large sparse matrix after a long calculation to avoid repeating it.
You need to share a sparse matrix with a teammate or another program.
You want to load a sparse matrix from disk to analyze or visualize it.
You are working with machine learning data that is mostly zeros and want to store it efficiently.
Syntax
SciPy
from scipy import sparse

# Save sparse matrix
sparse.save_npz(filename, sparse_matrix)

# Load sparse matrix
loaded_matrix = sparse.load_npz(filename)

Use save_npz to save and load_npz to load sparse matrices in compressed format.

This works with common sparse formats like CSR and CSC.

Examples
Create a CSR sparse matrix and save it to 'matrix.npz'.
SciPy
from scipy import sparse
import numpy as np

# Create a sparse matrix
matrix = sparse.csr_matrix(np.array([[0, 0, 1], [1, 0, 0], [0, 0, 0]]))

# Save it
sparse.save_npz('matrix.npz', matrix)
Load the saved sparse matrix and convert it to a normal array to see the data.
SciPy
from scipy import sparse

# Load the sparse matrix
loaded = sparse.load_npz('matrix.npz')
print(loaded.toarray())
Sample Program

This program creates a sparse matrix, saves it to a file, loads it back, and prints the full matrix to verify it saved correctly.

SciPy
from scipy import sparse
import numpy as np

# Create a sparse matrix with mostly zeros
rows = np.array([0, 1, 2])
cols = np.array([2, 0, 1])
data = np.array([1, 2, 3])

matrix = sparse.csr_matrix((data, (rows, cols)), shape=(3, 3))

# Save the sparse matrix to a file
sparse.save_npz('example_matrix.npz', matrix)

# Load the sparse matrix back
loaded_matrix = sparse.load_npz('example_matrix.npz')

# Print the dense form to check
print(loaded_matrix.toarray())
OutputSuccess
Important Notes

Always use toarray() or todense() to see the full matrix when printing.

File extension .npz is recommended for saving sparse matrices.

Saving in sparse format is much faster and smaller than saving dense matrices when data is mostly zeros.

Summary

Sparse matrix file I/O lets you save and load memory-efficient matrices easily.

Use save_npz and load_npz from scipy.sparse.

This helps reuse data and share it without losing the sparse structure.