+2 votes
in Programming Languages by (15.0k points)
I have a 2D Numpy array in which most of the elements are 0. How can I convert the array into a sparse matrix?

1 Answer

0 votes
by (26.2k points)
edited by

If you want to convert a 2D array to a CSR sparse matrix, you need the following parameters:

  • indices of the 2D array cell where value is non-zero,
  • list of non-zero values, and
  • size of the sparse matrix.

Here is an example:

import numpy as np
from scipy.sparse import csr_matrix

# Numpy array with a few non-zero values
a = np.array([[0,0,0,0,1], [0,2,0,0,0], [0,0,0,3,0], [4,0,0,0,0], [0,0,5,0,6], [0,0,7,0,0], [0,8,0,9,0]])

# find the indices of the cell where value is non-zero
row, col = np.where(a!=0)
cellValue = np.array([a[i][j] for i,j in zip(row, col)])

# create a CSR sparse matrix of size 7x5
X = csr_matrix((cellValue, (row, col)), shape=(a.shape[0], a.shape[1]), dtype=np.int8)