Numpy Random Choice not working for 2-dimentional list

You will need to use the indices:

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)]

Or, even shorter (based on hpaulj's comment):

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

Numpy doesn't know if you want to extract a random row or a random cell from the matrix. That's why it only works with 1-D data.

You could use random.choice instead:

>>> 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]]

With Python 3.6 or newer, you can use random.choices directly:

>>> 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]]

If you really want to use a numpy array, you'll have to convert your list of lists to a 1-D array of objects.


Or can do map:

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

Demo:

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))))

Output:

[[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]]

Tags:

Python

List

Numpy