In [1]:
from scipy.sparse import csr_matrix, csc_matrix, coo_matrix, lil_matrix
In [2]:
l = [[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]]
In [3]:
csr = csr_matrix(l)
csc = csc_matrix(l)
coo = coo_matrix(l)
lil = lil_matrix(l)
In [4]:
print(csr[1, 1])
In [5]:
print(csc[1, 1])
In [6]:
print(lil[1, 1])
In [7]:
# print(coo[1, 1])
# TypeError: 'coo_matrix' object is not subscriptable
In [8]:
lil[0, 0] = 10
lil[0, 1] = 100
In [9]:
print(lil)
In [10]:
print(lil.toarray())
In [11]:
lil[1, 1] = 0
In [12]:
print(lil)
In [13]:
print(lil.toarray())
In [14]:
csr[0, 0] = 10
In [15]:
# csr[0, 1] = 100
# SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient.
In [16]:
csr[1, 1] = 0
In [17]:
print(csr)
In [18]:
print(csr.toarray())