Sparse matrices save memory by only storing the important numbers, not all the zeros. This makes working with big data faster and easier.
0
0
Why sparse matrices save memory in SciPy
Introduction
When you have a large table mostly filled with zeros.
When you want to save computer memory for big datasets.
When you need to speed up calculations on data with many zeros.
When storing connections in a network where most nodes are not connected.
When working with text data represented as word counts where most words don't appear.
Syntax
SciPy
from scipy.sparse import csr_matrix # Create a sparse matrix from a dense matrix dense_matrix = [[0, 0, 1], [0, 2, 0], [3, 0, 0]] sparse_matrix = csr_matrix(dense_matrix)
csr_matrix is a common sparse matrix format that stores only non-zero values and their positions.
You convert a normal matrix (dense) to sparse to save memory when many values are zero.
Examples
This example converts a small dense matrix with zeros into a sparse matrix.
SciPy
from scipy.sparse import csr_matrix import numpy as np dense = np.array([[0, 0, 1], [0, 2, 0], [3, 0, 0]]) sparse = csr_matrix(dense) print(sparse)
This example creates a sparse matrix by specifying only the non-zero values and their positions.
SciPy
from scipy.sparse import csr_matrix # Create sparse matrix directly from data row = [0, 1, 2] col = [2, 1, 0] data = [1, 2, 3] sparse = csr_matrix((data, (row, col)), shape=(3, 3)) print(sparse.toarray())
Sample Program
This program shows how a large matrix mostly filled with zeros uses much less memory when stored as a sparse matrix.
SciPy
from scipy.sparse import csr_matrix import numpy as np # Create a large dense matrix mostly zeros rows, cols = 1000, 1000 dense = np.zeros((rows, cols)) dense[0, 0] = 10 dense[500, 500] = 20 dense[999, 999] = 30 # Convert to sparse matrix sparse = csr_matrix(dense) # Print memory sizes print(f"Dense matrix size: {dense.nbytes} bytes") print(f"Sparse matrix data size: {sparse.data.nbytes} bytes") print(f"Sparse matrix indices size: {sparse.indices.nbytes} bytes") print(f"Sparse matrix indptr size: {sparse.indptr.nbytes} bytes")
OutputSuccess
Important Notes
Sparse matrices only store non-zero values, their row and column positions.
This saves memory especially when zeros are the majority of the data.
Not all operations work the same on sparse matrices, so check compatibility.
Summary
Sparse matrices save memory by storing only non-zero values.
They are useful for big data with many zeros.
Using sparse matrices can speed up calculations and reduce memory use.