Table of Contents
TogglePermutations and shuffling are techniques used to rearrange the elements of an array in a random order. While shuffling modifies the array in place, permutations create a new array with the elements rearranged.
These operations are useful for tasks such as data augmentation, bootstrapping, and testing the robustness of algorithms.
NumPy provides the numpy.random.shuffle() function to shuffle the elements of an array in place. This means that the original array is modified, and the elements are rearranged randomly.
As shown in the example below, the elements of the array are shuffled in place, meaning the original array is modified, and the elements are rearranged randomly −
# Open Compiler
import numpy as np
# Define an array
array = np.array([1, 2, 3, 4, 5])
# Shuffle the array in place
np.random.shuffle(array)
print("Shuffled array:", array)
Output:
Following is the output obtained −
Shuffled array: [1 2 5 4 3]
Unlike shuffling, permutations create a new array with the elements rearranged randomly without modifying the original array. NumPy provides the numpy.random.permutation() function to generate permutations.
In the following example, the original array remains unchanged, and a new array with the elements rearranged randomly is generated −
# Open Compiler
import numpy as np
# Define an array
array = np.array([1, 2, 3, 4, 5])
# Generate a permutation of the array
permuted_array = np.random.permutation(array)
print("Original array:", array)
print("Permuted array:", permuted_array)
Output:
This will produce the following result −
Original array: [1 2 3 4 5]
Permuted array: [4 2 5 1 3]
NumPy also supports shuffling multidimensional arrays. When you shuffle a multidimensional array, only the first axis is shuffled.
In this example, only the rows of the 2D array are shuffled, while the elements within each row remain in their original order −
# Open Compiler
import numpy as np
# Define a 2D array
array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Shuffle the array in place
np.random.shuffle(array_2d)
print("Shuffled 2D array:", array_2d)
Output:
Following is the output of the above code −
Shuffled 2D array:
[[4 5 6]
[1 2 3]
[7 8 9]]
Similar to shuffling, you can generate permutations of multidimensional arrays. When using the numpy.random.permutation() function, the array is flattened, permuted, and then reshaped to its original dimensions.
In this example, the rows of the 2D array are permuted, creating a new array with the rows rearranged in a different order −
# Open Compiler
import numpy as np
# Define a 2D array
array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Generate a permutation of the 2D array
permuted_array_2d = np.random.permutation(array_2d)
print("Original 2D array:", array_2d)
print("Permuted 2D array:", permuted_array_2d)
Output:
The output obtained is as shown below −
Original 2D array:
[[1 2 3]
[4 5 6]
[7 8 9]]
Permuted 2D array:
[[1 2 3]
[4 5 6]
[7 8 9]]
NumPy can also generate random permutations of a range of integers using the numpy.random.permutation() function. This is useful for creating random sequences of integers.
Following is an example to generate a random permutation of integers from 0 to 9 using the numpy.random.permutation() function −
# Open Compiler
import numpy as np
# Generate a random permutation of integers from 0 to 9
random_permutation = np.random.permutation(10)
print("Random permutation of integers from 0 to 9:", random_permutation)
Output:
Following is the output obtained −
Random permutation of integers from 0 to 9: [2 8 1 3 4 0 6 7 5 9]
To ensure reproducibility, you can set a specific seed before shuffling or generating permutations. This ensures that the same sequence of random numbers is generated each time you run the code.
By setting the seed, you ensure that the random shuffling produces the same result every time the code is executed as shown in the example below −
# Open Compiler
import numpy as np
# Set the seed for reproducibility
np.random.seed(42)
# Define an array
array = np.array([1, 2, 3, 4, 5])
# Shuffle the array in place
np.random.shuffle(array)
print("Shuffled array with seed 42:", array)
# Reset the seed and shuffle again to get the same result
np.random.seed(42)
np.random.shuffle(array)
print("Shuffled array with seed 42 (again):", array)
Output:
The output obtained is as shown below −
Shuffled array with seed 42: [2 5 3 1 4]
Shuffled array with seed 42 (again): [5 4 3 2 1]
Shuffling and permutations are commonly used in machine learning for splitting datasets into training and testing sets. By randomly shuffling the data, you ensure that the training and testing sets are representative of the overall dataset.
In this example, the dataset is shuffled, and then split into training and testing sets. This helps in ensuring that the training and testing sets are randomly distributed and representative of the overall dataset −
# Open Compiler
import numpy as np
# Define a dataset
dataset = np.array([[1, 'A'], [2, 'B'], [3, 'C'], [4, 'D'], [5, 'E']])
# Shuffle the dataset in place
np.random.shuffle(dataset)
print("Shuffled dataset:", dataset)
# Split the dataset into training and testing sets
train_set = dataset[:3]
test_set = dataset[3:]
print("Training set:", train_set)
print("Testing set:", test_set)
Output:
Following is the output obtained −
Shuffled dataset:
[[‘5’ ‘E’]
[‘4’ ‘D’]
[‘2’ ‘B’]
[‘1’ ‘A’]
[‘3’ ‘C’]]
Training set:
[[‘5’ ‘E’]
[‘4’ ‘D’]
[‘2’ ‘B’]]
Testing set:
[[‘1’ ‘A’]
[‘3’ ‘C’]]
Key Takeaway: Master permutations and shuffling with NumPy at Vista Academy!
