bsparse.sparse.random#

sparse.random(shape, density=0.1, dtype=None, format='coo')#

Create a sparse random matrix of specified shape and density.

Parameters:
shapetuple[int, int]

The shape of the matrix.

densityfloat, optional

The density of the matrix, by default 0.1.

dtypenp.dtype, optional

The data type of the matrix elements, by default None.

formatstr, optional

The sparse format of the matrix, by default “coo”.

Returns:
Sparse

A sparse random matrix of specified shape and density.

Examples

>>> random((3, 3)) 
COO(shape=(3, 3), nnz=1, dtype=float64)
>>> random((3, 3), format="csr").toarray() 
array([[0.        , 0.        , 0.        ],
       [0.        , 0.        , 0.        ],
       [0.        , 0.        , 0.04794364]])