Memory-efficient storage
Large matrices with relatively few nonzero entries.
SciPy · Class
scipy.sparse.csr_matrix represents a matrix in compressed sparse row format. It supports construction from dense data, another sparse matrix, dimensions, coordinate triples, or the three arrays used by CSR storage.
A CSR matrix stores nonzero values row by row. It can be created from dense or sparse input, coordinate triples, dimensions, or the internal data, indices, and indptr arrays; the latter describe each row through slices into the storage arrays.
scipy.sparse.csr_matrix(arg1, shape=None, dtype=None, copy=False)| Name | Type | Description |
|---|---|---|
D | array_like | Dense matrix or rank-2 ndarray used as source data. |
S | sparse matrix | Another sparse matrix; it is converted to CSR. |
M, N | int | Number of rows and columns for an empty matrix. |
dtype | dtype, optional | Optional element data type. |
data | array_like | Stored values in coordinate or direct CSR form. |
row_ind / indices | array_like | Row indices for coordinate construction, or column indices in direct CSR form. |
col_ind / indptr | array_like | Column indices for coordinate construction; row boundaries in direct CSR form. |
shape | 2-tuple of int, optional | Optional matrix dimensions. |
A csr_matrix instance containing the supplied sparse or dense data.
import numpy as np
from scipy.sparse import csr_matrix
A = csr_matrix((np.array([1, 2, 3]),
(np.array([0, 0, 1]), np.array([0, 2, 1]))),
shape=(2, 3))
print(A.toarray())Builds a 2-by-3 CSR matrix from values and their row and column coordinates, then displays its dense form.
Large matrices with relatively few nonzero entries.
Row-oriented numerical computations on sparse data.
Importing sparse data represented as coordinates.
Building a matrix from precomputed CSR arrays.
The coordinate form accepts data together with row_ind and col_ind, placing each value at the corresponding row and column. The shape can optionally be supplied; otherwise it is inferred from the index arrays.
Follow-up: How would you construct CSR directly? Use data, indices, and indptr, where each row's column indices and values occupy the slice from indptr[i] through indptr[i+1].
The standard internal representation uses data for stored values, indices for their column positions, and indptr to delimit each row's slice in those arrays. The matrix shape may be passed explicitly or inferred from the index arrays.
Follow-up: Why is indptr useful? It gives constant-time boundaries for locating the stored entries belonging to any row.
It represents a compressed sparse row matrix and supports arithmetic such as addition, subtraction, multiplication, division, and matrix power. Construction can start from a dense rank-2 array, another sparse matrix, dimensions, coordinate data, or CSR arrays.
Follow-up: When converting another sparse object, what happens? The documentation specifies that passing a sparse matrix is equivalent to calling its tocsr conversion.
scipy.sparse.csr_arrayscipy.sparse.issparsescipy.sparse.coo_matrixscipy.sparse.csc_matrixscipy.sparse.bsr_matrix