NP.random.shuffle – Python NumPy random.shuffle() Function

NP.random.shuffle: The random module is part of the NumPy library. This module includes the functions for generating random numbers. This module includes some basic random data generating methods, as well as permutation and distribution functions and random generator functions.

NumPy random.shuffle() Function:

Numpy random shuffle: The shuffle() function of the NumPy random module is used to modify a sequence in-place by shuffling its contents. When used with a multi-dimensional array, the function just shuffles the items along the first axis.

Syntax:

numpy.random.shuffle(x)

Parameters

x: This is Required. The array or list to be shuffled is specified by this.

Return Value:

It has No return value.

NumPy random.shuffle() Function in Python

Example1

NP random shuffle: Here, the random.shuffle() function shuffles the elements of the list.

Approach:

  • Import numpy module using the import keyword.
  • Pass the lower and upper limit range as arguments to the arange() function to get a list containing elements in the given range(0 to 7 here).
  • Print the above-given list.
  • Pass the above-given list as an argument to the random.shuffle() function to shuffle the elements of the above list.
  • Print the above-given list after shuffling.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np  
# Pass the lower and upper limit range as arguments to the arange() function
# to get a list containing elements in given range(0 to 7 here)
gvn_lst = np.arange(0, 8)

# Print the above given list
print("The given list is:", gvn_lst)

# Pass the above given list as an argument to the random.shuffle() function
# to shuffle the elements of the above list
np.random.shuffle(gvn_lst)

# Print the above given list after shuffling
print("The above given list after shuffling:", gvn_lst)

Output:

The given list is: [0 1 2 3 4 5 6 7]
The above given list after shuffling: [3 4 1 2 0 7 6 5]

Example2

NP shuffle: When used with a multi-dimensional array, the function just shuffles the items along the first axis.

Approach:

  • Import numpy module using the import keyword.
  • Pass the lower and upper limit range as arguments to the arange() function to get a list containing elements in the given range and reshape it to the given number of rows and columns using the reshape() function.
  • Store it in a variable.
  • Print the above-given matrix (multi-dimensional array)
  • Pass the above-given matrix as an argument to the random.shuffle() function to shuffle the elements of the above matrix.
  • Print the above-given matrix after shuffling.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np  
# Pass the lower and upper limit range as arguments to the arange() function
# to get a list containing elements in given range and reshape it to given 
# number of rows and columns using the reshape() function
# Store it in a variable.
gvn_matx = np.arange(2, 11).reshape(3,3)
# Print the above given matrix (multi-dimensional array)
print("The given matrix is:\n", gvn_matx)
print()
# Pass the above given matrix as an argument to the random.shuffle() function
# to shuffle the elements of the above matrix
np.random.shuffle(gvn_matx)

# Print the above given matrix after shuffling
print("The above given matrix after shuffling:\n", gvn_matx)

Output:

The given matrix is:
[[ 2 3 4]
 [ 5 6 7]
 [ 8 9 10]]
 
The above given matrix after shuffling:
[[ 8 9 10]
 [ 5 6 7]
 [ 2 3 4]]