Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

l have two numpy arrays the first one contains data and the second one contains labels. l want to shuffle the data with respect to their labels. In other way, how can l shuffle my labels and data in the same order.

import numpy as np
data=np.genfromtxt("dataset.csv", delimiter=',')
classes=np.genfromtxt("labels.csv",dtype=np.str , delimiter='\t')

x=np.random.shuffle(data)
y=x[classes]

do this preserves the order of shuffling ?

like image 258
vincent Avatar asked Apr 05 '17 10:04

vincent


People also ask

How do you shuffle data and labels in Python?

Approach 1: Using the number of elements in your data, generate a random index using function permutation(). Use that random index to shuffle the data and labels. Approach 2: You can also use the shuffle() module of sklearn to randomize the data and labels in the same order.

How do you shuffle two lists at once?

Method : Using zip() + shuffle() + * operator In this method, this task is performed in three steps. Firstly, the lists are zipped together using zip(). Next step is to perform shuffle using inbuilt shuffle() and last step is to unzip the lists to separate lists using * operator.

How do you randomly shuffle a dataset in Python?

One of the easiest ways to shuffle a Pandas Dataframe is to use the Pandas sample method. The df. sample method allows you to sample a number of rows in a Pandas Dataframe in a random order. Because of this, we can simply specify that we want to return the entire Pandas Dataframe, in a random order.

How do you randomise the order of items in a list?

Python Random shuffle() Method The shuffle() method takes a sequence, like a list, and reorganize the order of the items. Note: This method changes the original list, it does not return a new list.


1 Answers

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]
like image 74
Divakar Avatar answered Oct 21 '22 10:10

Divakar