• We just launched and are currently in beta. Join us as we build and grow the community.

NumPy Permutations

zeek90000

Intel Collector
Z Rep
0
0
0
Rep
0
Z Vouches
0
0
0
Vouches
0
Posts
152
Likes
59
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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)

Book traversal links for NumPy Permutations

  • ‹ NumPy Log
  • Up
  • NumPy Product ›


Download
You must upgrade your account or reply in the thread to view hidden text.
 

438,622

315,778

315,787

Top