Last updated: Apr 12, 2024
Reading timeยท4 min
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.
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])
Running the code sample produces the following output.
[0 2 1] -------------------------------------------------- [[2 4] [6 8] [3 5]] -------------------------------------------------- [3 5 4]
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.
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.
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]
If you have to do this often, define a reusable function.
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.
[[6 8] [3 5] [2 4]] -------------------------------------------------- [5 4 3]
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.
numpy.random.shuffle()
You can also use the numpy.random.shuffle() method to shuffle two NumPy arrays together.
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])
Running the code sample produces the following output.
[[2 4] [3 5] [6 8]] -------------------------------------------------- [3 4 5]
The numpy.random.shuffle()
method modifies a sequence in place by shuffling
its contents.
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.
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.
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
.
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]
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.
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 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.
You can learn more about the related topics by checking out the following tutorials: