Skip to content Skip to sidebar Skip to footer

Csr Scipy Matrix Does Not Update After Updating Its Values

I have the following code in python: import numpy as np from scipy.sparse import csr_matrix M = csr_matrix(np.ones([2, 2],dtype=np.int32)) print(M) print(M.data.shape) for i in ran

Solution 1:

Yes, you are trying to change elements of the matrix one by one. :)

Ok, it does work that way, though if you changed things the other way (setting a 0 to nonzero) you will get an Efficiency warning.

To keep your kind of change fast, it only changes the value in the M.data array, and does not recalculate the indices. You have to invoke a separate csr_matrix.eliminate_zeros method the clean up the matrix. To get best speed call this once at the end of the loop.

There is a csr_matrix.setdiag method that lets you set the whole diagonal with one call. It still needs the cleanup.

In [1633]: M=sparse.csr_matrix(np.arange(9).reshape(3,3))
In [1634]: M
Out[1634]: 
<3x3 sparse matrix of type '<class 'numpy.int32'>'with8 stored elements in Compressed Sparse Row format>In [1635]: M.A
Out[1635]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]], dtype=int32)
In [1636]: M.setdiag(0)
/usr/local/lib/python3.5/dist-packages/scipy/sparse/compressed.py:730: SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient.
  SparseEfficiencyWarning)
In [1637]: M
Out[1637]: 
<3x3 sparse matrix of type '<class 'numpy.int32'>'with9 stored elements in Compressed Sparse Row format>In [1638]: M.A
Out[1638]: 
array([[0, 1, 2],
       [3, 0, 5],
       [6, 7, 0]])
In [1639]: M.data
Out[1639]: array([0, 1, 2, 3, 0, 5, 6, 7, 0])
In [1640]: M.eliminate_zeros()
In [1641]: M
Out[1641]: 
<3x3 sparse matrix of type '<class 'numpy.int32'>'with6 stored elements in Compressed Sparse Row format>In [1642]: M.data
Out[1642]: array([1, 2, 3, 5, 6, 7])

Post a Comment for "Csr Scipy Matrix Does Not Update After Updating Its Values"