Gab*_*iel 54 python arrays random numpy
这可能是一个愚蠢的问题,但我无法找到一个函数来生成一定范围内给定长度的随机浮点数组.
我看过随机抽样,但没有功能似乎做我需要的.
random.uniform接近但它只返回一个元素,而不是一个特定的数字.
这就是我所追求的:
ran_floats = some_function(low=0.5, high=13.3, size=50)
Run Code Online (Sandbox Code Playgroud)
这将返回[0.5, 13.3]
在该范围内均匀分布的随机非唯一浮点数组(即:允许重复)[0.5, 13.3]
.
有这样的功能吗?
Jos*_*del 90
np.random.uniform
适合您的用例:http:
//docs.scipy.org/doc/numpy/reference/generated/numpy.random.uniform.html
sampl = np.random.uniform(low=0.5, high=13.3, size=(50,))
Run Code Online (Sandbox Code Playgroud)
ise*_*dev 13
为什么不使用列表理解?
ran_floats = [random.uniform(low,high) for _ in xrange(size)]
Run Code Online (Sandbox Code Playgroud)
小智 11
这是最简单的方法
np.random.uniform(start,stop,(rows,columns))
Run Code Online (Sandbox Code Playgroud)
可能已经有一个函数可以完成您正在寻找的功能,但我还不知道(还?)。同时,我建议使用:
ran_floats = numpy.random.rand(50) * (13.3-0.5) + 0.5
Run Code Online (Sandbox Code Playgroud)
这将产生形状 (50,) 的数组,其均匀分布在 0.5 到 13.3 之间。
您还可以定义一个函数:
def random_uniform_range(shape=[1,],low=0,high=1):
"""
Random uniform range
Produces a random uniform distribution of specified shape, with arbitrary max and
min values. Default shape is [1], and default range is [0,1].
"""
return numpy.random.rand(shape) * (high - min) + min
Run Code Online (Sandbox Code Playgroud)
编辑:嗯,是的,所以我错过了,有 numpy.random.uniform() 具有与您想要的完全相同的调用!尝试import numpy; help(numpy.random.uniform)
获取更多信息。
或者你可以使用SciPy
from scipy import stats
stats.uniform(0.5, 13.3).rvs(50)
Run Code Online (Sandbox Code Playgroud)
对于记录采样整数来说,它是
stats.randint(10, 20).rvs(50)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
83670 次 |
最近记录: |