【问题标题】:Fastest way to fill numpy array with zeros and ones用零和一填充numpy数组的最快方法
【发布时间】:2016-05-12 20:14:32
【问题描述】:

我想用零和一随机填充任意大小的 numpy 数组。在每个条目上应该有一个概率为 p 的条目。

    for x in range(l):
        rn = numpy.random.uniform(0, 1)
        if rn <= p:
            arr[x] = 1.0
        else:
            arr[x] = 0.0

您知道更快的方法吗?

【问题讨论】:

    标签: python numpy random


    【解决方案1】:

    您想使用Binomial distribution

    arr = numpy.random.binomial(1, p, count)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-18
      • 2017-06-11
      • 2017-10-24
      • 2016-11-06
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多