I want to be able to generate a list (of arbitrary length) of arrays, each array being a random permutation of one array A.
There's this python option :
for x in range(n):
random.shuffle(A)
routes.append(A)
But I suspect there's a less procedural way to achieve this with numpy. Any suggestion?
Consider this example:
In [59]: crr
Out[59]:
array([[0.83704624, 0.42109715, 0.50779425, 0.93753455, 0.11773652,
0.08751938],
[0.83704624, 0.42109715, 0.50779425, 0.93753455, 0.11773652,
0.08751938],
[0.83704624, 0.42109715, 0.50779425, 0.93753455, 0.11773652,
0.08751938],
[0.83704624, 0.42109715, 0.50779425, 0.93753455, 0.11773652,
0.08751938],
[0.83704624, 0.42109715, 0.50779425, 0.93753455, 0.11773652,
0.08751938]])
In [60]: np.random.shuffle(crr[0])
In [61]: crr
Out[61]:
array([[0.42109715, 0.50779425, 0.93753455, 0.11773652, 0.08751938,
0.83704624],
[0.83704624, 0.42109715, 0.50779425, 0.93753455, 0.11773652,
0.08751938],
[0.83704624, 0.42109715, 0.50779425, 0.93753455, 0.11773652,
0.08751938],
[0.83704624, 0.42109715, 0.50779425, 0.93753455, 0.11773652,
0.08751938],
[0.83704624, 0.42109715, 0.50779425, 0.93753455, 0.11773652,
0.08751938]])
As random shuffle is in-place, here we can specify only the first row to be shuffled. Therefore if you use np.repeat
or np.tile
to repeat your A for N times and reshape the resultant into a 2D array like crr
here, you can do this to reach final goal:
In [69]: for v in crr:
...: np.random.shuffle(v)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.