对列表进行重复采样 [英] Take repeated sample of a list

查看:60
本文介绍了对列表进行重复采样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表 range(n) 并且想在不替换的情况下抽取 r 的随机样本,即 np.random.choice(n,r, 替换 = False).但是我想经常这样做,有没有以下命令的快速方法:

I have a list range(n) and want to take a random sample of r without replacing, i.e. np.random.choice(n,r, replace = False). But I want to do this often, is there a fast way for the following command:

a = [np.random.choice(n,r,replace = False) for i in range(100)]

注意:nr 可以是大",s.t.a = np.random.choice(n,(r,100), replace = True) 删除具有两个相似索引的那些将是低效的.

Note: n and r can be "large", s.t. a = np.random.choice(n,(r,100), replace = True) and removing the ones that have two similar indexes will be inefficient.

推荐答案

我经常用来替换 np.random.choice(..., replace=False) 迭代的一个技巧是生成2D 随机数数组,然后使用 argsort/argpartition 得到唯一的整数.

One trick I have often used to replace iterations of np.random.choice(..., replace=False) is generating 2D array of random numbers and then using argsort/argpartition to get unique integers numbers.

因此,使用 argsort,它将是 -

Thus, with argsort, it would be -

np.random.rand(100,n).argsort(axis=1)[:,:r]

或者,由于 r 是一个比 n 相对较小的数字,我们将使用 argpartition 来提升性能,就像这样 -

Alternatively, with r being a relatively smaller number than n, we would use argpartition for performance boost, like so -

np.argpartition(np.random.rand(100,n),r, axis=1)[:,:r]

这篇关于对列表进行重复采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆