efficient for constructing sparse matrices incrementally
efficient O(1) access to individual elements
flexible slicing, changing sparsity structure is efficient
can be efficiently converted to a coo_matrix once constructed
slow arithmetics (for loops with dict.iteritems())
create a DOK matrix element by element:
>>> mtx = sparse.dok_matrix((5, 5), dtype=np.float64)
>>> mtx
<5x5 sparse matrix of type '<type 'numpy.float64'>'
with 0 stored elements in Dictionary Of Keys format>
>>> for ir in range(5):
... for ic in range(5):
... mtx[ir, ic] = 1.0 * (ir != ic)
>>> mtx
<5x5 sparse matrix of type '<type 'numpy.float64'>'
with 20 stored elements in Dictionary Of Keys format>
>>> mtx.todense()
matrix([[ 0., 1., 1., 1., 1.],
[ 1., 0., 1., 1., 1.],
[ 1., 1., 0., 1., 1.],
[ 1., 1., 1., 0., 1.],
[ 1., 1., 1., 1., 0.]])
slicing and indexing:
>>> mtx[1, 1]
0.0
>>> mtx[1, 1:3]
<1x2 sparse matrix of type '<type 'numpy.float64'>'
with 1 stored elements in Dictionary Of Keys format>
>>> mtx[1, 1:3].todense()
matrix([[ 0., 1.]])
>>> mtx[[2,1], 1:3].todense()
Traceback (most recent call last):
...
NotImplementedError: fancy indexing supported over one axis only