Randomly shuffle data and labels from different files in the same order Randomly shuffle data and labels from different files in the same order numpy numpy

Randomly shuffle data and labels from different files in the same order


Generate a random order of elements with np.random.permutation and simply index into the arrays data and classes with those -

idx = np.random.permutation(len(data))x,y = data[idx], classes[idx]


Alternatively you can concatenate the data and labels together, shuffle them and then separate them into input x and label y as shown below:

def read_data(filename, delimiter, datatype): # Read data from a file    return = np.genfromtxt(filename, delimiter, dtype= datatype)classes = read_data('labels.csv', dtype= np.str , delimiter='\t')data = read_data('data.csv', delimiter=',')dataset = np.r_['1', data, classes] # Concatenate along second axisdef dataset_shuffle(dataset): # Returns separated shuffled data and classes from dataset     np.random.shuffle(dataset)    n, m = dataset.shape    x = data[:, 0:m-1]    y = data[:, m-1]    return x, y # Return shuffled x and y with preserved order