{ } Raw JSON

bundles / scipy 1.17.1 / scipy / sparse / _coo / coo_matrix

ABCMeta

scipy.sparse._coo:coo_matrix

source: /scipy/sparse/_coo.py :1766

Signature

def   coo_matrix ( arg1 shape = None dtype = None copy = False * maxprint = None )

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

dtype : dtype

Data type of the matrix

shape : 2-tuple

Shape of the matrix

ndim : int

Number of dimensions (this is always 2)

nnz
size
data

COO format data array of the matrix

row

COO format row index array of the matrix

col

COO format column index array of the matrix

has_canonical_format : bool

Whether the matrix has sorted indices and no duplicates

format
T

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()
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()
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)
coo.toarray()

Aliases

  • scipy.sparse.coo_matrix

Referenced by