Python numpy np.random.randn及np.random.rand的区别

Python numpy

np.random.randn及np.random.rand的区别

numpy中有一些常用的用来产生随机数的函数,randn()和rand()就属于这其中。
numpy.random.randn(d0, d1, …, dn)是从标准正态分布中返回一个或多个样本值
numpy.random.rand(d0, d1, …, dn)的随机样本位于[0, 1)之间

import numpy as np

print(np.random.rand(2,2))
print('--------------------')
print(np.random.rand(3,2))
print('********************')
print(np.random.randn(2,2))
print('--------------------')
print(np.random.randn(1,3))

output:

[[0.18098186 0.7482656 ]
 [0.2342925  0.37032098]]
--------------------
[[0.77782759 0.82793442]
 [0.91272684 0.49250067]
 [0.12239144 0.50111556]]
********************
[[ 1.31480601  1.07406308]
 [ 0.59012071 -0.767058  ]]
--------------------
[[-0.70718168  0.73662156  0.28464163]]
np.random.rand

Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).
numpy.random.rand(d0, d1, …, dn)的随机样本位于[0, 1)之间

np.random.randn

numpy.random.randn(d0, d1, …, dn)是从标准正态分布中返回一个或多个样本值

Notes

For random samples from N ( μ , σ 2 ) N(\mu, \sigma^2) N(μ,σ2), use:

sigma * np.random.randn(…) + mu

Examples
>>>
>>> np.random.randn()
2.1923875335537315 #random

Two-by-four array of samples from N(3, 6.25):

>>>
>>> 2.5 * np.random.randn(2, 4) + 3
array([[-4.49401501,  4.00950034, -1.81814867,  7.29718677],  #random
       [ 0.39924804,  4.68456316,  4.99394529,  4.84057254]]) #random

参考文档:https://docs.scipy.org/doc/numpy-1.16.0/reference/routines.random.html

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值