To select random values from a 2d array, you can use this
pool = np.random.randint(0, 30, size=[4,5])
seln = np.random.choice(pool.reshape(-1), 3, replace=False)
print(pool)
print(seln)
>[[29 7 19 26 22]
[26 12 14 11 14]
[ 6 1 13 11 1]
[ 7 3 27 1 12]]
[11 14 26]
pool needs to be reshaped into a 1-d vector because np.random.choice
can not handle 2d objects. So in order to create a 2d array composed of randomly selected values from the original 2d array, I had to do one row at a time using a loop.
pool = np.random.randint(0, 30, size=[4,5])
seln = np.empty([4,3], int)
for i in range(0, pool.shape[0]):
seln[i] =np.random.choice(pool[i], 3, replace=False)
print('pool = ', pool)
print('seln = ', seln)
>pool = [[ 1 11 29 4 13]
[29 1 2 3 24]
[ 0 25 17 2 14]
[20 22 18 9 29]]
seln = [[ 8 12 0]
[ 4 19 13]
[ 8 15 24]
[12 12 19]]
However, I am looking for a parallel method; handling all the rows at the same time, instead of one at a time in a loop.
Is this possible? If not numpy, how about Tensorflow?