我有一个问题,我需要创建m 个大小为n的样本而不进行替换。此外,该样本必须保留总体向量的原始顺序。所有这一切都超级快。
Population = [50, 30, 12, 24, 420, 243, 173, 194, 123, 43, 21, 64, 34...]
300 samples of a combination of 3
[[24, 21, 34], [50, 194, 21], [12, 173, 64], [30, 173, 194].... [12, 243, 34]]
Run Code Online (Sandbox Code Playgroud)
这些样本必须是独立的,在我的例子中,我需要保留原始总体数组的顺序。有多个可能的答案,但它们都不是很快,它们都是我的代码的瓶颈。我使用 NumPy 来生成随机数。
一些最有前途的方法如下:
gen = np.random.default_rng()
def random_combination(population, sample, number = 3):
with_replacement_samples = gen.choice(len(population), size=(sample, number))
pairs = np.sort(with_replacement_samples)
positions= positions[pairs]
for i in positions:
if i[0] == i[2] or i[0] == i[1] …Run Code Online (Sandbox Code Playgroud)