How do you shuffle two arrays in python?

Your "scary" solution does not appear scary to me. Calling shuffle() for two sequences of the same length results in the same number of calls to the random number generator, and these are the only "random" elements in the shuffle algorithm. By resetting the state, you ensure that the calls to the random number generator will give the same results in the second call to shuffle(), so the whole algorithm will generate the same permutation.

If you don't like this, a different solution would be to store your data in one array instead of two right from the beginning, and create two views into this single array simulating the two arrays you have now. You can use the single array for shuffling and the views for all other purposes.

Example: Let's assume the arrays a and b look like this:

a = numpy.array([[[  0.,   1.,   2.],
                  [  3.,   4.,   5.]],

                 [[  6.,   7.,   8.],
                  [  9.,  10.,  11.]],

                 [[ 12.,  13.,  14.],
                  [ 15.,  16.,  17.]]])

b = numpy.array([[ 0.,  1.],
                 [ 2.,  3.],
                 [ 4.,  5.]])

We can now construct a single array containing all the data:

c = numpy.c_[a.reshape(len(a), -1), b.reshape(len(b), -1)]
# array([[  0.,   1.,   2.,   3.,   4.,   5.,   0.,   1.],
#        [  6.,   7.,   8.,   9.,  10.,  11.,   2.,   3.],
#        [ 12.,  13.,  14.,  15.,  16.,  17.,   4.,   5.]])

Now we create views simulating the original a and b:

a2 = c[:, :a.size//len(a)].reshape(a.shape)
b2 = c[:, a.size//len(a):].reshape(b.shape)

The data of a2 and b2 is shared with c. To shuffle both arrays simultaneously, use numpy.random.shuffle(c).

In production code, you would of course try to avoid creating the original a and b at all and right away create c, a2 and b2.

This solution could be adapted to the case that a and b have different dtypes.

  1. HowTo
  2. Python NumPy Howtos
  3. NumPy Shuffle Two Arrays

Created: May-08, 2021

  1. NumPy Shuffle Two Arrays With the sklearn.utils.shuffle() Function in Python
  2. NumPy Shuffle Two Arrays With the numpy.random.shuffle() Function
  3. NumPy Shuffle Two Corresponding Arrays With the numpy.random.permutation() Function in Python

This tutorial will introduce how to shuffle two NumPy arrays in Python.

NumPy Shuffle Two Arrays With the sklearn.utils.shuffle() Function in Python

Suppose we have two arrays of the same length or same leading dimensions, and we want to shuffle them both in a way that the corresponding elements in both arrays remain corresponding. In that case, we can use the shuffle() function inside the sklean.utils library in Python. This shuffle() function takes the arrays as input parameters, shuffles them consistently, and returns a shuffled copy of each array. See the following code example.

import numpy as np
from sklearn import utils

array1 = np.array([[0,0], [1,1], [2,2]])
array2 = np.array([0,1,2])

array1, array2 = utils.shuffle(array1, array2)
print(array1)
print(array2)

Output:

[[0 0]
 [2 2]
 [1 1]]
[0 2 1]

In the above code, we shuffled the two arrays, array1 and array2, with the shuffle() function inside the sklearn.utils library in Python. We first created both arrays with the np.array() function. We then shuffled the arrays with the shuffle() function inside the sklearn.utils library and saved the shuffled arrays inside array1 and array2. In the end, we printed the elements inside both arrays. The output shows that the elements of both arrays correspond even after shuffling.

NumPy Shuffle Two Arrays With the numpy.random.shuffle() Function

If we don’t want to import the sklearn package and want to achieve the same goal as the previous one by using the NumPy package, we can use the shuffle() function inside the numpy.random library. This shuffle() function takes a sequence and randomizes it. We can then use this randomized sequence as an index for the two arrays to shuffle them. The following code example shows us how we can shuffle two arrays with the numpy.random.shuffle() function.

import numpy as np

array1 = np.array([[0,0], [1,1], [2,2]])
array2 = np.array([0,1,2])

randomize = np.arange(len(array2))

np.random.shuffle(randomize)

array1 = array1[randomize]
array2 = array2[randomize]
print(array1)
print(array2)

Output:

[[2 2]
 [0 0]
 [1 1]]
[2 0 1]

We first created the arrays with the np.array() function. We then created a sequence of integers that is equal to the length of the second array with the np.arange(len(array2)) function. After that, we randomized the sequence with the shuffle() function inside the np.random library and used it as an index for both arrays to shuffle them.

NumPy Shuffle Two Corresponding Arrays With the numpy.random.permutation() Function in Python

We can also use the permutation() function inside the numpy.random library to create a randomized sequence of integers within a specified range in Python. This sequence can then be used as an index for both arrays to shuffle them.

import numpy as np

def shuffle(x, y):
    p = np.random.permutation(len(y))
    return x[p], y[p]


array1 = np.array([[0,0], [1,1], [2,2]])
array2 = np.array([0,1,2])

array1, array2 = shuffle(array1, array2)
print(array1)
print(array2)

Output:

[[0 0]
 [2 2]
 [1 1]]
[0 2 1]

In the above code, we defined a function shuffle() that takes two arrays and randomizes them with the permutation() function inside the numpy.random library in Python. We specified the length of the randomized sequence to be equal to the number of elements in the second array with the len(y) function. We then used the randomized sequence p as an index for both arrays and returned them. The shuffled arrays returned by the shuffle() function are stored inside the array1 and array2 arrays.

How do you shuffle two arrays in python?

How do I shuffle two arrays together in Python?

shuffle() Function in Python. Suppose we have two arrays of the same length or same leading dimensions, and we want to shuffle them both in a way that the corresponding elements in both arrays remain corresponding. In that case, we can use the shuffle() function inside the sklean. utils library in Python.

How do you shuffle data in Python?

In Python, you can shuffle (= randomize) a list, string, and tuple with random. shuffle() and random. sample() .

How do I shuffle a NumPy array?

You can use numpy. random. shuffle() . This function only shuffles the array along the first axis of a multi-dimensional array..
Generator. shuffle..
Generator. permutation..
Generator. permuted..

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.