python 矩阵随机生成

本文介绍如何利用scipy.sparse.rand函数创建指定形状和密度的稀疏矩阵,包含参数解释及示例代码。通过调整参数,可以生成不同密度的随机值矩阵。

Generate a sparse matrix


Generate a sparse matrix of the given shape and density with uniformly distributed values.

scipy.sparse.rand(mndensity=0.01format='coo'dtype=Nonerandom_state=None)


Parameters:
m, n : int
shape of the matrix
density : real, optional
density 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, optional
sparse matrix format.
dtype : dtype, optional
type of the returned matrix values.
random_state : {numpy.random.RandomState, int}, optional
Random number generator or random seed. If not given, the singleton numpy.random will be used.

example:

>>> sp.rand(4,3,density=1,).todense()
matrix([[ 0.54318062,  0.73848674,  0.66199091],
     [ 0.55778929,  0.74904161,  0.97990645],
     [ 0.92667378,  0.07833997,  0.18023915],
     [ 0.02989316,  0.88534361,  0.00998536]])

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值