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?
Use the numpy. random. choice() function to generate the random choices and samples from a NumPy multidimensional array. Using this function we can get single or multiple random numbers from the n-dimensional array with or without replacement.
Working of the NumPy random choice() function When we pass the list of elements to the NumPy random choice() function it randomly selects the single element and returns as a one-dimensional array, but if we specify some size to the size parameter, then it returns the one-dimensional array of that specified size.
The only difference is in how the arguments are handled. With numpy. random. rand , the length of each dimension of the output array is a separate argument.
The numpy random choice() method takes four arguments and returns the array filled with random sample numbers. To generate a random sample from a given 1D array, use the random. choice(a, size=None, replace=True, p=None) method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With