Home » Python » python – 'csc_matrix' object does not support item assignment-Exceptionshub

python – 'csc_matrix' object does not support item assignment-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I am using sparse matrices using CuPy. If I define the csc_matrix using scipy, I can assign the matrix after its definition. But, I am not able to do it with CuPy. Below is my code:

data = np.ones((3,4))
diags = np.array([-1,0,1])
M= spdiags(data, diags, 4, 4)
M = sp.csc_matrix(M)
print(M[0,0])
M[0,0] = 5
print(M[0,0])

I get 1.0, 5.0 as the output. If I try to do same thing in CuPy I get an errot.

import cupy as cp
import cupyx as cpx
datac = cp.ones((3,4))
diagsc = cp.array(diags)
Mc = cpx.scipy.sparse.spdiags(datac, diagsc, 4, 4)
Mc = cpx.scipy.sparse.csc_matrix(Mc)

Mc = cpx.scipy.sparse.csc_matrix(Mc)
print(Mc.get()[0,0])
Mc[0,0] = 5
print(Mc[0,0])

I get 1.0 as th eoutput and then the error: ‘csc_matrix’ object does not support item assignment.

Am I doing something wrong?

Also, the assignment operation with csc_matrix is expensive in SciPy. But, I can use lil_matrix in scipy which is much faster. Does CuPy have a sparse lil_matirx option?

Thank you.

How to&Answers: