Data type of the array
Shape of the array
Number of dimensions (this is always 2)
Number of stored values, including explicit zeros
COO format data array of the array
COO format row index array of the array
COO format column index array of the array
Also known as the 'ijv' or 'triplet' format.
This can be instantiated in several ways:
coo_array(D)
with a dense array D
coo_array(S)
with another sparse array S (equivalent to S.tocoo())
coo_array((M, N), [dtype])
to construct an empty array with shape (M, N) dtype is optional, defaulting to dtype='d'.
coo_array((data, (i, j)), [shape=(M, N)])
to construct from three arrays:
data[:] the entries of the array, in any order
i[:] the row indices of the array entries
j[:] the column indices of the array entries
Where A[i[k], j[k]] = data[k]
. When shape is not specified, it is inferred from the index arrays
Sparse arrays can be used in arithmetic operations: they support addition, subtraction, multiplication, division, and array 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 arrays
Once a array has been constructed, convert to CSR or CSC format for fast arithmetic and array 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 arrays and the like. (see example)
A sparse array in COOrdinate format.
>>> # Constructing an empty array
... from scipy.sparse import coo_array
... coo_array((3, 4), dtype=np.int8).toarray() array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], dtype=int8)
>>> # Constructing a array using ijv format
... row = np.array([0, 3, 1, 0])
... col = np.array([0, 3, 1, 2])
... data = np.array([4, 5, 7, 9])
... coo_array((data, (row, col)), shape=(4, 4)).toarray() array([[4, 0, 9, 0], [0, 7, 0, 0], [0, 0, 0, 0], [0, 0, 0, 5]])
>>> # Constructing a array with duplicate indices
... 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_array((data, (row, col)), shape=(4, 4))
... # Duplicate indices are maintained until implicitly or explicitly summed
... np.max(coo.data) 1
>>> coo.toarray() array([[3, 0, 1, 0], [0, 2, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]])See :
The following pages refer to to this document either explicitly or contain code examples using this.
scipy.sparse._arrays.coo_array
networkx.convert_matrix.to_scipy_sparse_array
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them