[英]Fastest way to fill numpy array with zeros and ones
I want to fill randomly a numpy array with arbitrary size with zeroes and ones. 我想随机填充一个任意大小的numpy数组,其中包含0和1。 On each entry there should be a one with probability p.
在每个条目上应该有一个概率为p的一个。
for x in range(l):
rn = numpy.random.uniform(0, 1)
if rn <= p:
arr[x] = 1.0
else:
arr[x] = 0.0
Do you know a faster way to do this? 你知道更快的方法吗?
您想要使用二项分布 。
arr = numpy.random.binomial(1, p, count)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.