Skip to content
Advertisement

How to create 2d array with numpy random.choice for every rows?

I’m trying to create a 2d array (which is a six column and lots of rows) with numpy random choice with unique values between 1 and 50 for every row not all of the array

JavaScript

But this raises an error.

JavaScript

Is it possible to make this with an one liner without a loop

Edit

Okey i get the answer.

These are the results with jupyter %time cellmagic

JavaScript

I changed dtypes of np.empty and np.random.randint on @Paul Panzer’s solution because it was not working on my pc.

JavaScript

Fastest one is

JavaScript

Thank you all.

Advertisement

Answer

Here is a constructive approach, draw first (50 choices), second (49 choices) etc. For large sets it’s quite competitive (pp in table):

JavaScript

Code including benchmarking. Algo is a bit complicated because mapping to free spots is hairy:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement