I have multiple numpy arrays with the same number of rows (axis_0) that I'd like to shuffle in unison. After one shuffle, I'd like to shuffle them again with a different random seed.
Till now, I've used the solution from Better way to shuffle two numpy arrays in unison :
def shuffle_in_unison(a, b):
rng_state = numpy.random.get_state()
numpy.random.shuffle(a)
numpy.random.set_state(rng_state)
numpy.random.shuffle(b)
However, this doesn't work for multiple unison shuffles, since rng_state
is always the same.
I've tried to use
RandomState
in order to get a different seed for each call, but this doesn't even work for a single unison shuffle:
a = np.array([1,2,3,4,5])
b = np.array([10,20,30,40,50])
def shuffle_in_unison(a, b):
r = np.random.RandomState() # different state from /dev/urandom for each call
state = r.get_state()
np.random.shuffle(a) # array([4, 2, 1, 5, 3])
np.random.set_state(state)
np.random.shuffle(b) # array([40, 20, 50, 10, 30])
# -> doesn't work
return a,b
for i in xrange(10):
a,b = shuffle_in_unison(a,b)
print a,b
What am I doing wrong?
Edit:
For everyone that doesn't have huge arrays like me, just use the solution by Francesco (https://stackoverflow.com/a/47156309/3955022):
def shuffle_in_unison(a, b):
n_elem = a.shape[0]
indeces = np.random.permutation(n_elem)
return a[indeces], b[indeces]
The only drawback is that this is not an in-place operation, which is a pity for large arrays like mine (500G).