Introduction to Python Numpy Random Permutations

In This Article, You Will Learn About Python Numpy Random Permutations

Python Numpy Random permutations – Before moving ahead, let’s know a little bit about Python Numpy Random Data Distribution

Random Permutations of Elements

A Permutation is defined as arrangements of elements, e.g., [7,9,8] is a Permutation of [7,8,9] and vice-versa.

The NumPy Random module has two methods for Permutation of elements: shuffle() and permutation().

Shuffling Arrays

Shuffling Arrays means changing arranged elements of Array in Array itself.

Example – Returning randomly shuffling integers of the array.

from numpy import random
import numpy as np

Array = np.array([11, 12, 13, 14, 15])
random.shuffle(Array)

print(Array)
Output - 

[13 14 11 12 15]

As shown above, it returned an array with randomly shuffling elements.

Example – Returning randomly shuffling strings of the array.

from numpy import random
import numpy as np

Array = np.array(['c', 'b', 'a', 'e', 'd'])
random.shuffle(Array)

print(Array)
Output - 

['a' 'c' 'd' 'e' 'b']

As shown above, it returned an array with randomly shuffling elements.

The shuffle() method makes changes to the original array.

Generating Permutation of Arrays

Example – Generate a random permutation of elements of the array.

from numpy import random
import numpy as np

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

print(random.permutation(Array))
python-numpy-random-permutations

As a result, it returned an array with arrangement of random number.

Note: Permutation() method returns re-arranged array with no changing in original array.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Leave a Comment

Stay in the loop

codingstreets