bsparse.sparse.CSR#
- class CSR(rowptr, cols, data, shape=None, dtype=None, symmetry=None)#
A sparse matrix in Compressed Sparse Row format.
The
CSR
class represents a sparse matrix using three arrays:rowptr: contains the index of the first element of each row.
cols: contains the column indices of each non-zero element.
data: contains the values of each non-zero element.
- Parameters:
- rowptrarray_like
The index of the first element of each row.
- colsarray_like
The column indices of each non-zero element.
- dataarray_like
The values of each non-zero element.
- shapetuple, optional
The shape of the matrix. If not given, it is inferred from the row and column indices.
- dtypenumpy.dtype, optional
The data type of the matrix elements. If not given, it is inferred from the data array.
- symmetrystr, optional
The symmetry of the matrix. If not given, no symmetry is assumed. This is only applicable for square matrices, where possible values are
'symmetric'
and'hermitian'
. Note that when setting a symmetry, the lower triangular part of the matrix is discarded.
Examples
>>> rowptr = [0, 2, 3, 5] >>> cols = [0, 2, 1, 0, 2] >>> data = [1, 2, 3, 4, 5] >>> csr = CSR(rowptr, cols, data, shape=(3, 3)) >>> csr.toarray() array([[1, 0, 2], [0, 3, 0], [4, 0, 5]])
- Attributes:
- rowptrndarray
The index of the first element of each row.
- colsndarray
The column coordinates of the non-zero elements.
- datandarray
The values of the diagonals.
shape
tuple[int, int]The shape of the matrix.
dtype
dtypeThe data type of the matrix.
symmetry
strThe symmetry of the matrix.
nnz
intThe number of non-zero elements in the matrix.
T
CSRThe transpose of the matrix.
H
CSRThe conjugate transpose of the matrix.
Methods
astype
(dtype)Returns a copy of the matrix with a different data type.
conj
()The complex conjugate of the matrix.
The complex conjugate of the matrix.
copy
()Returns a copy of the matrix.
diagonal
([offset])Returns the diagonal of the matrix.
from_array
(arr[, symmetry])Creates a sparse matrix from a dense
numpy.ndarray
.from_sparray
(mat[, symmetry])Creates a sparse matrix from a
scipy.sparse.sparray
.save_npz
(filename)Saves the matrix as
.npz
archive.toarray
()Converts the matrix to a dense
numpy.ndarray
.tocoo
()Converts the matrix to
COO
format.tocsr
()Converts the matrix to
CSR
format.todia
()Converts the matrix to
DIA
format.