bundles / scipy 1.18.0 / scipy / sparse / _construct / rand
function
scipy.sparse._construct:rand
source: /scipy/sparse/_construct.py:1784
Signature
def rand(m, n, density = 0.01, format = coo, dtype = None, rng = None, *, random_state = None)Summary
Generate a sparse matrix of the given shape and density with uniformly distributed values.
Extended Summary
Parameters
m, n: intshape of the matrix
density: real, optionaldensity of the generated matrix: density equal to one means a full matrix, density of 0 means a matrix with no non-zero items.
format: str, optionalsparse matrix format.
dtype: dtype, optionaltype of the returned matrix values.
rng: {None, int, `numpy.random.Generator`}, optionalIf
rngis passed by keyword, types other than numpy.random.Generator are passed to numpy.random.default_rng to instantiate aGenerator. Ifrngis already aGeneratorinstance, then the provided instance is used. Specifyrngfor repeatable function behavior.If this argument is passed by position or
random_stateis passed by keyword, legacy behavior for the argumentrandom_stateapplies:If
random_stateis None (or numpy.random), the numpy.random.RandomState singleton is used.If
random_stateis an int, a newRandomStateinstance is used, seeded withrandom_state.If
random_stateis already aGeneratororRandomStateinstance then that instance is used.
Returns
res: sparse matrixRandom sparse matrix.
Notes
Only float types are supported for now.
Examples
from scipy.sparse import rand matrix = rand(3, 4, density=0.25, format="csr", rng=42)✓
matrix matrix.toarray()✗
array([[0.094, 0. , 0. , 0. ],
[0. , 0. , 0.697, 0. ],
[0. , 0. , 0.976, 0. ]])
See also
- random
Similar function allowing a custom random data sampler
- random_array
Similar to random() but returns a sparse array
Aliases
scipy.sparse.rand