numpy.random抽样

numpy.random中的函数

rand()

生成随机值

>>>np.random.rand(3)
array([0.68256392, 0.14294158, 0.72120024])

randn()

生成标准正态分布样本

>>>np.random.randn(3)
array([-2.49001983, -0.33634447, -0.57466392])

randint(low,high,size)

生成左闭右开的随机整数
randint(high[,size]) 取小于high的整数[个数为size]

>>>np.random.randint(2,10,3)
array([9, 7, 4])

random_integers(low,high,size)

左闭右闭的随机整数,注意,python3.8.1已经不再使用这个函数

>>>np.random.random_integers(4,size=10)

Warning (from warnings module):
  File "<pyshell#11>", line 1
DeprecationWarning: This function is deprecated. Please call randint(1, 4 + 1) instead
array([3, 1, 3, 4, 2, 1, 4, 4, 1, 4])

random_sample([size])、random([size]) 、ranf([size])、sample([size])

生成[0.0,1.0)间的随机浮点数

>>>np.random.random_sample(3)
array([0.07828906, 0.92357492, 0.23780065])

>>>np.random.random(3)
array([0.92554691, 0.98998066, 0.1320273 ])

>>>np.random.ranf(3)
array([0.57944383, 0.89816435, 0.8438508 ])

>>>np.random.sample(3)
array([0.01619052, 0.04228103, 0.89007802])

choice(a[, size, replace, p])

从一个给定的数组a中生成随机样本,其中p为a中元素被抽取的概率分布

>>>np.random.choice([2,3,1,4,8,5],10,p=[0.3,0.5,0,0,0.1,0.1])
array([3, 3, 3, 3, 2, 3, 8, 3, 5, 3])

uniform(low,high,size)

生成 [low.high) 间的均匀分布,注意是左闭右开

>>>np.random.uniform(-1,1,3)
array([-0.52535198, -0.81110613, -0.69339663])
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值