【问题标题】:Generate random array of floats between a range生成范围之间的随机浮点数组
【发布时间】:2014-03-31 02:05:42
【问题描述】:

我还没有找到一个函数来生成一个在特定范围内具有给定长度的随机浮点数组。

我查看了Random sampling,但似乎没有任何功能可以满足我的需求。

random.uniform 很接近,但它只返回一个元素,而不是一个特定的数字。

这就是我所追求的:

ran_floats = some_function(low=0.5, high=13.3, size=50)

这将返回一个由 50 个随机非唯一浮点数组成的数组(即:允许重复),均匀分布在 [0.5, 13.3] 范围内。

有这样的功能吗?

【问题讨论】:

  • 您已标记问题numpy,但您没有提及numpy.random.uniform,即使它具有您想要的呼叫签名。你有可用的numpy 库吗?
  • [random.uniform(low, high) for i in xrange(size)]
  • @DSM 是的,我有,你显然是 100% 正确的。我错过了那个功能,它似乎完全符合我的需要。您介意发表您的评论作为答案吗?

标签: python arrays random numpy


【解决方案1】:

np.random.uniform 适合您的用例:

sampl = np.random.uniform(low=0.5, high=13.3, size=(50,))

2019 年 10 月更新:

虽然仍支持该语法,但似乎 NumPy 1.17 更改了 API,以支持对随机数生成器的更大控制。今后 API 已更改,您应该查看 https://docs.scipy.org/doc/numpy/reference/random/generated/numpy.random.Generator.uniform.html

增强建议在这里:https://numpy.org/neps/nep-0019-rng-policy.html

【讨论】:

  • OP 的直观搜索问题是some_function(low=0.5, high=13.3, size=50)。这就是python库的设计方式#wow
  • 大小不完全清楚,链接不起作用。这是一个小的澄清。 size: 整数或整数元组,可选。输出形状。如果给定的形状是,例如,(m, n, k),则绘制 m * n * k 个样本。如果 size 是 None 默认值),如果 low 和 high 都是标量,则返回单个值。
  • @vlad - 感谢您指出链接的问题。我已经更新了答案,希望能涵盖当前的使用情况。
  • 在Numpy的官方文档中,下面的函数解决了这个问题。 numpy.org/doc/stable/reference/random/generated/…
【解决方案2】:

为什么不使用列表推导式?

在 Python 2 中

ran_floats = [random.uniform(low,high) for _ in xrange(size)]

在 Python 3 中,range 的工作方式类似于 xrange(ref)

ran_floats = [random.uniform(low,high) for _ in range(size)]

【讨论】:

    【解决方案3】:

    可能已经有一个功能可以做你正在寻找的东西,但我不知道它(还没有?)。 同时,我建议使用:

    ran_floats = numpy.random.rand(50) * (13.3-0.5) + 0.5
    

    这将产生一个形状为 (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
    

    编辑:嗯,是的,所以我错过了,有 numpy.random.uniform() 与您想要的完全相同的调用! 请尝试import numpy; help(numpy.random.uniform) 了解更多信息。

    【讨论】:

      【解决方案4】:

      您也可以使用SciPy

      from scipy import stats
      stats.uniform(0.5, 13.3).rvs(50)
      

      为了记录采样整数它是

      stats.randint(10, 20).rvs(50)
      

      【讨论】:

        【解决方案5】:

        为什么不将random.uniform 与列表理解结合起来?

        >>> def random_floats(low, high, size):
        ...    return [random.uniform(low, high) for _ in xrange(size)]
        ... 
        >>> random_floats(0.5, 2.8, 5)
        [2.366910411506704, 1.878800401620107, 1.0145196974227986, 2.332600336488709, 1.945869474662082]
        

        【讨论】:

          【解决方案6】:

          列表推导中的 for 循环需要时间并使其变慢。 最好使用 numpy 参数(低、高、大小、..等)

          import numpy as np
          import time
          rang = 10000
          tic = time.time()
          for i in range(rang):
              sampl = np.random.uniform(low=0, high=2, size=(182))
          print("it took: ", time.time() - tic)
          
          tic = time.time()
          for i in range(rang):
              ran_floats = [np.random.uniform(0,2) for _ in range(182)]
          print("it took: ", time.time() - tic)
          

          样本输出:

          ('花了:', 0.06406784057617188)

          ('花了:', 1.7253198623657227)

          【讨论】:

            【解决方案7】:

            这是最简单的方法

            np.random.uniform(start,stop,(rows,columns))
            

            【讨论】:

              【解决方案8】:

              或者,如果您可以使用实数列表代替,您可以使用标准的random.randrange

              def some_function(low, high, size):
                  low_int = int(low * 1000)
                  high_int = int(high *1000)
                  return [random.randrange(low_int, high_int, size)/1000 for _ in range(size)]
              

              【讨论】:

                【解决方案9】:

                np.random.random_sample(size) 会在半开区间 [0.0, 1.0) 内生成随机浮点数。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2013-12-20
                  • 2011-07-14
                  • 2011-08-30
                  • 1970-01-01
                  • 2016-09-25
                  • 2015-10-10
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多