random

sparse.random(shape, density=0.01, canonical_order=False, random_state=None, data_rvs=None, format='coo')[source]

Generate a random sparse multidimensional array

Parameters:
  • shape (Tuple[int]) – Shape of the array
  • density (float, optional) – Density of the generated array.
  • canonical_order (bool, optional) – Whether or not to put the output COO object into canonical order. False by default.
  • random_state (Union[numpy.random.RandomState, int], optional) – Random number generator or random seed. If not given, the singleton numpy.random will be used. This random state will be used for sampling the sparsity structure, but not necessarily for sampling the values of the structurally nonzero entries of the matrix.
  • data_rvs (Callable) – Data generation callback. Must accept one single parameter: number of nnz elements, and return one single NumPy array of exactly that length.
  • format ({'coo', 'dok'}) – The format to return the output array in.
Returns:

The generated random matrix.

Return type:

{COO, DOK}

See also

scipy.sparse.rand
Equivalent Scipy function.
numpy.random.rand
Similar Numpy function.

Examples

>>> from sparse import random
>>> from scipy import stats
>>> rvs = lambda x: stats.poisson(25, loc=10).rvs(x, random_state=np.random.RandomState(1))
>>> s = random((2, 3, 4), density=0.25, random_state=np.random.RandomState(1), data_rvs=rvs)
>>> s.todense()  
array([[[ 0,  0,  0,  0],
        [ 0, 34,  0,  0],
        [33, 34,  0, 29]],

       [[30,  0,  0, 34],
        [ 0,  0,  0,  0],
        [ 0,  0,  0,  0]]])