In [1]:
import numpy as np
from scipy.sparse import csr_matrix, csc_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)
lil = lil_matrix(l)
In [4]:
print(csr[0, :])
In [5]:
print(csr[0, :].toarray())
In [6]:
print(csr[0].toarray())
In [7]:
print(csr[:, 0])
In [8]:
print(csr[:, 0].toarray())
In [9]:
print(csr[1:3, 1:3])
In [10]:
print(csr[1:3, 1:3].toarray())
In [11]:
print(csr[:, ::2])
In [12]:
print(csr[:, ::2].toarray())
In [13]:
print(type(csr[0]))
In [14]:
print(type(csc[0]))
In [15]:
print(type(lil[0]))
In [16]:
print(type(csr[:, 0]))
In [17]:
print(type(csc[:, 0]))
In [18]:
print(type(lil[:, 0]))
In [19]:
print(type(csr[1:3, 1:3]))
In [20]:
print(type(csc[1:3, 1:3]))
In [21]:
print(type(lil[1:3, 1:3]))
In [22]:
csr_slice = csr[1:3, 1:3]
csr_slice[0, 0] = 100
In [23]:
print(csr.toarray())
In [24]:
print(csr_slice.toarray())
In [25]:
csc_slice = csc[1:3, 1:3]
csc_slice[0, 0] = 100
In [26]:
print(csc.toarray())
In [27]:
print(csc_slice.toarray())
In [28]:
lil_slice = lil[1:3, 1:3]
lil_slice[0, 0] = 100
In [29]:
print(lil.toarray())
In [30]:
print(lil_slice.toarray())
In [31]:
lil[0] = [10, 20, 30, 40]
In [32]:
print(lil)
In [33]:
print(lil.toarray())
In [34]:
lil[1:3, 1:3] = np.arange(4).reshape(2, 2) * 100
In [35]:
print(lil)
In [36]:
print(lil.toarray())
In [37]:
lil[:, 0] = csr[:, 3]
In [38]:
print(lil)
In [39]:
print(lil.toarray())
In [40]:
# lil[1:3, 1:3] = [10, 20, 30, 40]
# ValueError: shape mismatch: objects cannot be broadcast to a single shape
In [41]:
csr[0] = [0, 0, 0, 100]
In [42]:
print(csr)
In [43]:
print(csr.toarray())