AppDividend
Latest Code Tutorials

np.random.shuffle: How to Shuffle Array in Python

To shuffle randomly in the array, use the np random shuffle() method. The shuffle() function modifies the sequence in-place by shuffling its contents.

np.random.shuffle

The np.random.shuffle() method is used to modify the sequence in place by shuffling its content. The numpy random shuffle() method takes a single argument called seq_name and returns the modified form of the original sequence. In the case of multi-dimensional arrays, the array is shuffled only across the first axis.

Also, only the order of sub-arrays in multi-dimensional arrays is changed, not the content inside the sub-arrays. One point to keep in mind is that the random. The shuffle() method modifies the original sequence and does not return a new sequence.

You have to install numpy for this tutorial. Also, check your numpy version as well.

Syntax

numpy.random.shuffle (seq_name)

Parameters

The numpy shuffle() function takes only one parameter:

seq_name: This is the input sequence whose elements must be shuffled in place.

Return Value

The shuffle() method returns the modified form of the original sequence.

Programming Example

Program to show the working of numpy.random.shuffle()

# importing the numpy module
import numpy as np

# Making a list of integers
org_list = [10, 20, 30, 40, 50, 60]

# Converting  the list into a numpy array
seq = np.array(org_list)

# Printing content of original sequence
print("Original order of the sequence is :", seq)

# Performing shuffling operation
np.random.shuffle(seq)

# Printing the content of sequence after shuffling
print("\nOutput sequence obtained after shuffling is: ", seq)

Output

Original order of the sequence is : [10 20 30 40 50 60]

Output sequence obtained after shuffling is:  [10 50 40 30 60 20]

Explanation

In the above code example, we have taken a list of integers named org_list and have stored multiple integer elements inside the list. Then, we have passed the complete list as a parameter inside the np.array() method, which converts the usual list to a numpy array.

Then, we can view the order of the elements of the original sequence by printing its content. Then, this sequence is passed as the only parameter inside the numpy.random.shuffle() to modify its content.

The np random shuffle() method returns a shuffled sequence, and it could be verified by printing the order of elements in the modified sequence.

As seen from the output, the content of the original sequence also gets shuffled; no new sequence is returned by the method.

Program to understand the working of np.random.shuffle() method, in the case of multi-dimensional arrays.

See the following code.

# importing the numpy module
import numpy as np

# Making original sequence of multi-dimensional array
seq = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])

# Printing the shape of multi-dimensional array
print("Shape of original sequence is: ", seq.shape)

# Printing content of original sequence
print("Original order of the sequence is :", seq)

# Performing shuffling operation
np.random.shuffle(seq)

# Printing the content of sequence after shuffling
print("\nOutput sequence obtained after shuffling is: ", seq)
print("Shape of shuffled sequence is: ", seq.shape)

Output

Shape of original sequence is:  (3, 3)
Original order of the sequence is : [[9 8 7]
 [6 5 4]
 [3 2 1]]

Output sequence obtained after shuffling is:  [[6 5 4]
 [3 2 1]
 [9 8 7]]
Shape of shuffled sequence is:  (3, 3)

Explanation

In the above code example, a multi-dimensional array of shape 3X3 was made as an original sequence that contains few random integer values.

Now, we can view the order of the elements of the original sequence by printing its content. Then, this sequence is passed as the only parameter inside the numpy.random.shuffle() to modify its content. The shuffle() method returns a shuffled sequence. It could be verified by printing the order of elements in the modified sequence that the sub-arrays get shuffled only along its first axis, i.e., in rows.

Also, the content stored inside the sub-arrays remains in the same order, and they don’t get shuffled.

Multi-dimensional arrays shuffling using arange() and shuffle()

The Numpy arange() method returns the ndarray object containing evenly spaced values within the given range. We can create 9 random elements, then reshape them to (3 x 3), and then shuffle the elements using the np random shuffle() method.

# importing the numpy module
import numpy as np

arr = np.arange(9).reshape(3, 3)

np.random.shuffle(arr)

print(arr)

Output

[[3 4 5]
 [0 1 2]
 [6 7 8]]

If you rerun the file, then you will see the different outputs. That is what shuffle does. It randomizes the elements in a specific order within the shape of an array.

That’s it.

See also

Numpy random rand()

Numpy random randn()

Generate numpy random permutation

Python random module

Generate a random number

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.