numpy random 生成随机矩阵

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jacke121/article/details/88676847
import numpy as np
np.random.rand(a, b):

>>> np.random.rand(4,3)
array([[ 0.06679473,  0.71073515,  0.5694172 ],
       [ 0.95018143,  0.60161401,  0.8076899 ],
       [ 0.40341822,  0.72154255,  0.92283012],
       [ 0.81143322,  0.87853742,  0.38013707]])
np.random.randint(a, b, size=(c, d)):
>>> np.random.randint(0,10,(4,3))
array([[1, 9, 5],
       [6, 1, 1],
       [8, 2, 0],
       [3, 4, 3]])
 
二项分布函数
np.random.binomial(n,p,size=N),函数的返回值表示n中成功的次数,且以Cn^x*p^x*(1-p)^(n-x)的概率选择成功x次

>>> np.random.binomial(5, 0.5, size=(2,3)) # 一次试验抛5次硬币朝上的硬币数,做2*3次试验
array([[3, 2, 5],
       [2, 2, 3]])

这个需要1ms

for i in range(10):
    start=time.time()
    np.random.randint(0, 10, (100, 200,3))
    print('time',time.time()-start)

如果生成一张大图,需要70ms

for i in range(10):
    start=time.time()
    np.random.randint(0, 10, (1000, 2000,3))
    print('time',time.time()-start)

猜你喜欢

转载自blog.csdn.net/jacke121/article/details/88676847
今日推荐