Last Updated : 05 Jul, 2025
If most of the elements of the matrix have 0 value, then it is called a sparse matrix. The two major benefits of using sparse matrix instead of a simple matrix are:
Sparse matrices are generally utilized in applied machine learning such as in data containing data-encodings that map categories to count and also in entire subfields of machine learning such as natural language processing (NLP).
Example:
0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0
Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. This means storing non-zero elements with triples- (Row, Column, value).
Create a Sparse Matrix with SciPyThe scipy.sparse module in Python provides efficient ways to store and work with large sparse matrices using various formats, each optimized for specific operations. Let's explore the different methods this module offers:
Format
Best For
Description
csr_matrix
Fast row slicing, math operations
Compressed Sparse Row good for arithmetic and row access.
csc_matrix
Fast column slicing
Compressed Sparse Column efficient for column-based ops.
coo_matrix
Easy matrix building
Coordinate format using (row, col, value) triples.
lil_matrix
Incremental row-wise construction
List of Lists, modify rows easily before converting.
dia_matrix
Diagonal-dominant matrices
Stores only diagonals, saves space.
dok_matrix
Fast item assignment
Dictionary-like, ideal for random updates.
ExamplesExample 1: csr_matrix (Compressed Sparse Row)
Python
import numpy as np
from scipy.sparse import csr_matrix
d = np.array([3, 4, 5, 7, 2, 6]) # data
r = np.array([0, 0, 1, 1, 3, 3]) # rows
c = np.array([2, 4, 2, 3, 1, 2]) # cols
csr = csr_matrix((d, (r, c)), shape=(4, 5))
print(csr.toarray())
Output
[[0 0 3 0 4]
[0 0 5 7 0]
[0 0 0 0 0]
[0 2 6 0 0]]
Explanation: Creates a Compressed Sparse Row (CSR) matrix using non-zero values d and their row r and column c indices. csr_matrix stores only the non-zero elements efficiently and toarray() converts it back to a full 2D NumPy array.
Example 2: csc_matrix (Compressed Sparse Column)
Python
import numpy as np
from scipy.sparse import csc_matrix
d = np.array([3, 4, 5, 7, 2, 6]) # data
r = np.array([0, 0, 1, 1, 3, 3]) # rows
c = np.array([2, 4, 2, 3, 1, 2]) # cols
csc = csc_matrix((d, (r, c)), shape=(4, 5))
print(csc.toarray())
Output
[[0 0 3 0 4]
[0 0 5 7 0]
[0 0 0 0 0]
[0 2 6 0 0]]
Explanation: Creates a Compressed Sparse Column (CSC) matrix using non-zero values d with their row r and column c indices. csc_matrix efficiently stores data column-wise and toarray() converts it back to a full 2D NumPy array.
Example 3: coo_matrix (Coordinate Format)
Python
import numpy as np
from scipy.sparse import coo_matrix
d = np.array([3, 4, 5, 7, 2, 6]) # data
r = np.array([0, 0, 1, 1, 3, 3]) # rows
c = np.array([2, 4, 2, 3, 1, 2]) # cols
coo = coo_matrix((d, (r, c)), shape=(4, 5))
print(coo.toarray())
Output
[[0 0 3 0 4]
[0 0 5 7 0]
[0 0 0 0 0]
[0 2 6 0 0]]
Explanation: Creates a COO matrix using non-zero values and their row-column positions. Suitable for quick construction and toarray() returns the full matrix.
Example 4: lil_matrix (List of Lists)
Python
import numpy as np
from scipy.sparse import lil_matrix
lil = lil_matrix((4, 5))
lil[0, 2] = 3
lil[0, 4] = 4
lil[1, 2] = 5
lil[1, 3] = 7
lil[3, 1] = 2
lil[3, 2] = 6
print(lil.toarray())
Output
[[0. 0. 3. 0. 4.]
[0. 0. 5. 7. 0.]
[0. 0. 0. 0. 0.]
[0. 2. 6. 0. 0.]]
Explanation: Creates a List of Lists (LIL) matrix by assigning non-zero values directly to specified row and column positions. lil_matrix allows efficient row-wise insertion
Example 5: dok_matrix (Dictionary of Keys)
Python
import numpy as np
from scipy.sparse import dok_matrix
dok = dok_matrix((4, 5))
dok[0, 2] = 3
dok[0, 4] = 4
dok[1, 2] = 5
dok[1, 3] = 7
dok[3, 1] = 2
dok[3, 2] = 6
print(dok.toarray())
Output
[[0. 0. 3. 0. 4.]
[0. 0. 5. 7. 0.]
[0. 0. 0. 0. 0.]
[0. 2. 6. 0. 0.]]
Example 6: dia_matrix (Diagonal Matrix)
Python
import numpy as np
from scipy.sparse import dia_matrix
d = np.array([[3, 0, 0, 0, 0], [0, 5, 0, 0, 0]])
offsets = np.array([0, -1])
dia = dia_matrix((d, offsets), shape=(4, 5))
print(dia.toarray())
Output
[[3 0 0 0 0]
[0 0 0 0 0]
[0 5 0 0 0]
[0 0 0 0 0]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4