Module « scipy.sparse »
Classe « coo_matrix »
Informations générales
Héritage
builtins.object
_minmax_mixin
builtins.object
spmatrix
_data_matrix
coo_matrix
Définition
class coo_matrix(_data_matrix, _minmax_mixin):
Description [extrait de coo_matrix.__doc__]
A sparse matrix in COOrdinate format.
Also known as the 'ijv' or 'triplet' format.
This can be instantiated in several ways:
coo_matrix(D)
with a dense matrix D
coo_matrix(S)
with another sparse matrix S (equivalent to S.tocoo())
coo_matrix((M, N), [dtype])
to construct an empty matrix with shape (M, N)
dtype is optional, defaulting to dtype='d'.
coo_matrix((data, (i, j)), [shape=(M, N)])
to construct from three arrays:
1. data[:] the entries of the matrix, in any order
2. i[:] the row indices of the matrix entries
3. j[:] the column indices of the matrix entries
Where ``A[i[k], j[k]] = data[k]``. When shape is not
specified, it is inferred from the index arrays
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 stored values, including explicit zeros
data
COO format data array of the matrix
row
COO format row index array of the matrix
col
COO format column index array of the matrix
Notes
-----
Sparse matrices can be used in arithmetic operations: they support
addition, subtraction, multiplication, division, and matrix 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 matrices
- Once a matrix has been constructed, convert to CSR or
CSC format for fast arithmetic and matrix 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 matrices and the like. (see example)
Examples
--------
>>> # Constructing an empty matrix
>>> from scipy.sparse import coo_matrix
>>> coo_matrix((3, 4), dtype=np.int8).toarray()
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]], dtype=int8)
>>> # Constructing a matrix 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_matrix((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 matrix 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_matrix((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]])
Constructeur(s)
Liste des attributs statiques
Liste des propriétés
dtype | |
nnz | Number of stored values, including explicit zeros. [extrait de __doc__] |
shape | Get shape of a matrix. [extrait de __doc__] |
Liste des opérateurs
Opérateurs hérités de la classe _data_matrix
__imul__, __itruediv__, __neg__
Liste des opérateurs
Opérateurs hérités de la classe spmatrix
__add__, __eq__, __ge__, __gt__, __iadd__, __isub__, __le__, __lt__, __matmul__, __mul__, __ne__, __pow__, __radd__, __rmul__, __rsub__, __rtruediv__, __sub__, __truediv__
Liste des méthodes
Toutes les méthodes
Méthodes d'instance
Méthodes statiques
Méthodes dépréciées
diagonal(self, k=0) |
Returns the kth diagonal of the matrix. [extrait de diagonal.__doc__] |
eliminate_zeros(self) |
Remove zero entries from the matrix [extrait de eliminate_zeros.__doc__] |
getnnz(self, axis=None) |
Number of stored values, including explicit zeros. [extrait de getnnz.__doc__] |
reshape(self, *args, **kwargs) |
reshape(self, shape, order='C', copy=False) [extrait de reshape.__doc__] |
resize(self, *shape) |
Resize the matrix in-place to dimensions given by ``shape`` [extrait de resize.__doc__] |
sum_duplicates(self) |
Eliminate duplicate matrix entries by adding them together [extrait de sum_duplicates.__doc__] |
toarray(self, order=None, out=None) |
See the docstring for `spmatrix.toarray`. [extrait de toarray.__doc__] |
tocoo(self, copy=False) |
Convert this matrix to COOrdinate format. [extrait de tocoo.__doc__] |
tocsc(self, copy=False) |
Convert this matrix to Compressed Sparse Column format [extrait de tocsc.__doc__] |
tocsr(self, copy=False) |
Convert this matrix to Compressed Sparse Row format [extrait de tocsr.__doc__] |
todia(self, copy=False) |
Convert this matrix to sparse DIAgonal format. [extrait de todia.__doc__] |
todok(self, copy=False) |
Convert this matrix to Dictionary Of Keys format. [extrait de todok.__doc__] |
transpose(self, axes=None, copy=False) |
|
Méthodes héritées de la classe _minmax_mixin
__init_subclass__, __subclasshook__, argmax, argmin, max, min
Méthodes héritées de la classe _data_matrix
__abs__, __round__, astype, conj, copy, count_nonzero, power
Méthodes héritées de la classe spmatrix
__bool__, __div__, __getattr__, __idiv__, __iter__, __len__, __nonzero__, __rdiv__, __repr__, __rmatmul__, __str__, asformat, asfptype, conjugate, dot, get_shape, getcol, getformat, getH, getmaxprint, getrow, maximum, mean, minimum, multiply, nonzero, set_shape, setdiag, sum, tobsr, todense, tolil
Méthodes héritées de la classe object
__delattr__,
__dir__,
__format__,
__getattribute__,
__hash__,
__reduce__,
__reduce_ex__,
__setattr__,
__sizeof__
Améliorations / Corrections
Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.
Emplacement :
Description des améliorations :