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!
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.
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.
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.
The shuffle() method randomizes the items of a list in place.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With