mxnet.ndarray.sparse.csr_matrix¶
-
mxnet.ndarray.sparse.
csr_matrix
(arg1, shape=None, ctx=None, dtype=None)[source]¶ Creates a CSRNDArray, an 2D array with compressed sparse row (CSR) format.
The CSRNDArray can be instantiated in several ways:
- csr_matrix(D):
- to construct a CSRNDArray with a dense 2D array
D
D (array_like) - An object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence.
ctx (Context, optional) - Device context (default is the current default context).
dtype (str or numpy.dtype, optional) - The data type of the output array. The default dtype is
D.dtype
ifD
is an NDArray or numpy.ndarray, float32 otherwise.
- to construct a CSRNDArray with a dense 2D array
- csr_matrix(S)
- to construct a CSRNDArray with a sparse 2D array
S
S (CSRNDArray or scipy.sparse.csr.csr_matrix) - A sparse matrix.
ctx (Context, optional) - Device context (default is the current default context).
dtype (str or numpy.dtype, optional) - The data type of the output array. The default dtype is
S.dtype
.
- to construct a CSRNDArray with a sparse 2D array
- csr_matrix((M, N))
- to construct an empty CSRNDArray with shape
(M, N)
M (int) - Number of rows in the matrix
N (int) - Number of columns in the matrix
ctx (Context, optional) - Device context (default is the current default context).
dtype (str or numpy.dtype, optional) - The data type of the output array. The default dtype is float32.
- to construct an empty CSRNDArray with shape
- csr_matrix((data, indices, indptr))
- to construct a CSRNDArray based on the definition of compressed sparse row format using three separate arrays, where the column indices for row i are stored in
indices[indptr[i]:indptr[i+1]]
and their corresponding values are stored indata[indptr[i]:indptr[i+1]]
. The column indices for a given row are expected to be sorted in ascending order. Duplicate column entries for the same row are not allowed. data (array_like) - An object exposing the array interface, which holds all the non-zero entries of the matrix in row-major order.
indices (array_like) - An object exposing the array interface, which stores the column index for each non-zero element in
data
.indptr (array_like) - An object exposing the array interface, which stores the offset into
data
of the first non-zero element number of each row of the matrix.shape (tuple of int, optional) - The shape of the array. The default shape is inferred from the indices and indptr arrays.
ctx (Context, optional) - Device context (default is the current default context).
dtype (str or numpy.dtype, optional) - The data type of the output array. The default dtype is
data.dtype
ifdata
is an NDArray or numpy.ndarray, float32 otherwise.
- to construct a CSRNDArray based on the definition of compressed sparse row format using three separate arrays, where the column indices for row i are stored in
- csr_matrix((data, (row, col)))
- to construct a CSRNDArray based on the COOrdinate format using three seperate arrays, where
row[i]
is the row index of the element,col[i]
is the column index of the element anddata[i]
is the data corresponding to the element. All the missing elements in the input are taken to be zeroes. data (array_like) - An object exposing the array interface, which holds all the non-zero entries of the matrix in COO format.
row (array_like) - An object exposing the array interface, which stores the row index for each non zero element in
data
.col (array_like) - An object exposing the array interface, which stores the col index for each non zero element in
data
.shape (tuple of int, optional) - The shape of the array. The default shape is inferred from the
row
andcol
arrays.ctx (Context, optional) - Device context (default is the current default context).
dtype (str or numpy.dtype, optional) - The data type of the output array. The default dtype is float32.
- to construct a CSRNDArray based on the COOrdinate format using three seperate arrays, where
- Parameters
arg1 (tuple of int, tuple of array_like, array_like, CSRNDArray, scipy.sparse.csr_matrix, scipy.sparse.coo_matrix, tuple of int or tuple of array_like) – The argument to help instantiate the csr matrix. See above for further details.
shape (tuple of int, optional) – The shape of the csr matrix.
ctx (Context, optional) – Device context (default is the current default context).
dtype (str or numpy.dtype, optional) – The data type of the output array.
- Returns
A CSRNDArray with the csr storage representation.
- Return type
Example
>>> a = mx.nd.sparse.csr_matrix(([1, 2, 3], [1, 0, 2], [0, 1, 2, 2, 3]), shape=(4, 3)) >>> a.asnumpy() array([[ 0., 1., 0.], [ 2., 0., 0.], [ 0., 0., 0.], [ 0., 0., 3.]], dtype=float32)
See also
CSRNDArray()
MXNet NDArray in compressed sparse row format.