7

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,780
  • 3
  • 25
  • 59
Daniel James
  • 1,381
  • 1
  • 10
  • 28

5 Answers5

7

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
  • 12,594
  • 4
  • 29
  • 46
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
  • 52,989
  • 9
  • 71
  • 124
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]]
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
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,451
  • 1
  • 25
  • 20
1

alternatively, use random to generate indices; then slice from the array. works on any dimention

n_sample = 10 #sample count
a_list [np.random.randint(0,a_list.shape[0], n_sample )]
Ansh
  • 321
  • 1
  • 8