Reputation: 311
Say I have a list of values, e.g.: [9, 17, 2]
What is the best way to create an n-dimensional numpy array (e.g.: [110 x 90 x 11] say) populated by the values in the list randomly, but evenly sampled?
Upvotes: 0
Views: 204
Reputation: 29317
Use
np.random.choice(arr, (9, 17, 2))
random.choice
will by default pick a random sample from arr
with uniform probability and with replacement (and with the given shape).
Upvotes: 1
Reputation: 1299
)you can select numbers randomly with random.randrange
import random
import numpy as np
l=[9, 17, 2]
arrayshape=[110,90,11]
#random.randrange(len(l)) generates random indexes
#l[random.randrange(len(l))] select random indexes from list
#for i in range(110*90*11) how many number we need
#np.array(...) make an array from list
#np.reshape(... ,arrayshape) reshaping array to our shape
array=np.reshape(np.array([l[random.randrange(len(l))] for i in range(110*90*11)]),arrayshape)
Upvotes: 0