NumPy Permutations
Submitted by moazkhan on Wednesday, June 24, 2020 - 22:20.
In this tutorial you will learn:
- What is Random Permutation?
- NumPy Permutation function
- NumPy Shuffle function
Random Permutation
Permutation is a mathematical term and permutation of a set is defined as the arrangement of its elements in an sequence or a linear order and if it is already arranged then permutation is the rearrangement of its elements in another sequence. The number of permutations for a specific data set can be calculated using a formula. NumPy provides following 2 functions for carrying out the permutation.- Permutation() function
- Shuffle() function
NumPy Permutation function
Random module in numpy library provides an in-built function permutation() which gives the permutation of an array as output. It does not give all the permutations of an array but only one in which we can find that the elements of the array have been rearranged. The function can take in multi dimensional arrays as arguments. It must be noted that the permutation() function does not affect the original array, rather it generates result which could be saved in another array or just be displayed. Lets take an example, in this example we are passing a 1D array as a parameter to the permutation function and when we rerun the program multiple times, we can observe that the same values are being displayed but every time their indexes are changed.- from numpy import random as r
- import numpy as np
- #declaring an array
- my_arr = np.array(['a','b','c','d','e'])
- print('Array on which permutation will be carried on:', my_arr)
- #carrying out permutation on array and saving it to another array
- per_arr = r.permutation(my_arr)
- from numpy import random as r
- import numpy as np
- #declaring an array
- my_arr = np.array([[0,10,20,30,40],[50,60,70,80,90],[100,110,120,130,140]])
- print('2D Array on which permutation will be carried on:', my_arr)
- #carrying out permutation on array and saving it to another array
- per_arr = r.permutation(my_arr)
- #printing the result
- print('Result of 2D permutated array', per_arr)
NumPy Shuffle Function
The output of shuffle() function is same as permutation() function except it make changes/ permutations in the array that is being passed on to the function as parameter and it shuffle the multi-dimensional array along the first axis only. Lets take an example in which I will shuffle a 1D array, here you can observe the difference that while carrying out permutation(), the result is saved in another array while during usage of shuffle() function, the result is not saved but the array being passed as an argument to the function has been changed/ reshuffled.- from numpy import random as r
- import numpy as np
- #declaring an array
- my_arr = np.array(['a','b','c','d','e'])
- print('Array on which shuffle function will be implied upon:', my_arr)
- #carrying out shuffle on array and while not saving it to another array
- r.shuffle(my_arr)
- #printing the result
- print('Result of a shuffled array', my_arr)
- from numpy import random as r
- import numpy as np
- #declaring an array
- my_arr = np.array([[0,10,20,30,40],[50,60,70,80,90],[100,110,120,130,140]])
- print('2D Array on which shuffle function will be implied upon:', my_arr)
- #carrying out shuffle on array and while not saving it to another array
- r.shuffle(my_arr)
- #printing the result
- print('Result of a shuffled array', my_arr)
Add new comment
- 261 views