Q:

Numpy shuffle 3-D numpy array by row

Suppose I have the following 3D matrix:
1 1 1
2 2 2
3 3 3
and behind it (3rd dimension):
a a a
b b b
c c c
Defined as the following if I am correct:
import numpy as np
x = np.array([[[1,1,1], 
               [2,2,2], 
               [3,3,3]],
              [["a","a","a"],
               ["b","b","b"],
               ["c","c","c"]]])

And I want to randomly shuffle my 3D-array by row becoming something like this:
2 2 2
1 1 1
3 3 3
behind:
b b b
a a a
c c c
*Note that a always belongs to 1, b to 2 and c to 3 (same rows)
How do I achieve this?

A:

Using np.random.shuffle:
import numpy as np

x = np.array([[[1,1,1], 
               [2,2,2], 
               [3,3,3]],
              [["a","a","a"],
               ["b","b","b"],
               ["c","c","c"]]])

ind = np.arange(x.shape[1])
np.random.shuffle(ind)

x[:, ind, :]

Output:
array([[['1', '1', '1'],
        ['3', '3', '3'],
        ['2', '2', '2']],

       [['a', 'a', 'a'],
        ['c', 'c', 'c'],
        ['b', 'b', 'b']]], dtype='<U21')

