Randomly shuffle items in each row of numpy array

  • A+
Category:Languages

I have a numpy array like the following:

Xtrain = np.array([[1, 2, 3],                    [4, 5, 6],                    [1, 7, 3]]) 

I want to shuffle the items of each row separately, but do not want the shuffle to be the same for each row (as in several examples just shuffle column order).

For example, I want an output like the following:

output = np.array([[3, 2, 1],                    [4, 6, 5],                    [7, 3, 1]]) 

How can I randomly shuffle each of the rows randomly in an efficient way? My actual np array is over 100000 rows and 1000 columns.


Since you want to only shuffle the columns you can just perform the shuffling on transposed of your matrix:

In [86]: np.random.shuffle(Xtrain.T)  In [87]: Xtrain Out[87]:  array([[2, 3, 1],        [5, 6, 4],        [7, 3, 1]]) 

Note that random.suffle() on a 2D array shuffles the rows not items in each rows. i.e. changes the position of the rows. Therefor if your change the position of the transposed matrix rows you're actually shuffling the columns of your original array.

If you still want a completely independent shuffle you can create random indexes for each row and then create the final array with a simple indexing:

In [172]: def crazyshuffle(arr):      ...:     x, y = arr.shape      ...:     rows = np.indices((x,y))[0]      ...:     cols = [np.random.permutation(y) for _ in range(x)]      ...:     return arr[rows, cols]      ...:  

Demo:

In [173]: crazyshuffle(Xtrain) Out[173]:  array([[1, 3, 2],        [6, 5, 4],        [7, 3, 1]])  In [174]: crazyshuffle(Xtrain) Out[174]:  array([[2, 3, 1],        [4, 6, 5],        [1, 3, 7]]) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: