Let’s learn how to permute in Numpy. We will use Python Numpy permutation method.
There are two different use cases of permutations in Python you should bew aware of.
Permutation of random generated array
Use random permutation Numpy function and use a number of elements as an arguments. I need 10 elements array to randomly generate.
import numpy as np for i in range(5): my_array = np.random.permutation(10) print(f"My generated permuted array: \n {my_array}")
Python returned five different arrays 10 items each.
Permutation of existing array
Put your array as an argument of random permutation function.
import numpy as np my_array = np.array([1, 3, 5, 7, 9]) for i in range(5): permuted_array = np.random.permutation(my_array) print(f"My permuted array: \n {permuted_array}")
Your array has been permuted five times.