I just tried to show with an example so it can help others. , xn), of n elements, which could stand for playing cards or any other objects we want to randomly permute. import numpy anumpy.array(1,2) ba.sum() print type(b) (b) - If I run it (Python 2.5, numpy 1.0.1 on a. Where as (x) has changed original data and does not return a new variable. Generating Random Permutations qThe input to the random permutation problem is a list, X (x1, x2. If x is an array, make a copy and shuffle the elements randomly. If x is an integer, randomly permute np.arange (x).
(x) actually returns a new variable and the original data is not changed. So you could use function to generate the index array and use it to shuffle multiple arrays. Randomly permute a sequence, or return a permuted range. def invertpermutationnumpy2 (permutation): inv. I found that this solution can speed up the computation of pinv by an order of magnitude. Shuffle means changing arrangement of elements in-place. p np.random.permutation (n) pinv np.argsort (p) The same idea as approach 1, but using the solution provided here. The NumPy Random module provides two methods for this: shuffle() and permutation(). Key inference is: When x is an array, both (x) and (x) can permute the elements in x randomly along the first axis. Computing a random permutation and its inverse separately using inbuilt NumPy functions. To start with I have created an array which is of shape 3,3 and has numbers from 0 to 8 import numpy as np x1 = np.array(np.arange(0,9)).reshape(3,3) #array with shape 3,3 and have numbers from 0 to 8 #step1: using np.random.permutation x_per = np.random.permutation(x1) print('x_per:', x_per) print('x_1:', x_1) #Inference: x1 is not changed and x_per has its rows randomly changed #The outcome will be x1: ] x_per: ] #Lets apply shuffling x2 = np.array(range(9)).reshape(3,3) x2_shuffle = np.random.shuffle(x2) print('x2_shuffle:', x2_shuffle) print('x2', x2) #Outcome: x2_shuffle: None x2 ]