Sparse matrices save memory by storing only non-zero values. This helps when working with big data that has many zeros.
0
0
Creating sparse matrices in SciPy
Introduction
When you have a large dataset with mostly zero values.
When you want to speed up calculations by ignoring zeros.
When storing data like text documents or graphs where many entries are empty.
When you want to save memory in machine learning tasks with sparse features.
Syntax
SciPy
from scipy.sparse import csr_matrix, csc_matrix, coo_matrix # Create sparse matrix from dense array sparse_matrix = csr_matrix(dense_array) # Create sparse matrix from data, row indices, and column indices sparse_matrix = coo_matrix((data, (row_indices, col_indices)), shape=(rows, cols))
Common sparse formats are CSR, CSC, and COO.
COO format is good for building matrices; CSR and CSC are efficient for calculations.
Examples
Create a CSR sparse matrix from a small dense array.
SciPy
from scipy.sparse import csr_matrix import numpy as np dense = np.array([[0, 0, 1], [1, 0, 0], [0, 0, 0]]) sparse = csr_matrix(dense) print(sparse)
Create a COO sparse matrix from data and their positions.
SciPy
from scipy.sparse import coo_matrix data = [4, 5, 7] rows = [0, 1, 2] cols = [1, 2, 0] sparse = coo_matrix((data, (rows, cols)), shape=(3, 3)) print(sparse)
Sample Program
This program shows two ways to create sparse matrices: from a dense array and from data with positions.
SciPy
from scipy.sparse import csr_matrix, coo_matrix import numpy as np # Create a dense numpy array dense_array = np.array([ [0, 0, 3], [4, 0, 0], [0, 0, 0], [0, 5, 0] ]) # Convert dense array to CSR sparse matrix csr = csr_matrix(dense_array) print('CSR matrix:') print(csr) # Create COO sparse matrix directly from data data = [3, 4, 5] row_indices = [0, 1, 3] col_indices = [2, 0, 1] coo = coo_matrix((data, (row_indices, col_indices)), shape=(4, 3)) print('\nCOO matrix:') print(coo)
OutputSuccess
Important Notes
Use CSR or CSC format for fast arithmetic and matrix-vector operations.
COO format is easy to create but slower for calculations.
Always specify the shape when creating sparse matrices from data and indices.
Summary
Sparse matrices store only non-zero values to save memory.
Use scipy.sparse to create sparse matrices in different formats.
Choose the format based on your task: COO for building, CSR/CSC for calculations.