動作環境
ideone(Python3)
2つのnumpy.array(以下、array)があるとする。
1つのarrayを入替え、もう1つのarrayも同じ順番で入替えたい。
(ニホンゴムズカシイ)
https://stackoverflow.com/questions/4601373/better-way-to-shuffle-two-numpy-arrays-in-unison
の
answered Jan 5 '11 at 8:52
mtrw
のコードが参考になりました。
ideoneでの実装例。
https://ideone.com/pw7L27
import numpy as np
def unison_shuffled_copies(a, b):
assert len(a) == len(b)
p = np.random.permutation(len(a))
return a[p], b[p]
alist = np.array([ 1, 2, 3, 4, 5 ])
blist = np.array([ 3, 1, 4, 1, 5 ])
ares, bres = unison_shuffled_copies(alist, blist)
print(ares)
print(bres)
run
[2 1 5 3 4]
[1 3 5 4 1]