+1 vote
in Programming Languages by (72.0k points)

I have declared a csr_matrix like this:

ff=csr_matrix((16064320, 66159), dtype=np.int16)

But when I try to update an element of the matrix (e.g. ff[1,0]=1), it gives the following warning message:

SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient. self._set_intXint(row, col, x.flat[0])

1 Answer

+1 vote
by (354k points)
selected by
 
Best answer

The List of Lists (lil_matrix) format is more efficient for incremental changes, such as adding or modifying elements, and is often recommended when you need to build or update the matrix incrementally. If you need to modify the matrix frequently, start with lil_matrix and then convert it to csr_matrix when you are done making changes.

Here is an example:

from scipy.sparse import lil_matrix, csr_matrix
ff_lil = lil_matrix((16064320, 66159), dtype=np.int16)
ff_lil[1, 0] = 1
ff = csr_matrix(ff_lil)


...