6

I ran the following python code:

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)

expecting a result like this:

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

but what I got instead was the error message below:

 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

How do you randomly choose from a 2-dimensional list?

NOhs
  • 2,730
  • 3
  • 22
  • 53
Daniel James
  • 1,247
  • 8
  • 24

4 Answers4

6

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)]
roganjosh
  • 11,753
  • 4
  • 29
  • 43
4

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.

Eric Duminil
  • 50,694
  • 8
  • 64
  • 113
2

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]]
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
1

Sampling with replacement

Use random.choices

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

Sampling without replacement

Use random.sample

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