Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting {ValueError} 'a' must be 1-dimensoinal for list of lists from np.random.choice

Tags:

python

numpy

I want to create a toy training set from the XOR function:

xor = [[0, 0, 0],
       [0, 1, 1],
       [1, 0, 1],
       [1, 1, 0]]

input_x = np.random.choice(a=xor, size=200)

However, this is giving me

{ValueError} 'a' must be 1-dimensoinal

But, if I add e.g. a number to this list:

xor = [[0, 0, 0],
       [0, 1, 1],
       [1, 0, 1],
       [1, 1, 0],
       1337]       # With this it will work

input_x = np.random.choice(a=xor, size=200)

it starts to work. Why is this the case and how can I make this work without having to add another primitive to the xor list?

like image 972
Stefan Falk Avatar asked Jan 17 '17 13:01

Stefan Falk


People also ask

What is np random choice in Python?

NumPy random. choice() function in Python is used to return a random sample from a given 1-D array. It creates an array and fills it with random samples. It has four parameters and using these parameters we can manipulate the random samples of an array.

Does NumPy have tools for reading or writing array based datasets to disk?

Tools for reading/writing array data to disk and working with memory-mapped files. Linear algebra, random number generation, and Fourier transform capabilities. A C API for connecting NumPy with libraries written in C, C++, or FORTRAN.

How do you use choices in Python?

The choices() method returns a list with the randomly selected element from the specified sequence. You can weigh the possibility of each result with the weights parameter or the cum_weights parameter. The sequence can be a string, a range, a list, a tuple or any other kind of sequence.


1 Answers

In case of an array I would do the following:

xor = np.array([[0,0,0],
       [0,1,1],
       [1,0,1],
       [1,1,0]])
rnd_indices = np.random.choice(len(xor), size=200)

xor_data = xor[rnd_indices]
like image 195
BloodyD Avatar answered Sep 30 '22 00:09

BloodyD