How to Shuffle Two or Multiple NumPy Arrays in Python?

How to Reverse Range in Python?

The most common issue that Python programmers get into is how to shuffle two or multiple numpy arrays together. If you fall into this category, don’t worry; in this post, we will explain in detail how to shuffle two arrays. Continue reading. 🧾

In this article, we will shuffle two different NumPy arrays with the same length but in a different order. Shuffling two NumPy arrays in Python means reordering the elements of both arrays in the same pattern. 

Thus, in this tutorial, you will learn different ways to shuffle two given NumPy arrays, and we will make sure you understand all the ways with the help of simple examples. First, we understand what NumPy is and how we can use it. 

So let’s get started on the solutions right away.

 

 

What is NumPy in Python, and How to Use it?

Nearly all branches of research and engineering use the free source Python library known as NumPy (Numerical Python). It is the de facto standard for manipulating numerical data in Python and forms the basis of both the PyData and scientific Python ecosystems

There are other different libraries that Python programmers can use in Data Science, Artificial Intelligence,  and Scientific programs, including Pandas, SciPy, Matplotlib, scikit-learn, and scikit-image, which make substantial use of the NumPy API.

Although Python lists can be an alternative to arrays, they need to improve performance when processing vast quantities of numerical data. We use a Python module called NumPy to solve this problem. Numerical Python is the full name of the term NumPy. 

The array object ndarray(N dimension array) is provided by NumPy. They are comparable to typical Python sequences but differ in a few significant ways. Python itself is the only need for installing NumPy. You must use pip to install NumPy, and to do so; you have to enter the following:

 

Code

pip install numpy

 

After installing numpy, you can easily use it. Here is a simple example of using numpy in your code:

 

Code

# import numpy first, then you can use it

import numpy as np




# declare and initialize a simple array

myArray = [10, 20, 30, 40]




# Declare and initialize a numpy array

nArray = np.array(myArray)




# Print numpy on screen

print(nArray)

 

Output

[10 20 30 40]

 

 

Different Ways to Shuffle Two NP Arrays Together

Following are the ways to shuffle two NumPy arrays together:

  1. Using random Generator permutation.
  2. Using numpy random permutation.
  3. Using sklearn.utils.shuffle() function.
  4. Using random shuffle function.

 

Method 1: Using Random Generator Permutation

The permutation method of the NumPy Random Generator creates a new array with the values shuffled. Let’s understand how it works with the help of a simple example:

 

Code

#import numpy array

import numpy as np




# Declare and initialize two arrays

array1 = np.array([[8, 1], [2, 3], [4, 5], [6, 7]])

array2 = np.array([10, 20, 30, 40])




print("Both array before shuffling")

print("Array 1 : ", array1)

print("Array 2 : ", array2)




# Create a new Generator with the default BitGenerator

myGenerator = np.random.default_rng()




# Print shuffled array on screen

print("Both arrays after shuffling")

print("Array 1 : ", myGenerator.permutation(array1))

print("Array 2 : ", myGenerator.permutation(array2))

 

Output

Both array before shuffling

Array 1 : [[8 1]

 [2 3]

 [4 5]

 [6 7]]

Array 2 : [10 20 30 40]

Both arrays after shuffling

Array 1 : [[2 3]

 [8 1]

 [6 7]

 [4 5]]

Array 2 : [30 40 10 20]

 

 

Method 2: Using Random Permutation

We may obtain random samples of permutation and return sequences using the numpy.random.permutation() method. With the help of this function, we can easily shuffle two numpy arrays together. Let’s understand with the help of a simple example:

 

Code

#import numpy array

import numpy as np




# Declare and initialize two arrays

array1 = np.array([[8, 1], [2, 3], [4, 5], [6, 7]])

array2 = np.array([10, 20, 30, 40])




# store random permutation to a variable

myshuffler = np.random.permutation(len(array1))




# Use square notation to shuffle array

shuffledArray1 = array1[myshuffler]

shuffledArray2 = array2[myshuffler]




# Print shuffled array on screen

print(shuffledArray1)

print(shuffledArray2)

 

Output

[[6 7]

 [8 1]

 [2 3]

 [4 5]]

[40 10 20 30]

 

 

Method 3: Using sklearn.utils.shuffle() Function

Using the shuffle() method from the sklearn.utils module is an alternative strategy for solving the given problem. Arrays and sparse matrices can be consistently shuffled using the shuffle method. The two arrays can be passed as sklearn.utils.shuffle(array1, array2), which properly shuffles them before returning a shuffled version of each array. 

You can easily understand this concept with the help of the following example:

 

Code

#import numpy array and sklearn

import numpy as np

import sklearn




# Declare and initialize two arrays

array1 = np.array([[8, 1], [2, 3], [4, 5], [6, 7]])

array2 = np.array([10, 20, 30, 40])




print("Both array before shuffling")

print("Array 1 : ", array1)

print("Array 2 : ", array2)




# Shuffle both arrays together

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




# Print shuffled array on screen

print("Both arrays after shuffling")

print("Array 1 : ", array1)

print("Array 2 : ", array2)

 

Output

Both array before shuffling

Array 1 : [[8 1]

 [2 3]

 [4 5]

 [6 7]]

Array 2 : [10 20 30 40]

Both arrays after shuffling

Array 1 : [[2 3]

 [8 1]

 [4 5]

 [6 7]]

Array 2 : [20 10 30 40]

 

 

Method 4: Using Random Shuffle Method

The shuffle method from the numpy.random module is another comparable function that enables you to shuffle the specified arrays. It accepts a sequence as an input and rearranges its elements to produce the original sequence in a new order. Keep in mind that the original sequence itself is altered by the shuffle method. 

Let’s understand how the shuffle method will work with the help of a simple example:

 

Code

#import numpy array

import numpy as np




# Declare and initialize two arrays

array1 = np.array([[8, 1], [2, 3], [4, 5], [6, 7]])

array2 = np.array([10, 20, 30, 40])




print("Both array before shuffling")

print("Array 1 : ", array1)

print("Array 2 : ", array2)




# Shuffle both arrays with random shuffle function

shuffler = np.arange(len(array1))

np.random.shuffle(shuffler)




array1 = array1[shuffler]

array2 = array2[shuffler]




# Print shuffled array on screen

print("Both arrays after shuffling")

print("Array 1 : ", array1)

print("Array 2 : ", array2)

 

Output

Both array before shuffling

Array 1 : [[8 1]

 [2 3]

 [4 5]

 [6 7]]

Array 2 : [10 20 30 40]

Both arrays after shuffling

Array 1 : [[4 5]

 [6 7]

 [2 3]

 [8 1]]

Array 2 : [30 40 20 10]

 

 

Conclusion

To conclude the article on “how to shuffle two np arrays together“, we discussed four different methods. We can shuffle NumPy arrays with the help of shuffle and permutation methods. The main distinction between permutation and shuffle functions is that permutation returns a new array while the original array is left unchanged. Shuffle, however, altered the original array and did not produce a new one. 

This post discusses both approaches in-depth and provides perfect examples to help you understand the subject.

Share this article with your fellow programmers if you found it helpful, and let us know in the comments area below 👇 which way worked best for you to solve the puzzle of shuffling two np arrays.

Happy Coding!🙋

 

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share