Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy Random Choice not working for 2-dimentional list

Tags:

python

list

numpy

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?

like image 982
Daniel James Avatar asked Sep 28 '18 10:09

Daniel James


People also ask

How do you randomly select from a list in NumPy?

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.

How does NumPy random choice work?

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.

What is the difference between random and Rand in NumPy?

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.

Does NP random choice return an array?

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.


2 Answers

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)]
like image 172
roganjosh Avatar answered Sep 28 '22 06:09

roganjosh


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.

like image 37
Eric Duminil Avatar answered Sep 28 '22 06:09

Eric Duminil