Skip to main content

NumPy Permutations


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.

  1. from numpy import random as r
  2. import numpy as np
  3. #declaring an array
  4. my_arr = np.array(['a','b','c','d','e'])
  5. print('Array on which permutation will be carried on:', my_arr)
  6. #carrying out permutation on array and saving it to another array
  7. per_arr = r.permutation(my_arr)

Lets take another example, in this example we will perform permutation operation on a 2D array. Here you can observe that the permutation function only changes the position of rows only and there is no effect on the elements of each row.
  1. from numpy import random as r
  2. import numpy as np
  3. #declaring an array
  4. my_arr = np.array([[0,10,20,30,40],[50,60,70,80,90],[100,110,120,130,140]])
  5. print('2D Array on which permutation will be carried on:', my_arr)
  6. #carrying out permutation on array and saving it to another array
  7. per_arr = r.permutation(my_arr)
  8. #printing the result
  9. 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.

  1. from numpy import random as r
  2. import numpy as np
  3. #declaring an array
  4. my_arr = np.array(['a','b','c','d','e'])
  5. print('Array on which shuffle function will be implied upon:', my_arr)
  6. #carrying out shuffle on array and while not saving it to another array
  7. r.shuffle(my_arr)
  8. #printing the result
  9. print('Result of a shuffled array', my_arr)

Lets take a look at another example, in this example we will shuffle a 2D array. Here you can observe that the array is shuffled on the first axis only and only the position of rows is shuffled, the content of each array remains unchanged.
  1. from numpy import random as r
  2. import numpy as np
  3. #declaring an array
  4. my_arr = np.array([[0,10,20,30,40],[50,60,70,80,90],[100,110,120,130,140]])
  5. print('2D Array on which shuffle function will be implied upon:', my_arr)
  6. #carrying out shuffle on array and while not saving it to another array
  7. r.shuffle(my_arr)
  8. #printing the result
  9. print('Result of a shuffled array', my_arr)

Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.