How to Create a Sparse Matrix with SciPy
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:
- Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store only those elements.
- Computing time: Computing time can be saved by logically designing a data structure traversing only non-zero elements.
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 SciPy
The 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. |
---|
Examples
Example 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]]
Similar Reads
Python program to Convert a Matrix to Sparse Matrix Converting a matrix to a sparse matrix involves storing only non-zero elements along with their row and column indices to save memory.Using a DictionaryConverting a matrix to a sparse matrix using a dictionary involves storing only the non-zero elements of the matrix, with their row and column indic
2 min read
Python - Matrix creation of n*n Matrices are fundamental structures in programming and are widely used in various domains including mathematics, machine learning, image processing, and simulations. Creating an nÃn matrix efficiently is an important step for these applications. This article will explore multiple ways to create such
3 min read
Python Program to Check if a given matrix is sparse or not A matrix is a two-dimensional data object having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix are 0 then we say that the matrix is sparse. Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elem
4 min read
How to create an array of zeros in Python? Creating an array of zeros in Python involves generating a collection filled entirely with zero values. For example, if we need to create a list of 5 zeros, the list would look like [0, 0, 0, 0, 0]. Letâs explore some common approaches to create zero-filled arrays.Using numpy.zeros()numpy.zeros() cr
4 min read
Python Program to Construct n*m Matrix from List We are given a list we need to construct a n*m matrix from that list. For example, a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] we need to construct a 3*4 matrix so that resultant output becomes [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] .Using List ComprehensionThis method uses list comprehension
3 min read
Initialize Matrix in Python Initializing a matrix in Python refers to creating a 2D structure (list of lists) to store data in rows and columns. For example, a 3x2 matrix can be represented as three rows, each containing two elements. Letâs explore different methods to do this efficientely.Using list comprehensionList comprehe
2 min read