scipy.sparse.dok_matrix¶
-
class
scipy.sparse.
dok_matrix
(arg1, shape=None, dtype=None, copy=False)[source]¶ 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’
Notes
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.
Examples
>>> 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
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 Number of nonzero elements Methods
clear
(() -> None. Remove all items from D.)fromkeys
(...)v defaults to None. has_key
((k) -> True if D has a key k, else False)items
(() -> list of D’s (key, value) pairs, ...)iteritems
(() -> an iterator over the (key, ...)iterkeys
(() -> an iterator over the keys of D)itervalues
(...)keys
(() -> list of D’s keys)pop
((k[,d]) -> v, ...)If key is not found, d is returned if given, otherwise KeyError is raised popitem
(() -> (k, v), ...)2-tuple; but raise KeyError if D is empty. setdefault
((k[,d]) -> D.get(k,d), ...)update
(([E, ...)If E present and has a .keys() method, does: for k in E: D[k] = E[k] values
(() -> list of D’s values)viewitems
(...)viewkeys
(...)viewvalues
(...)