python随机抽样numpy_numpy 随机采样 ranf()用法及代码示例

numpy.random.ranf()是用于在numpy中进行随机采样的功能之一。它返回指定形状的数组,并在half-open间隔中填充随机浮点数[0.0, 1.0).

用法: numpy.random.ranf(size=None)

参数:

size :[int或int元组,可选]输出形状。如果给定的形状是例如(m,n,k),则绘制m * n * k个样本。默认值为无,在这种情况下,将返回单个值。

Return :间隔中的随机浮动数组[0.0, 1.0).如果未提供尺寸,则为单个此类随机浮动。

代码1:

# Python program explaining

# numpy.random.ranf() function

# importing numpy

import numpy as geek

# output random float value

out_val = geek.random.ranf()

print ("Output random float value:", out_val)

输出:

Output random float value: 0.0877051588430926

代码2:

# Python program explaining

# numpy.random.ranf() function

# importing numpy

import numpy as geek

# output array

out_arr = geek.random.ranf(size =(2, 1))

print ("Output 2D Array filled with random floats:", out_arr)

输出:

Output 2D Array filled with random floats: [[ 0.14186407]

[ 0.58068259]]

代码3:

# Python program explaining

# numpy.random.ranf() function

# importing numpy

import numpy as geek

# output array

out_arr = geek.random.ranf((3, 3, 2))

print ("Output 3D Array filled with random floats:", out_arr)

输出:

Output 3D Array filled with random floats: [[[ 0.11013584 0.67844746]

[ 0.84691569 0.09467084]

[ 0.69918864 0.12137178]]

[[ 0.30629051 0.28301093]

[ 0.1302665 0.2196221 ]

[ 0.51555358 0.73191852]]

[[ 0.72806359 0.66485275]

[ 0.80654791 0.04947181]

[ 0.06380535 0.99306064]]]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值