How to shuffle two NumPy Arrays the same way (in Unison)

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. Shuffle two NumPy Arrays together (in Unison)
  2. Shuffle two NumPy Arrays together (in Unison) using numpy.random.shuffle()
  3. Shuffle two NumPy Arrays in Unison using sklearn

# Shuffle two NumPy Arrays the same way (in Unison)

You can use the numpy.random.permutation() method to shuffle two NumPy arrays together (in unison).

The method will return a randomly permuted sequence that you can then use to index the arrays.

main.py
import numpy as np arr1 = np.array([[2, 4], [3, 5], [6, 8]]) arr2 = np.array([3, 4, 5]) perm = np.random.permutation(len(arr1)) print(perm) print('-' * 50) print(arr1[perm]) print('-' * 50) print(arr2[perm])
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
[0 2 1] -------------------------------------------------- [[2 4] [6 8] [3 5]] -------------------------------------------------- [3 5 4]

shuffle two numpy arrays together in unison

The numpy.random.permutation() method randomly permutes a sequence or returns a permuted range.

We passed the length of one of the arrays to the method so that the generated sequence is of the expected length.

main.py
import numpy as np arr1 = np.array([[2, 4], [3, 5], [6, 8]]) arr2 = np.array([3, 4, 5]) perm = np.random.permutation(len(arr1)) print(perm) # ๐Ÿ‘‰๏ธ [0 2 1]

The last step is to use the permuted sequence to index the two arrays.

main.py
import numpy as np arr1 = np.array([[2, 4], [3, 5], [6, 8]]) arr2 = np.array([3, 4, 5]) perm = np.random.permutation(len(arr1)) print(perm) # ๐Ÿ‘‰๏ธ [2 1 0] print('-' * 50) # [[6 8] # [3 5] # [2 4]] print(arr1[perm]) print('-' * 50) print(arr2[perm]) # ๐Ÿ‘‰๏ธ [5 4 3]
The code for this article is available on GitHub

If you have to do this often, define a reusable function.

main.py
import numpy as np def shuffle_arrays(array1, array2): perm = np.random.permutation(len(array1)) return (array1[perm], array2[perm]) arr1 = np.array([[2, 4], [3, 5], [6, 8]]) arr2 = np.array([3, 4, 5]) tup = shuffle_arrays(arr1, arr2) print(tup[0]) print('-' * 50) print(tup[1])

Running the code sample produces the following output.

shell
[[6 8] [3 5] [2 4]] -------------------------------------------------- [5 4 3]

define reusable function that shuffles two arrays

The function takes the two arrays as parameters and uses the numpy.random.permutation() method to shuffle the two arrays.

The function returns a tuple containing the shuffled arrays, so make sure to access it at indices 0 and 1 to get the results.

# Shuffle two NumPy Arrays the same way (in Unison) using numpy.random.shuffle()

You can also use the numpy.random.shuffle() method to shuffle two NumPy arrays together.

main.py
import numpy as np def shuffle_arrays(array1, array2): rand = np.arange(len(array1)) np.random.shuffle(rand) return (array1[rand], array2[rand]) arr1 = np.array([[2, 4], [3, 5], [6, 8]]) arr2 = np.array([3, 4, 5]) tup = shuffle_arrays(arr1, arr2) print(tup[0]) print('-' * 50) print(tup[1])
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
[[2 4] [3 5] [6 8]] -------------------------------------------------- [3 4 5]

shuffle two numpy arrays using numpy random shuffle

The numpy.random.shuffle() method modifies a sequence in place by shuffling its contents.

main.py
import numpy as np arr1 = np.array([[2, 4], [3, 5], [6, 8]]) rand = np.arange(len(arr1)) print(rand) # ๐Ÿ‘‰๏ธ [0 1 2] np.random.shuffle(rand) print(rand) # ๐Ÿ‘‰๏ธ [1 2 0]

Once we've shuffled the sequence, we can use it to index the two arrays.

# Shuffle two NumPy Arrays in Unison using sklearn

You can also use the scikit-learn module to shuffle two NumPy arrays in unison.

First, make sure that you have the scikit-learn module installed.

shell
pip install scikit-learn numpy # or with pip3 pip3 install scikit-learn numpy

Once you have the module installed, import the shuffle method from sklearn.utils.

main.py
import numpy as np from sklearn.utils import shuffle arr1 = np.array([[2, 4], [3, 5], [6, 8]]) arr2 = np.array([3, 4, 5]) arr1, arr2 = shuffle(arr1, arr2, random_state=0) # [[6 8] # [3 5] # [2 4]] print(arr1) print('-' * 50) print(arr2) # ๐Ÿ‘‰๏ธ [5 4 3]

shuffle two numpy arrays using sklearn

The code for this article is available on GitHub

The sklearn.utils.shuffle method shuffles the supplied arrays in a consistent way.

The random_state argument is an integer that determines the random number generation for shuffling the data.

If you have to do this often, define a reusable function.

main.py
import numpy as np from sklearn.utils import shuffle def shuffle_arrays(array1, array2): return shuffle(array1, array2, random_state=0) arr1 = np.array([[2, 4], [3, 5], [6, 8]]) arr2 = np.array([3, 4, 5]) arr1, arr2 = shuffle_arrays(arr1, arr2) # [[6 8] # [3 5] # [2 4]] print(arr1) print('-' * 50) print(arr2) # ๐Ÿ‘‰๏ธ [5 4 3]
The code for this article is available on GitHub

The function takes the two arrays as parameters, shuffles them and returns the result.

I've also written an article on how to get N random rows from a NumPy array.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev