bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / sparse / _coo / coo_matrix
ABCMeta
scipy.sparse._coo:coo_matrix
source: build-install/usr/lib/python3.14/site-packages/scipy/sparse/_coo.py:1823
Signature
def coo_matrix(*args, **kwargs)Members
Summary
A sparse matrix in COOrdinate format.
Extended Summary
Also known as the 'ijv' or 'triplet' format.
This can be instantiated in several ways:
coo_matrix(D)
where D is a 2-D ndarray
coo_matrix(S)
with another sparse array or matrix S (equivalent to S.tocoo())
coo_matrix((M, N), [dtype])
to construct an empty matrix with shape (M, N) dtype is optional, defaulting to dtype='d'.
coo_matrix((data, (i, j)), [shape=(M, N)])
to construct from three arrays:
data[:] the entries of the matrix, in any order
i[:] the row indices of the matrix entries
j[:] the column indices of the matrix entries
Where
A[i[k], j[k]] = data[k]. When shape is not specified, it is inferred from the index arrays
Attributes
data: ndarrayCOO format data array of the sparse matrix
coords: tuple of ndarrayCOO format tuple of index matrix
has_canonical_format: boolWhether the matrix has sorted coordinates and no duplicates
dtype: dtypeData type of the matrix
shape: tuple of integersShape of the matrix
ndim: intNumber of dimensions of the matrix
format: strThree letter code for the format of the matrix storage, e.g. 'coo'
nnz: intNumber of values stored in the matrix
size: intNumber of values stored in the matrix
T: coo_matrixThe transpose of the matrix
mT: coo_matrixThe matrix transpose
Notes
Sparse matrices can be used in arithmetic operations: they support addition, subtraction, multiplication, division, and matrix power.
Advantages of the COO format
facilitates fast conversion among sparse formats
permits duplicate entries (see example)
very fast conversion to and from CSR/CSC formats
Disadvantages of the COO format
does not directly support:
arithmetic operations
slicing
Intended Usage
COO is a fast format for constructing sparse matrices
Once a COO matrix has been constructed, convert to CSR or CSC format for fast arithmetic and matrix vector operations
By default when converting to CSR or CSC format, duplicate (i,j) entries will be summed together. This facilitates efficient construction of finite element matrices and the like. (see example)
Canonical format
Entries and coordinates sorted by row, then column.
There are no duplicate entries (i.e. duplicate (i,j) locations)
Data arrays MAY have explicit zeros.
Examples
import numpy as np from scipy.sparse import coo_matrix coo_matrix((3, 4), dtype=np.int8).toarray()✓
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]], dtype=int8)
row = np.array([0, 3, 1, 0]) col = np.array([0, 3, 1, 2]) data = np.array([4, 5, 7, 9]) coo_matrix((data, (row, col)), shape=(4, 4)).toarray()✓
array([[4, 0, 9, 0],
[0, 7, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 5]])
row = np.array([0, 0, 1, 3, 1, 0, 0]) col = np.array([0, 2, 1, 3, 1, 0, 0]) data = np.array([1, 1, 1, 1, 1, 1, 1]) coo = coo_matrix((data, (row, col)), shape=(4, 4))✓
np.max(coo.data)
✗np.int64(1)
coo.toarray()
✓array([[3, 0, 1, 0],
[0, 2, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 1]])
Aliases
scipy.sparse.coo_matrix