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

NP random permutation: 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.permutation() Function:

Numpy random permutation: The permutation() function of the NumPy random module can randomly permute a sequence or return a permuted range. If x is a multidimensional array, only the first axis is shuffled.

Syntax:

numpy.random.permutation(x)

Parameters

x: This is Required. The array or list to be shuffled is specified by this. If x is an integer, permute np.arange(x) at random.

Return Value:

A permuted sequence or array range is returned

NumPy random.permutation() Function in Python

Example1

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).
  • Store it in a variable.
  • Print the above-given list.
  • Pass the above given list as an argument to the random.permutation() function to randomly permute the elements of the above list.
  • Store it in another variable.
  • Print the above-given list after permuting randomly.
  • 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)
# Store it in a variable.
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.permutation() function
# to randomly permute the elements of the above list
# Store it in another variable.
rslt = np.random.permutation(gvn_lst)
# Print the above given list after permuting
print("The above given list after permuting:", rslt)

Output:

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

Example2

NP.random.permutation: When used with a multi-dimensional array, the function just permutes 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.permutation() function to randomly permute the elements of the above matrix.
  • Store it in another variable.
  • Print the above-given matrix after permuting randomly.
  • 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.permutation() function
# to randomly permute the elements of the above matrix
# Store it in another variable.
rslt = np.random.permutation(gvn_matx)
# Print the above given matrix after permuting randomly
print("The above given matrix after permuting:\n", rslt)

Output:

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

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