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

SciPy · Class

sparse.coo_matrix

Constructs a sparse matrix in coordinate (COO), also called ijv or triplet, format. It accepts dense or sparse input, an empty shape, or value arrays paired with row and column indices.

How it works

A COO matrix stores each supplied value alongside its row and column index. Create one from coordinate arrays, optionally specify its shape and dtype, and convert it to another sparse format for operations that require a different representation.

Syntax

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

Parameters

NameTypeDescription
arg1array-like, sparse matrix, tuple, or coordinate tupleInput object, shape tuple, or coordinate data used to construct the matrix.
shape2-tuple of ints, optionalOptional matrix dimensions; for coordinate input, written as (M, N).
dtypedtype, optionalOptional data type; empty matrices default to dtype='d'.
copybool, optionalWhether to copy input data when applicable.

Returns

A sparse matrix in COO format.

Key points

  • coo_matrix stores sparse entries using coordinate, or triplet, format.
  • The format is also called ijv because it associates indices with values.
  • Passing a dense matrix creates a COO matrix from its two-dimensional contents.
  • Passing another sparse matrix converts that input to COO representation.
  • A shape tuple creates an empty sparse matrix, with dtype optionally supplied.
  • Each coordinate/value triple assigns its value to the specified row and column.

Common mistakes

  • Coordinate entries do not need to be supplied in sorted order.
  • If you omit shape, SciPy derives dimensions from the largest supplied indices, which may not represent intended trailing empty rows or columns.

Example: Construct a COO matrix from coordinates

import numpy as np
from scipy.sparse import coo_matrix

row = np.array([0, 1, 1])
col = np.array([0, 1, 2])
data = np.array([4, 5, 6])
A = coo_matrix((data, (row, col)), shape=(2, 3))

print(A.toarray())

Builds a 2-by-3 COO matrix from values and their row and column coordinates, then converts it to a dense array for display.

When to use it

Build from row, column, and value arrays

Assembling sparse data from indexed observations

Create a COO representation

Converting an existing dense or sparse matrix

Stage a matrix for conversion

Preparing sparse data before changing formats

Interview questions

QWhat does coo_matrix store and how are coordinate inputs interpreted?

The constructor represents a sparse matrix in coordinate, or triplet, format. With data, row indices, and column indices, each value is placed according to A[i[k], j[k]] = data[k].

Follow-up: How can an empty matrix be created? Use coo_matrix((M, N), dtype=...) with the desired dimensions and optional dtype.

QWhich input forms can the constructor accept?

The constructor accepts a dense matrix, another sparse matrix, a shape tuple for an empty matrix, or coordinate data. The coordinate form uses coo_matrix((data, (i, j)), shape=(M, N)).

Follow-up: What happens when shape is omitted for coordinate data? The shape is inferred from the index arrays.

QWhat operations does the documented sparse matrix support?

COO supports sparse arithmetic such as addition, subtraction, multiplication, division, and matrix power. Its coordinate representation is also useful when assembling entries from indexed data.

Follow-up: Which representation should be used when indexed access or efficient arithmetic in a particular operation is required? Convert the COO object to the appropriate sparse format using its conversion methods.

  • scipy.sparse.coo_array
  • scipy.sparse.csr_matrix
  • scipy.sparse.csc_matrix
  • scipy.sparse.dok_matrix
  • scipy.sparse.lil_matrix

Source: scipy.sparse.coo_matrix — SciPy v1.13.1 Manual. Checked 2026-10-15.