Compressed Sparse Row Format (CSR)#
row oriented
three NumPy arrays:
indices
,indptr
,data
indices
is array of column indicesdata
is array of corresponding nonzero valuesindptr
points to row starts inindices
anddata
length of
indptr
isn_row + 1
, last item = number of values = length of bothindices
anddata
nonzero values of the
i
-th row aredata[indptr[i]:indptr[i + 1]]
with column indicesindices[indptr[i]:indptr[i + 1]]
item
(i, j)
can be accessed asdata[indptr[i] + k]
, wherek
is position ofj
inindices[indptr[i]:indptr[i + 1]]
subclass of
_cs_matrix
(common CSR/CSC functionality)subclass of
_data_matrix
(sparse array classes with.data
attribute)
fast matrix vector products and other arithmetic (sparsetools)
constructor accepts:
dense array/matrix
sparse array/matrix
shape tuple (create empty array)
(data, coords)
tuple(data, indices, indptr)
tuple
efficient row slicing, row-oriented operations
slow column slicing, expensive changes to the sparsity structure
use:
actual computations (most linear solvers support this format)
Examples#
Create empty CSR array:#
mtx = sp.sparse.csr_array((3, 4), dtype=np.int8)
mtx.toarray()
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]], dtype=int8)
Create using (data, coords)
tuple:#
row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
mtx = sp.sparse.csr_array((data, (row, col)), shape=(3, 3))
mtx
<Compressed Sparse Row sparse array of dtype 'int64'
with 6 stored elements and shape (3, 3)>
mtx.toarray()
array([[1, 0, 2],
[0, 0, 3],
[4, 5, 6]])
mtx.data
array([1, 2, 3, 4, 5, 6])
mtx.indices
array([0, 2, 2, 0, 1, 2])
mtx.indptr
array([0, 2, 3, 6])
Create using (data, indices, indptr)
tuple:#
data = np.array([1, 2, 3, 4, 5, 6])
indices = np.array([0, 2, 2, 0, 1, 2])
indptr = np.array([0, 2, 3, 6])
mtx = sp.sparse.csr_array((data, indices, indptr), shape=(3, 3))
mtx.toarray()
array([[1, 0, 2],
[0, 0, 3],
[4, 5, 6]])