Simultaneous shuffling

Implementation using numpy


In [1]:
import numpy as np
import gc

In [2]:
def simultaneous_shuffle(X, Y):
    merged_data = list(zip(X, Y))
    np.random.shuffle(merged_data)
    X, Y = zip(*merged_data)
    del merged_data
    gc.collect()
    return np.asarray(X), np.asarray(Y)

In [3]:
X = [[1, 5], [6, 7], [4,2]]
Y = [[0, 0], [1, 0], [1,1]]

In [4]:
print(simultaneous_shuffle(X, Y))


(array([[4, 2],
       [1, 5],
       [6, 7]]), array([[1, 1],
       [0, 0],
       [1, 0]]))

Implementation using scikit learn


In [5]:
from sklearn.utils import shuffle

In [6]:
print(shuffle(X, Y, random_state=1024))


[[[1, 5], [4, 2], [6, 7]], [[0, 0], [1, 1], [1, 0]]]

Independent shuffle


In [7]:
print ("X = ", X)
print ("Y = ", Y)
np.random.shuffle(X)
np.random.shuffle(Y)
print ("Shuffled X = ",X)
print ("Shuffled Y = ",Y)


X =  [[1, 5], [6, 7], [4, 2]]
Y =  [[0, 0], [1, 0], [1, 1]]
Shuffled X =  [[6, 7], [1, 5], [4, 2]]
Shuffled Y =  [[0, 0], [1, 1], [1, 0]]

In [ ]: