HOW TO GENERATE RANDOM PERMUTATIONS OF PYTHON NUMPY ARRAY?

Random Permutations

Random Permutations means arrangement of the elements of array in different ways.

The shuffle() and permutation() methods from random module of python’s numpy library is used to generate permutations of arrays.

Shuffling Arrays

Shuffling means changing arrangement of elements in the array itself.

The shuffle() method is used to shuffle the elements of array. It changes the original array.

from numpy import random
import numpy as np

arr = np.array([1, 2, 3, 4])

random.shuffle(arr)

print(arr)

Output-

[3 4 1 2]

Generating Permutation of Arrays

The permutation() method is used to generate permutation of the array. It doesn’t changes to the original array, it returns a re-arranged array and the original array remains intact or unchanged.

from numpy import random
import numpy as np

arr = np.array([1, 2, 3, 4])

print(random.permutation(arr))

Output

[3 4 1 2]

Posted

in

Tags:

Comments

Leave a Reply