随机选择列表与替换

时间:2015-04-10 17:42:03

标签: python numpy random

我有一个列表列表,如下所示:

a = [[1,2],[2,3]]

我想创建一个来自a的给定大小的随机列表替换numpy.random.choice()方法只接受1D数组。我可以编写自己的函数来执行此操作,但是已经有一种优化的方法吗?

预期输出

[[1,2],[1,2],[2,3],[2,3]] 
// the size (4 here) has to be a parameter passed to the function

4 个答案:

答案 0 :(得分:11)

您可以反复拨打the standard library's random.choice()。无需numpy

>>> list_of_lists = [[1, 2], [2, 3]]
>>> sample_size = 4
>>> [random.choice(list_of_lists) for _ in range(sample_size)]
[[1, 2], [2, 3], [1, 2], [1, 2]]

这是random.sample()的替代方案,无需替换即可使用,并允许您选择大于原始大小的“样本”。

答案 1 :(得分:1)

使用numpy:

size = 4
a = np.array([[1,2],[2,3]])
b = np.random.randint(len(a), size = size)
a[b,:]

Out[93]:
array([[2, 3],
       [2, 3],
       [2, 3],
       [1, 2]])

答案 2 :(得分:1)

从Python 3.6开始,您可以直接使用random.choices

random.choices(list_of_lists, k=sample_size)
## [[1, 2], [3, 4], [3, 4], [1, 2]]

一个粗略的基准测试表明,与列表理解方法相比,这似乎在不同的样本量上更具性能。

>>> list_of_lists = [[1, 2], [3, 4]]
>>> sample_size = 4

>>> %timeit [random.choice(list_of_lists) for _ in range(sample_size)]
4.49 µs ± 20.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

>>> %timeit random.choices(list_of_lists, k=sample_size)
1.99 µs ± 14.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

>>> list_of_lists *= 100
>>> sample_size *= 1000

>>> %timeit [random.choice(list_of_lists) for _ in range(sample_size)]
3.54 ms ± 28.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

>>> %timeit random.choices(list_of_lists, k=sample_size)
927 µs ± 1.39 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

答案 3 :(得分:0)

more_itertools库实现more_itertools.random_combination_with_replacement

import more_itertools as mit

list_of_lists = [[1, 2], [2, 3]]
sample_size = 4
list(mit.random_combination_with_replacement(list_of_lists, sample_size))
# [[1, 2], [1, 2], [2, 3], [2, 3]]