[英]random sample in python
我想加快速度:
import random
ndim = 50000
for i in xrange(ndim):
random.sample([j for j in xrange(ndim) if j != i], 30000)
我正在考虑使用NumPy,但不知道如何使用NumPy。
numpy.random.sample(大小=无)
返回随机浮动在半开间隔[0.0,1.0)中。
>>> np.random.random_sample()
0.47108547995356098
>>> np.random.random_sample((5,))
array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428])
因此,如果你的ndim = 50000
,那么你可以从上面得到一个随机样本并将其与ndim
相乘并将其ndim
为int。 这可行...
使用NumPy的可能解决方案是:
import numpy as np
from numpy.random import randint
ndim = 50000
mndim = ndim -1
base = np.arange(1, ndim)
# addr = np.ones(mndim, dtype=int)
# addr[0] = 0
for i in xrange(5000):
base[randint(0, mndim, 30000)]
try:
base[i] = i
except IndexError:
pass
使用在使用后为下一轮更新的允许值x
列表:
import random
s = 3000
ndim = 5000
x = range(1, ndim)
for i in xrange(ndim):
random.sample(x, s)
if(i < ndim - 1): x[i] = i #update
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.