# 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.

`from numpy import random as rimport numpy as np#declaring an arraymy_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 arrayper_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.
`from numpy import random as rimport numpy as np#declaring an arraymy_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 arrayper_arr = r.permutation(my_arr)#printing the resultprint('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 rimport numpy as np#declaring an arraymy_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 arrayr.shuffle(my_arr)#printing the resultprint('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.
`from numpy import random as rimport numpy as np#declaring an arraymy_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 arrayr.shuffle(my_arr)#printing the resultprint('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.