The coords parameter contains the indices where the data is nonzero,
and the data parameter contains the data corresponding to those indices.
For example, the following code will generate a \(5 \times 5\) diagonal
matrix:

In general coords should be a (ndim,nnz) shaped
array. Each row of coords contains one dimension of the
desired sparse array, and each column contains the index
corresponding to that nonzero element. data contains
the nonzero elements of the array corresponding to the indices
in coords. Its shape should be (nnz,).

If data is the same across all the coordinates, it can be passed
in as a scalar. For example, the following produces the \(4 \times 4\)
identity matrix:

You can, and should, pass in numpy.ndarray objects for
coords and data.

In this case, the shape of the resulting array was determined from
the maximum index in each dimension. If the array extends beyond
the maximum index in coords, you should supply a shape
explicitly. For example, if we did the following without the
shape keyword argument, it would result in a
\(4 \times 5\) matrix, but maybe we wanted one that was actually
\(5 \times 5\).

COO arrays support arbitrary fill values. Fill values are the “default”
value, or value to not store. This can be given a value other than zero. For
example, the following builds a (bad) representation of a \(2 \times 2\)
identity matrix. Note that not all operations are supported for operations
with nonzero fill values.

It’s possible to build COO arrays from DOK arrays, if it is not
easy to construct the coords and data in a simple way. DOK
arrays provide a simple builder interface to build COO arrays, but at
this time, they can do little else.

You can get started by defining the shape (and optionally, datatype) of the
DOK array. If you do not specify a dtype, it is inferred from the value
dictionary or is set to dtype('float64') if that is not present.

s=DOK((6,5,2))s2=DOK((2,3,4),dtype=np.uint8)

After this, you can build the array by assigning arrays or scalars to elements
or slices of the original array. Broadcasting rules are followed.

s[1:3,3:1:-1]=[[6,5]]

At the end, you can convert the DOK array to a COO array, and
perform arithmetic or other operations on it.

s3=COO(s)

In addition, it is possible to access single elements of the DOK array
using normal Numpy indexing.