The Random
module in NumPy
helps make permutations of elements of an array.
Permutation refers to the arrangement of elements in an array. For example, [3, 1, 2] and [1, 3, 2] are permutations of the array elements in [1, 2, 3] and vice versa.
To make a permutation of elements in an array, we need to choose either of these two methods:
Shuffle()
methodPermutation()
methodshuffle()
methodThe shuffle()
method changes or “shuffles” the arrangements of elements in an array randomly. This method modifies the original array.
random.shuffle(array)
The shuffle()
method takes a single parameter value: an array object.
from numpy import random, arrayfrom random import shuffle# creating an arraymy_array = array([1, 2, 3, 4, 5])# calling the shuffle() methodshuffle(my_array)print(my_array)
random
and array
modules from numpy
.shuffle
method from random
module.my_array
using the array()
function.shuffle()
method on the array my_array
.my_array
.permutation()
methodThe permutation()
method is the same as the shuffle()
method, but it returns a re-arranged array and does not modify the original array.
random.permutation(array)
The permutation()
method takes a single parameter value: an array object.
from numpy import random, arrayfrom random import permutation# creating an arraymy_array = array([1, 2, 3, 4, 5])# calling the permutation() methodprint(permutation(my_array))print(my_array)
random
and array
modules from numpy
.my_array
using the array()
function.permutation()
method on the array my_array
and print the re-arranged array.permutation()
function does not modify the original array, we print the array my_array
.