NumPy – Using random Generator.permuted() method (4 examples)

Updated: March 2, 2024 By: Guest Contributor Post a comment

In the vast arena of numerical computing and data science, NumPy stands as a cornerstone. It fuels the computational engine of Python with array objects, mathematical operations, and a suite of utilities to process numerical data efficiently. In this exploration, we dive into a specific facet of NumPy’s capabilities, the numpy.random.Generator.permuted() method, dissecting its functionality through a series of examples that scale in complexity.

Introduction to numpy.random.Generator.permuted()

The numpy.random.Generator.permuted() method is a tool in the NumPy library designed for random permutation or shuffling of arrays. Its signature flexibility accomodates singular arrays, array sequences, and even multidimensional arrays, providing a base for both simple random selection and complex dataset manipulations. Unlike the older numpy.random.shuffle(), it treats all dimensions of an array equitably, thus enabling a more intuitive and effective shuffling or permutation technique.

Example 1: Basic Array Permutation

Let’s kick off with the basics by permuting a simple array:

import numpy as np

rng = np.random.default_rng()
array = np.array([1, 2, 3, 4, 5])
permuted_array = rng.permuted(array)

print(permuted_array)

This might yield a varied output like the following since the permutation is random:

[5 2 4 1 3]

Note how every element has been shuffled, demonstrating the method’s straightforward application to one-dimensional arrays.

Example 2: Multidimensional Array Permutation

Moving into the realm of multidimensional arrays, permuted() shows its true flexibility. Consider a 2D array:

import numpy as np

rng = np.random.default_rng()

array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
permuted_array_2d = rng.permuted(array_2d, axis=0)

print(permuted_array_2d)

Sample result:

[[7 8 9]
 [1 2 3]
 [4 5 6]]

Here, the rows are permuted while the columns remain intact. The axis=0 parameter indicates that the shuffling occurs along the first axis (rows). This illustration serves to emphasize permutations in multidimensional contexts.

Example 3: Permutation with Dimension Preservation

What about preserving the dimensionality structure while still applying permutations? permuted() can achieve this too. For an array of lists:

import numpy as np

rng = np.random.default_rng()

list_array = np.array([[1, 'a'], [2, 'b'], [3, 'c']])
permuted_list_array = rng.permuted(list_array, axis=None)

print(permuted_list_array)

Output:

[['3' 'c']
 ['1' 'a']
 ['2' 'b']]

In this case, setting axis=None allows for cross-element permutation, shuffling the inner structure while moving elements across the original dimension boundaries.

Example 4: Advanced Scenario – Data Simulation

Advancing our journey into a more sophisticated use case, consider simulating data where permutation needs to be isolated within subsets of a dataset. This could be critical for simulations or anonymized data processing:

import numpy as np

rng = np.random.default_rng()

data = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12]])
labels = np.array(['A', 'B', 'A', 'B'])

# Permuted within labels
for label in np.unique(labels):
    idx = np.where(labels == label)
    data[idx] = rng.permuted(data[idx], axis=1)

print(data)

This output may vary, illustrating how data associated with each unique label (‘A’ and ‘B’) is permuted across columns but segregated based on label association, simulating a real-world application where data integrity by group must be preserved during permutation.

Conclusion

The numpy.random.Generator.permuted() method stands as a powerful tool for random permutation tasks, compatible with a diversity of data shapes and sizes. From simple array shuffling to complex, dimensionally aware data reorganization, it equips the data scientist with the flexibility to tackle a wide spectrum of randomization challenges, thereby enhancing data simulation, anonymization, and analysis workflows.