Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to randomly shuffle data and target in python?

Tags:

I have a 4D array training images, whose dimensions correspond to (image_number,channels,width,height). I also have a 2D target labels,whose dimensions correspond to (image_number,class_number). When training, I want to randomly shuffle the data by using random.shuffle, but how can I keep the labels shuffled by the same order of my images? Thx!

like image 706
Demonedge Avatar asked Jan 29 '16 03:01

Demonedge


People also ask

How do I randomly shuffle 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 randomly shuffle elements in an array Python?

Shuffle an Array in Python Using the random.The random. shuffle() method takes a sequence as input and shuffles it. The important thing to note here is that the random. shuffle() does not return a new sequence as output but instead shuffles the original sequence.

How do you shuffle data and labels together 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 can you randomize the items of a list in place in Python?

The shuffle() method randomizes the items of a list in place.


1 Answers

from sklearn.utils import shuffle import numpy as np  X = np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]) y = np.array([0, 1, 2, 3, 4]) X, y = shuffle(X, y) print(X) print(y)    [[1 1 1]  [3 3 3]  [0 0 0]  [2 2 2]  [4 4 4]]   [1 3 0 2 4] 
like image 112
Foreever Avatar answered Sep 23 '22 14:09

Foreever