SciPy Sparse Matrix
A sparse matrix refers to a matrix in which the vast majority of values are zero in numerical analysis. Conversely, if most elements are non-zero, the matrix is dense.
Large sparse matrices frequently appear when solving linear models in science and engineering.

In the figure above, the left side is a sparse matrix containing many zero elements, while the right side is a dense matrix whose elements are mostly non-zero.
Here is a simple example:
The sparse matrix above contains only 9 non-zero elements and 26 zero elements. Its sparsity is 74% and its density is 26%.
SciPy's scipy.sparse module provides functions for working with sparse matrices.
We mainly use the following two types of sparse matrices:
- CSC - Compressed Sparse Column, compressed by column.
- CSR - Compressed Sparse Row, compressed by row.
In this chapter we mainly use CSR matrices.
CSR Matrix
We can create a CSR matrix by passing an array to the scipy.sparse.csr_matrix() function.
Example
Create a CSR matrix.
import
numpy
as
np
from
scipy.
sparse
import
csr_matrix
arr
=
np.
array
(
[
0
,
0
,
0
,
0
,
0
,
1
,
1
,
0
,
2
]
)
(
csr_matrix
(
arr
)
)
The output of the code above is:
(0, 5) 1
(0, 6) 1
(0, 8) 2
Result analysis:
- First row: in the first row of the matrix (index 0), there is a value 1 at the sixth position (index 5).
- Second row: in the first row of the matrix (index 0), there is a value 1 at the seventh position (index 6).
- Third row: in the first row of the matrix (index 0), there is a value 2 at the ninth position (index 8).
CSR Matrix Methods
We can use the data attribute to view the stored data (excluding zero elements):
Example
import
numpy
as
np
from
scipy.
sparse
import
csr_matrix
arr
=
np.
array
(
[
[
0
,
0
,
0
]
,
[
0
,
0
,
1
]
,
[
1
,
0
,
2
]
]
)
(
csr_matrix
(
arr
)
.
data
)
The output of the code above is:
[1 1 2]
Use the count_nonzero() method to count the total number of non-zero elements:
Example
import
numpy
as
np
from
scipy.
sparse
import
csr_matrix
arr
=
np.
array
(
[
[
0
,
0
,
0
]
,
[
0
,
0
,
1
]
,
[
1
,
0
,
2
]
]
)
(
csr_matrix
(
arr
)
.
count_nonzero
(
)
)
The output of the code above is:
3
Use the eliminate_zeros() method to remove zero elements from the matrix:
Example
import
numpy
as
np
from
scipy.
sparse
import
csr_matrix
arr
=
np.
array
(
[
[
0
,
0
,
0
]
,
[
0
,
0
,
1
]
,
[
1
,
0
,
2
]
]
)
mat
=
csr_matrix
(
arr
)
mat.
eliminate_zeros
(
)
(
mat
)
The output of the code above is:
(1, 2) 1
(2, 0) 1
(2, 2) 2
Use the sum_duplicates() method to remove duplicates:
Example
import
numpy
as
np
from
scipy.
sparse
import
csr_matrix
arr
=
np.
array
(
[
[
0
,
0
,
0
]
,
[
0
,
0
,
1
]
,
[
1
,
0
,
2
]
]
)
mat
=
csr_matrix
(
arr
)
mat.
sum_duplicates
(
)
(
mat
)
The output of the code above is:
(1, 2) 1
(2, 0) 1
(2, 2) 2
To convert CSR to CSC, use the tocsc() method:
Example
import
numpy
as
np
from
scipy.
sparse
import
csr_matrix
arr
=
np.
array
(
[
[
0
,
0
,
0
]
,
[
0
,
0
,
1
]
,
[
1
,
0
,
2
]
]
)
newarr
=
csr_matrix
(
arr
)
.
tocsc
(
)
(
newarr
)
(2, 0) 1
(1, 2) 1
(2, 2) 2