Numpy随机选择不适用于二维列表

时间:2018-09-28 10:01:04

标签: python list numpy

我运行了以下python代码:

import numpy as np
a_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 1, 2]]
np.random.choice(a_list, size=20, 
    replace=True)

期望这样的结果:

[[7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 1, 2], [1, 2, 3], [1, 2, 3], [10, 1, 2], [1, 2, 3], [7, 8, 9], [1, 2, 3], [1, 2, 3], [10, 1, 2], [4, 5, 6], [4, 5, 6], [10, 1, 2], [10, 1, 2], [7, 8, 9], [1, 2, 3], [7, 8, 9]]

但是我得到的是以下错误消息:

 ValueError                           Traceback (most recent call last)
 <ipython-input-80-c11957aca587> in <module>()
    2 a_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 1, 2]]
    3 np.random.choice(a_list, size=20, 
----> 4 replace=True)

mtrand.pyx in mtrand.RandomState.choice()

ValueError: a must be 1-dimensional

您如何从二维列表中随机选择?

4 个答案:

答案 0 :(得分:4)

Numpy不知道您要从矩阵中提取随机行还是随机单元格。这就是为什么它仅适用于一维数据的原因。

您可以改用random.choice

>>> import random
>>> a_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 1, 2]]
>>> [random.choice(a_list) for _ in range(20)]
[[4, 5, 6], [7, 8, 9], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [4, 5, 6], [4, 5, 6], [1, 2, 3], [10, 1, 2], [10, 1, 2], [4, 5, 6], [1, 2, 3], [1, 2, 3], [1, 2, 3], [10, 1, 2], [4, 5, 6], [1, 2, 3], [4, 5, 6], [4, 5, 6]]

在Python 3.6或更高版本中,您可以直接使用random.choices

>>> random.choices(a_list, k=20)
[[10, 1, 2], [7, 8, 9], [4, 5, 6], [10, 1, 2], [1, 2, 3], [1, 2, 3], [10, 1, 2], [10, 1, 2], [1, 2, 3], [7, 8, 9], [10, 1, 2], [10, 1, 2], [7, 8, 9], [4, 5, 6], [7, 8, 9], [4, 5, 6], [1, 2, 3], [4, 5, 6], [7, 8, 9], [7, 8, 9]]

如果您确实要使用numpy数组,则必须将列表列表转换为1-D array of objects.

答案 1 :(得分:3)

您将需要使用索引:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 1, 2]])
indices = np.arange(arr.shape[0])

output = arr[np.random.choice(indices, 20)]

或更短(根据hpaulj的评论):

output = arr[np.random.choice(arr.shape[0],20)]

答案 2 :(得分:2)

或者可以执行map

print(list(map(lambda x: random.choice(a_list),range(20))))

演示:

import random
a_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 1, 2]]
print(list(map(lambda x: random.choice(a_list),range(20))))

输出:

[[7, 8, 9], [10, 1, 2], [4, 5, 6], [10, 1, 2], [4, 5, 6], [10, 1, 2], [7, 8, 9], [4, 5, 6], [7, 8, 9], [1, 2, 3], [7, 8, 9], [1, 2, 3], [1, 2, 3], [10, 1, 2], [10, 1, 2], [10, 1, 2], [4, 5, 6], [10, 1, 2], [1, 2, 3], [7, 8, 9]]

答案 3 :(得分:1)

替换样品

使用random.choices

x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 1, 2]])
samples = random.choices(x, k=20)

无需更换的采样

使用random.sample

x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 1, 2]])
samples = random.sample(x.tolist(), k=2)