close

How to randomly shuffle data and target in python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to randomly shuffle data and target in python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to randomly shuffle data and target in python?

  1. How to randomly shuffle data and target in python?

    There is another easy way to do that. Let us suppose that there are total N images. Then we can do the following:

  2. randomly shuffle data and target in python

    There is another easy way to do that. Let us suppose that there are total N images. Then we can do the following:

Method 1

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]

Method 2

There is another easy way to do that. Let us suppose that there are total N images. Then we can do the following:

from random import shuffle

ind_list = [i for i in range(N)]
shuffle(ind_list)
train_new  = train[ind_list, :,:,:]
target_new = target[ind_list,]

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read