Numpy Permutation() | How to use np.random.permutation()

Introduction

In this tutorial, We will learn how to find the permutation of the numpy array. We can find the permutation of the numpy array using np.random.permutation() function. we will learn the calculation of this function in deep. We will give you a thorough explanation of every part of the code with examples.

What is Numpy Permutation?

Numpy is a toolkit that helps us in working with numeric data. It contains a set of tools for creating a data structure called a Numpy array. It is basically a row and column grid of numbers.

Numpy. random. permutation() function gives us the random samples of a sequence of permutation and returns sequence by using this method. If x is a multi-dimensional array, it is only shuffled along with its first index.

Syntax

numpy.random.permutation(x)

Parameters of np.random.permutation

x: It is an array. If the input in x is an integer value, then it randomly permutes np. arange(x). If the input in x is an array, then it makes a copy and shuffles the elements randomly.

Return Value of np.random.permutation

The np.random.permutation() function returns the permuted sequence or an array range.

Example of np.random.permutation

Let us understand the permutation function of the numpy module in detail with the help of different examples:

1. Taking x parameter as a integer

In this example, we will be importing a numpy library. Then, we will be taking input x(parameter) as an integer. After that, we will apply the permutation function and see the output.

#importing numpy library
import numpy as np

result = np.random.permutation(8)
print("Output : ",result)

Output:

Output :  [1 4 0 7 3 5 6 2]

Explanation:

  • Firstly, we will import a numpy module with an alias name as np.
  • Then, we will take the variable result in which we have applied the permutation() function.
  • At last, we have printed the output stored in the result variable.

2. Taking x parameter as a array on np.random.permutation

In this example, we will be importing a numpy library. Then, we will be taking input x(parameter) as an array. After that, we will apply the permutation function and see the output.

#importing numpy library
import numpy as np

arr = [1,2,3,4,5,6]
result = np.random.permutation(arr)
print("Output : ",result)

Output:

Output :  [1 5 3 2 6 4]

Explanation:

  • Firstly, we will import a numpy module with an alias name as np.
  • Then, we will take an input array in the variable named arr.
  • Then, we will take the variable result in which we have applied the permutation() function passing the input array in it.
  • At last, we have printed the output stored in the result variable.

3. Using arange and reshape function on np.random.permutation

In this example, we will be importing the numpy library. Then, we will be using the np. arange and reshape function from the numpy library. After that, we will apply the permutation function and see the output.

#importing numpy library
import numpy as np

arr = np.arange(16).reshape((4, 4))
result = np.random.permutation(arr)
print("output : ",result)

Output:

output :  [[ 4  5  6  7]
 [ 0  1  2  3]
 [ 8  9 10 11]
 [12 13 14 15]]

Explanation:

  • Firstly, we will import a numpy module with an alias name as np.
  • Then, we will take an input variable ‘arr’ in which we have applied numpy arrange() function with reshape().
  • Then, we will take the variable result in which we have applied the permutation() function passing the input array in it.
  • At last, we have printed the output stored in the result variable.

4. Implementing graph of permutation on np.random.permutation

In this example, we will be importing the numpy module and matplotlib module. Then, we will apply the permutation function with the value as an integer. After that, we will apply the graph function and print the output.

# import numpy
import numpy as np
import matplotlib.pyplot as plt
  
# Using permutation() method
arr = np.random.permutation(100)
  
c, bin, ig = plt.hist(arr, 15, density = True)
plt.show()

Output:

Implementing Graph Of Permutation On Np.Random.Permutation

Explanation:

  • Firstly, we will be importing the numpy module with an alias name as np and matplotlib. pyplot as plt.
  • Then, we will use the permutation function and store the output in the arr variable.
  • After that, we will be applying the plt library with hist function to make the histogram with their parameters.
  • At last, we have printed the output.
  • Hence, you can see the output.

Difference between permutation and shuffle function

Np.random.permutation()

Numpy.random.permutation() function gives us the random samples of a sequence of permutation and returns sequence by using this method.

Numpy.random.shuffle()

Numpy.random.shuffle() function gives us the modified sequence in-place by shuffling its contents.

Example

In this example, we will be importing the numpy library. Then, we will take an input array as input. After that, we will be applying both the function permutation and shuffle. Then, see the necessary changes by seeding the output.

#importing numpy library
import numpy as np

#taking input for permutaion and shuffle function
arr = [2,4,6,8,10]

#applying permutation function
result = np.random.permutation(arr)

#applyimg shuffle function
results = np.random.shuffle(arr)

#printing both the result
print("Permutation output : ",result)
print("shuffle output : ",results) 

Output:

Permutation output :  [ 4  8  6  2 10]
shuffle output :  [4, 6, 2, 8, 10]

Explanation:

  • Firstly, we have imported the numpy library with an alias name as np.
  • Then, we have taken the input array in the variable as arr.
  • After that, we have applied the numpy permutation() with passing the input array parameter.
  • Then, we have applied the shuffle() function and stored the output in the results variable.
  • At last, we have printed both the output and see the changes in the output.
  • Hence, you can see the output.

Conclusion

In this tutorial, we have learned about the concept of numpy random permutation. We have discussed all the ways through which we can use the permutation concept and also discussed the example in detail. We have also discussed the difference between the numpy permutation() and numpy shuffle() with the example explained in detail. You can use the permutation function as required by you in the program.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments