[英]generate long list (with repetition) of permutations of an array with numpy
I want to be able to generate a list (of arbitrary length) of arrays, each array being a random permutation of one array A. 我希望能够生成一个列表(任意长度)的数组,每个数组是一个数组A的随机排列。
There's this python option : 有这个python选项 :
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. 但我怀疑用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. 由于随机shuffle就位,这里我们只能指定要洗牌的第一行。 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: 因此,如果您使用
np.repeat
或np.tile
重复A次N次并将结果重新crr
为像此处的crr
这样的2D数组,则可以执行此操作以达到最终目标:
In [69]: for v in crr:
...: np.random.shuffle(v)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.