sparse.dok

Dictionary Of Keys based matrix

Module Contents

Classes

dok_matrix(self,arg1,shape=None,dtype=None,copy=False) Dictionary Of Keys based sparse matrix.

Functions

_is_sequence(x)
isspmatrix_dok(x) Is x of dok_matrix type?
_prod(x) Product of a list of numbers; ~40x faster vs np.prod for Python tuples
_is_sequence(x)
class dok_matrix(arg1, shape=None, dtype=None, copy=False)

Dictionary Of Keys based sparse matrix.

This is an efficient structure for constructing sparse matrices incrementally.

This can be instantiated in several ways:
dok_matrix(D)
with a dense matrix, D
dok_matrix(S)
with a sparse matrix, S
dok_matrix((M,N), [dtype])
create the matrix with initial shape (M,N) dtype is optional, defaulting to dtype=’d’
dtype : dtype
Data type of the matrix
shape : 2-tuple
Shape of the matrix
ndim : int
Number of dimensions (this is always 2)
nnz
Number of nonzero elements

Sparse matrices can be used in arithmetic operations: they support addition, subtraction, multiplication, division, and matrix power.

Allows for efficient O(1) access of individual elements. Duplicates are not allowed. Can be efficiently converted to a coo_matrix once constructed.

>>> import numpy as np
>>> from scipy.sparse import dok_matrix
>>> S = dok_matrix((5, 5), dtype=np.float32)
>>> for i in range(5):
...     for j in range(5):
...         S[i, j] = i + j    # Update element
__init__(arg1, shape=None, dtype=None, copy=False)
update(val)
_update(data)

An update method for dict data defined for direct access to dok_matrix data. Main purpose is to be used for effcient conversion from other spmatrix classes. Has no checking if data is valid.

getnnz(axis=None)
count_nonzero()
__len__()
get(key, default=0.0)

This overrides the dict.get method, providing type checking but otherwise equivalent functionality.

__getitem__(index)

If key=(i, j) is a pair of integers, return the corresponding element. If either i or j is a slice or sequence, return a new sparse matrix with just these elements.

_getitem_ranges(i_indices, j_indices, shape)
__setitem__(index, x)
__add__(other)
__radd__(other)
__neg__()
_mul_scalar(other)
_mul_vector(other)
_mul_multivector(other)
__imul__(other)
__truediv__(other)
__itruediv__(other)
__reduce__()
transpose(axes=None, copy=False)
conjtransp()

Return the conjugate transpose.

copy()
getrow(i)

Returns the i-th row as a (1 x n) DOK matrix.

getcol(j)

Returns the j-th column as a (m x 1) DOK matrix.

tocoo(copy=False)
todok(copy=False)
tocsc(copy=False)
resize(shape)

Resize the matrix in-place to dimensions given by shape.

Any non-zero elements that lie outside the new shape are removed.

isspmatrix_dok(x)

Is x of dok_matrix type?

x
object to check for being a dok matrix
bool
True if x is a dok matrix, False otherwise
>>> from scipy.sparse import dok_matrix, isspmatrix_dok
>>> isspmatrix_dok(dok_matrix([[5]]))
True
>>> from scipy.sparse import dok_matrix, csr_matrix, isspmatrix_dok
>>> isspmatrix_dok(csr_matrix([[5]]))
False
_prod(x)

Product of a list of numbers; ~40x faster vs np.prod for Python tuples