I'm trying to permute a list composed of sublists with mixed-type elements:
import numpy as np
a0 = ['122', 877.503017, 955.471176, [21.701201, 1.315585]]
a1 = ['176', 1134.076908, 1125.504758, [19.436181, 0.9987899]]
a2 = ['177', 1038.686843, 1018.987868, [19.539959, 1.183997]]
a3 = ['178', 878.999081, 1022.050447, [19.6448771, 1.1867719]]
a = [a0, a1, a2, a3]
b = np.random.permutation(a)
This will fail with:
ValueError: cannot set an array element with a sequence
Is there a built in function that will allow me to generate such permutation?
I need to generate a single random permutation, I'm not trying to obtain all the possible permutations.
I checked the three answers given with:
import time
import random
# np.random.permutation()
start = time.time()
for _ in np.arange(100000):
b = np.random.permutation([np.array(i, dtype='object') for i in a])
print(time.time() - start)
# np.random.shuffle()
start = time.time()
for _ in np.arange(100000):
b = a[:]
np.random.shuffle(b)
print(time.time() - start)
# random.shuffle()
start = time.time()
for _ in np.arange(100000):
random.shuffle(a)
print(time.time() - start)
The results are:
1.47580695152
0.11471414566
0.26300907135
so the np.random.shuffle()
solution is about 10x faster than np.random.permutation()
and 2x faster than random.shuffle()
.