scipy.sparse.rand¶
- scipy.sparse.rand(m, n, density=0.01, format='coo', dtype=None, random_state=None)[源代码]¶
生成具有均匀分布值的给定形状和密度的稀疏矩阵。
- 参数
- m, n集成
矩阵的形状
- density真实,可选
生成矩阵的密度:密度等于1表示全矩阵,密度为0表示没有非零项的矩阵。
- format字符串,可选
稀疏矩阵格式。
- dtype数据类型,可选
返回的矩阵值的类型。
- random_state :{无,整型,
numpy.random.Generator
,{无,整型, 如果 seed 为无(或 np.random )、
numpy.random.RandomState
使用的是Singleton。如果 seed 是一个整型、一个新的RandomState
实例,其种子设定为 seed 。如果 seed 已经是一个Generator
或RandomState
实例,则使用该实例。
- 退货
- res稀疏矩阵
参见
scipy.sparse.random
允许用户指定随机数据源的类似功能。
注意事项
目前仅支持浮点类型。
示例
>>> from scipy.sparse import rand >>> matrix = rand(3, 4, density=0.25, format="csr", random_state=42) >>> matrix <3x4 sparse matrix of type '<class 'numpy.float64'>' with 3 stored elements in Compressed Sparse Row format> >>> matrix.toarray() array([[0.05641158, 0. , 0. , 0.65088847], [0. , 0. , 0. , 0.14286682], [0. , 0. , 0. , 0. ]])