Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Class

sparse.csr_matrix

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.

How it works

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.

Syntax

scipy.sparse.csr_matrix(arg1, shape=None, dtype=None, copy=False)

Parameters

NameTypeDescription
Darray_likeDense matrix or rank-2 ndarray used as source data.
Ssparse matrixAnother sparse matrix; it is converted to CSR.
M, NintNumber of rows and columns for an empty matrix.
dtypedtype, optionalOptional element data type.
dataarray_likeStored values in coordinate or direct CSR form.
row_ind / indicesarray_likeRow indices for coordinate construction, or column indices in direct CSR form.
col_ind / indptrarray_likeColumn indices for coordinate construction; row boundaries in direct CSR form.
shape2-tuple of int, optionalOptional matrix dimensions.

Returns

A csr_matrix instance containing the supplied sparse or dense data.

Key points

  • csr_matrix is SciPy's matrix type for compressed row-oriented sparse storage.
  • A dense matrix or two-dimensional NumPy array can be used as the constructor input.
  • You can allocate an empty matrix with dimensions M by N and optionally choose its dtype.
  • Coordinate construction pairs each stored value with its row and column indices.
  • For coordinate input, the kth value is assigned at the kth row-index and column-index pair.
  • In direct CSR form, indptr marks the segment of indices belonging to row i.
  • The class participates in sparse arithmetic rather than requiring conversion to a dense matrix first.

Common mistakes

  • Omitting shape makes dimensions depend on the supplied index arrays, so provide shape when the intended matrix extent is not fully implied.
  • Passing an existing sparse matrix converts it to CSR rather than preserving its original sparse format.

Example: Construct a CSR matrix from coordinates

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.

When to use it

Memory-efficient storage

Large matrices with relatively few nonzero entries.

Sparse arithmetic

Row-oriented numerical computations on sparse data.

Coordinate-to-CSR conversion

Importing sparse data represented as coordinates.

Direct CSR construction

Building a matrix from precomputed CSR arrays.

Interview questions

QHow can csr_matrix be constructed from coordinate data?

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].

QWhat do data, indices, and indptr mean in CSR storage?

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.

QWhat operations and input forms does the class support?

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_array
  • scipy.sparse.issparse
  • scipy.sparse.coo_matrix
  • scipy.sparse.csc_matrix
  • scipy.sparse.bsr_matrix

Source: csr_matrix — SciPy v1.18.0 Manual. Checked 2026-10-15.