لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

Python Data Science NumPy Random Permutations

The Shuffle() And The permutation() Methods

Random Permutations of Elements refers to an arrangement of elements. e.g. [2, 3, 4, 1] is a permutation of [1, 2, 3, 4] and vice-versa.

The NumPy Random module provides two methods for this

1. shuffle() and

2. permutation()

The Shuffle() method: Shuffling Arrays Shuffle means changing arrangement of elements in-place. i.e. in the array itself.

Example 1: Randomly shuffle elements of the given array.

Code

from numpy import random
import numpy as np

arr = np.array([11, 12, 13, 14, 15])

random.shuffle(arr)

print(arr)

the output will be

[14 13 12 15 11]

Note: The shuffle() method makes changes to the original array.
Each time you run the code to shuffle the output will be different because it is generated randomly.


How to Generate Permutation of Arrays

To permutate means to change, interchange especially to arrange in a different order i.e. re-arrange.

Example 2: Example Generate a random permutation of elements from given array.

Code

from numpy import random
import numpy as np

arr = np.array([11, 12, 13, 14, 15])

print(random.permutation(arr))

the output will be
[13 11 14 15 12]

The permutation() method returns a re-arranged array and leaves the original array un-changed.
Each time you run the code to permutate the output will be different because it is generated randomly.