How to shuffle two NP arrays together [SOLVED]


Python

Reviewer: Deepak Prasad

In this article we will learn how to shuffle two np arrays together. We will first learn a bit about numpy library and arrays and then we will proceed to learn how to shuffle two np arrays together.

 

The Numpy Module

Numpy is the library used for handling arrays in python. It has many in-built functions that help greatly while dealing with arrays. The code base of numpy library can be found on Github. It is an open source library.

 

Why Use Numpy for Arrays?

In python we use lists as arrays. But the processing of lists is slow and Numpy provides Arrays objects that are 50 times faster than lists. The reason for numpy arrays being faster is that they use continuous memory which optimize the memory allocation process.

 

Numpy installation

The installation process is easy, you have to type in the following command to the terminal

pip install numpy

The output must be something like this

Collecting numpy
  Downloading numpy-1.23.3-cp310-cp310-win_amd64.whl (14.6 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.6/14.6 MB 1.7 MB/s eta 0:00:00
Installing collected packages: numpy
Successfully installed numpy-1.23.3

 

How to shuffle two np arrays together

The first approach is rather simple and a naive approach but it can shuffle the arrays. We will randomize the array indexes by using the random shuffle method, and assign these random indexes to the array, to  shuffle two np arrays together this can work. The code is written below

import numpy as np

# shuffle two np arrays together
x=np.array([1,2,4,4,3,6])
y=np.array([1,9,7,4,6,7])

r_indexes = np.arange(len(x))
np.random.shuffle(r_indexes)

x = x[r_indexes]
y = y[r_indexes]

print(x)
print(y)

The output of this code will be random, lets run the code three times and check if the output is different.

First run :

shuffle two np arrays together
the x array after shuffling becomes :  [6 2 4 4 1 3]
the y array after shuffling becomes  [7 9 4 7 1 6]

Second run :

shuffle two np arrays together
the x array after shuffling becomes :  [1 2 6 3 4 4]
the y array after shuffling becomes  [1 9 7 6 4 7]

Third run :

shuffle two np arrays together
the x array after shuffling becomes :  [3 1 2 4 4 6]
the y array after shuffling becomes  [6 1 9 7 4 7]

Hence the results show that the shuffling is accurate, every time the arrays shuffle and give different result.

Second method is to use permutations, and shuffle the indexes. The code is written below

import numpy as np

from numpy.random import permutation

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

perm = permutation(len(X))

X = X[perm]
Y = Y[perm]

print ("shuffle two np arrays together, X becomes ",X)
print("and Y becomes ", Y)

 shuffle two np arrays together

The output for 3 runs will be different

shuffle two np arrays together, X becomes  [2 4 8 7 3]
and Y becomes  [3 1 5 4 2]
shuffle two np arrays together, X becomes  [8 2 3 4 7]
and Y becomes  [5 3 2 1 4]
shuffle two np arrays together, X becomes  [4 7 8 3 2]
and Y becomes  [1 4 5 2 3]

 

Conclusion

In this article we learned how we can shuffle two np arrays together using permutations or randomize function from np module. We also learned a bit about the np module.

 

Azka Iftikhar

Azka Iftikhar

She is proficient in multiple programming languages, including C++, GO, and Java, she brings a versatile skillset to tackle a wide array of challenges. Experienced Computer Scientist and Web Developer, she excels as a MERN Stack Expert. You can check her professional profile on GitHub which captures her experience and portfolio.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment