Vous êtes un professionnel et vous avez besoin d'une formation ?
Programmation Python
Les compléments
Voir le programme détaillé
Module « scipy.sparse »
Classe « coo_matrix »
Informations générales
Héritage
builtins.object
_minmax_mixin
builtins.object
_spbase
_data_matrix
_coo_base
builtins.object
spmatrix
coo_matrix
Définition
class coo_matrix(spmatrix, _coo_base):
help(coo_matrix)
A sparse matrix in COOrdinate format.
Also known as the 'ijv' or 'triplet' format.
This can be instantiated in several ways:
coo_matrix(D)
where D is a 2-D ndarray
coo_matrix(S)
with another sparse array or 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
size
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
has_canonical_format : bool
Whether the matrix has sorted indices and no duplicates
format
T
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 COO 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)
Canonical format
- Entries and coordinates sorted by row, then column.
- There are no duplicate entries (i.e. duplicate (i,j) locations)
- Data arrays MAY have explicit zeros.
Examples
--------
>>> # Constructing an empty matrix
>>> import numpy as np
>>> 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 coordinates
>>> 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 coordinates 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 propriétés
col | |
dtype | |
format | Format string for matrix. [extrait de format.__doc__] |
imag | |
ndim | |
nnz | Number of stored values, including explicit zeros. [extrait de nnz.__doc__] |
real | |
row | |
shape | Shape of the matrix [extrait de shape.__doc__] |
size | Number of stored values. [extrait de size.__doc__] |
T | Transpose. [extrait de T.__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 _spbase
__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
Méthodes héritées de la classe _coo_base
__init_subclass__, __subclasshook__, count_nonzero, diagonal, dot, eliminate_zeros, reshape, resize, sum_duplicates, tensordot, toarray, tocoo, tocsc, tocsr, todia, todok, transpose
Méthodes héritées de la classe _minmax_mixin
argmax, argmin, max, min, nanmax, nanmin
Méthodes héritées de la classe _data_matrix
__abs__, __round__, astype, conjugate, copy, power
Méthodes héritées de la classe _spbase
__bool__, __div__, __idiv__, __iter__, __len__, __nonzero__, __rdiv__, __repr__, __rmatmul__, __str__, asformat, conj, maximum, mean, minimum, multiply, nonzero, setdiag, sum, tobsr, todense, tolil, trace
Méthodes héritées de la classe spmatrix
asfptype, get_shape, getcol, getformat, getH, getmaxprint, getnnz, getrow, set_shape
Méthodes héritées de la classe object
__delattr__,
__dir__,
__format__,
__getattribute__,
__getstate__,
__hash__,
__reduce__,
__reduce_ex__,
__setattr__,
__sizeof__
Vous êtes un professionnel et vous avez besoin d'une formation ?
Programmation Python
Les fondamentaux
Voir le programme détaillé
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 :